当前位置:首页 » 编程语言 » 多项式相加c语言

多项式相加c语言

发布时间: 2022-04-19 11:11:45

c语言 多项式加法

你的判断有问题,代码如下,自己找一下问题:

#include<iostream>
#include<queue>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<bitset>
#include<map>
#include<string>

usingnamespacestd;

intmain()
{
inti,a,b,first,array[2001];;
//while(scanf("%d",&n)!=EOF)
{
first=1;
memset(array,0,sizeof(array));
//多项式一
while(scanf("%d%d",&a,&b)&&(a!=0||b!=0))
array[a+1000]=b;
//多项式二
while(scanf("%d%d",&a,&b)&&(a!=0||b!=0))
//相同次数系数相加
array[a+1000]+=b;

//输出
for(i=2001;i>=0;i--)
{
//系数为0的整数对不用输出
if(array[i]==0)
continue;
//控制格式
if(first)
first=0;
else
printf(" ");

printf("%d%d",i-1000,array[i]);
}
printf(" ");
}
return0;
}

❷ c语言多项式相加

我这有一个实现加减乘除的多项式程序,自己写的,另外输入形式为:-2x^3 +5x^2+3x+4 即可。

其中网络的现实问题,有一个&;A和&;B的 应该是& COPYA, & COPYB
去掉中间空格

支持整数多项式加减乘除。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

typedef struct _POLYNODE{
int coef;//系数
int exp;//指数
struct _POLYNODE *next;
}polynode,*polyptr;

void createPoly(polynode **P, char ch[]);//建立多项式链表
void polyAdd(polynode *A,polynode *B);//多项式加
void polyMinus(polynode *A,polynode *B);//减
void polyMulti(polynode *A,polynode *B);//乘
void polyDiv(polynode *A,polynode *B);//除
void order(polynode **P);//排序
void display(polynode *P);//展示多项式
void destroy(polynode **P);//销毁多项式
void menu();//命令菜单
int isPut(char ch[]);
//菜单
void menu(){
printf("1.输入多项式.\n"
"2.多项式相加.\n"
"3.多项式相减.\n"
"4.多项式相乘.\n"
"5.多项式相除.\n"
"6.显示多项式.\n"
"7.销毁多项式.\n"
"8.退出.\n");
}
//判断菜单选择
int IsChoice(int choice){
if(0 < choice && 9 > choice)
return 1;
else
return 0;
}

int isPut(char ch[]){
int i,j = 1;
for(i = 0; ch[i] != '\0'; i++){
{if(0 == j && '^' == ch[i])
return 0;
if('^' == ch[i] && 1 == j)
j = 0;
if(('+' ==ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && 0 == j)
j = 1;

}
if('.' != ch[i] && 'x' != ch[i] && 'X' != ch[i] && '^' != ch[i] && '+' != ch[i] && '-' != ch[i] && '*' != ch[i] && '/' != ch[i] && !isdigit(ch[i]))
return 0;
else{
if('+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0] || '.' == ch[0])
return 0;
if('\0' == ch[i+1] && '+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0])
return 0;
// 上面是判断字符串首尾是否合格 下面是中间部分
if(0 != i && ch[i+1] != '\0' ){
if(('X' == ch[i] || 'x' == ch[i]) && !isdigit(ch[i-1]) && '+' != ch[i-1] && '-' != ch[i-1] && '*' != ch[i-1] && '/' != ch[i-1] && '.' != ch[i-1])
return 0;
if(('X' == ch[i] || 'x' == ch[i]) && '^' != ch[i+1] && '+' != ch[i+1] && '-' != ch[i+1] && '*' != ch[i+1] && '/' != ch[i+1])
return 0;
if(('+' == ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && !isdigit(ch[i-1]) && 'X' != ch[i-1] && 'x' != ch[i-1] && !isdigit(ch[i+1]) && 'X' != ch[i+1] && 'x' != ch[i+1])
return 0;
if('^' == ch[i] && 'X' != ch[i-1] && 'x' != ch[i-1])
return 0;
if('^' == ch[i] && !isdigit(ch[i+1]))
return 0;
if('.' == ch[i] && !isdigit(ch[i+1]) && !isdigit(ch[i-1]))
return 0;
}
}
}
return 1;
}

void createPoly(polynode **P, char ch[]){
char *t = ch;
int i = 0, j = 1;
int iscoef = 1,isminus = 1;
polyptr Q,L;

if('-' == ch[0]){
isminus = -1;
*t++;
}
while('\0' != *t){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = 1;
Q->exp = 0;
Q->next = NULL;//申请节点,初始化参数为1.

if(-1 == isminus){
Q->coef *= isminus;
isminus = 1;
}

while('+' != *t && '-' != *t && '*' != *t && '/' != *t && '\0' != *t){
if('x' != *t && 'X' != *t){
while(isdigit(*t)){
i =((int)*t - 48) + i*10;
t++;
j *= i;
}//抽取数字
if(1 == iscoef && 0 != i){
Q->coef *= i;
}
if(0 == iscoef){
Q->exp += i;
iscoef = 1;
}
//如果i=0,则

}
else{
iscoef = 0;
t++;
if('^' == *t)
t++;
else
Q->exp = 1;
}
i = 0;
}//while 遍历到加减乘除,则退出循环,到下一新的节点
iscoef = 1;
if('\0' != *t){
if('-' == *t)
isminus = -1;
t++;
}
if(0 == j){
Q->coef = 0;
j = 1;
}
printf("系数:%d,指数:%d\n",Q->coef,Q->exp);

if(NULL == *P){
*P = Q;
}
else{
L->next = Q;
}
L = Q;

}//while遍历整个字符串

}
void polyAdd(polynode *A,polynode *B){
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}

while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}

L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.
order(&;B);
order(&;B);
printf("相加结果为:");
display(COPYB);
destroy(&;B);
}
void polyMinus(polynode *A,polynode *B){//相减和相加差不多
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}

while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = -(P->coef);
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}

L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.
order(&;B);
order(&;B);
printf("相减结果为:");
display(COPYB);
destroy(&;B);

}
void polyMulti(polynode *A,polynode *B){
polyptr R = A, P = B, Q, L = NULL, T;

if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
if(0 == A->coef || 0 == B->coef){
printf("多项式乘积为:0\n");
return ;
}

while(NULL != R){
while(NULL != P){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;

if(NULL == L)
L = Q;
else
T->next = Q;
T = Q;
P = P->next;
}
P = B;
R = R->next;
}
order(&L);
order(&L);
printf("多项式乘积为:\n");
display(L);
destroy(&L);
}
void polyDiv(polynode *A,polynode *B){//多项式除法
polyptr P = A, Q,L,R,T,C,D;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
if(A->coef == 0){
printf("0.\n");
return ;
}
if(B->coef == 0){
printf("除数为0,错误!\n");
return ;
}
if(A->coef < B->coef){
printf("商:0,余数为:");
display(A);
return ;
}
while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = -(P->coef);
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
C = P = Q = L = R = T = NULL;

//------------------开始计算
while(COPYA->exp >= COPYB->exp){
D = COPYA;
while(NULL != D->next)
D = D->next;
R = COPYB;
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = (-COPYA->coef) / R->coef;
Q->exp = COPYA->exp - R->exp;
Q->next = NULL;
if(NULL == L)
L = Q;
else
P->next = Q;
P = Q;

while(NULL != R){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;

if(NULL == T)
T = Q;
else
C->next = Q;
C = Q;

R = R->next;
}
D->next = T;
order(&;A);
order(&;A);
T = NULL;
C = NULL;
}
order(&L);
order(&;A);
printf("A除以B,商:");
display(L);
printf("余数:");
display(COPYA);

destroy(&L);
destroy(&;A);
destroy(&;B);
}

void display(polynode *P){
//考虑情况有系数为1,指数为1,0,一般数;系数为系数不为1,指数为1,0,一般数;
//系数为负数,指数为1,0,一般数,主要考虑中间符号问题.
if(NULL == P){
printf("多项式为空.\n");
return ;
}
if(1 == P->coef){
if(0 == P->exp)
printf("1");
else if(1 == P->exp) printf("x");
else printf("x^%d",P->exp);
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
P = P->next;
while(NULL != P){
if(0 < P->coef){
if(1 == P->coef){
if(0 == P->exp)
printf("+1");
else if(1 == P->exp) printf("+x");
else printf("+x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("+%d",P->coef);
else if(1 == P->exp) printf("+%dx",P->coef);
else
printf("+%dx^%d",P->coef,P->exp);
}
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
}
P = P->next;
}
printf("\n");
}

void destroy(polynode **P){
polyptr Q = *P;
if(NULL == *P)
return ;
while(*P != NULL){
Q = *P;
*P = (*P)->next;
delete Q;
}

}

void order(polynode **P){
//首先 系数为零的要清掉,其次指数从高到低排序,再者系数相同的要合并.
polyptr prev,curr,OUT,INcurr;//前一节点和当前节点
int temp;

//出去第一节点系数为0的项
while(NULL != *P){
if(0 != (*P)->coef)
break;
else{
if(NULL == (*P)->next)
return;
curr = *P;
(*P) = (*P)->next;
delete curr;
}
}
if(NULL == *P || NULL == (*P)->next)//如果只剩1项或空,则不需要整理,退出函数
return;
//冒泡排序
OUT = INcurr = *P;
while(NULL != OUT->next){//外循环
while(NULL != INcurr->next){//内循环
prev = INcurr;
INcurr = INcurr->next;
if(prev->exp < INcurr->exp){
temp = prev->coef;
prev->coef = INcurr->coef;
INcurr->coef = temp;//交换系数

temp = prev->exp;
prev->exp = INcurr->exp;
INcurr->exp = temp;//交换指数
}
}
OUT = OUT->next;
INcurr = *P;
}

//去除0项
prev = curr = *P;
curr = curr->next;
while(NULL != curr){
if(0 == curr->coef){
prev->next = curr->next;
delete curr;
curr = prev->next;
}
else{
prev = curr;
curr = curr->next;
}
}
//合并同类项
OUT = INcurr = *P;
while(NULL != OUT->next){
while(NULL != INcurr->next){
prev = INcurr;
INcurr = INcurr->next;
if(INcurr->exp == OUT->exp){
OUT->coef += INcurr->coef;
prev->next = INcurr->next;
delete INcurr;
INcurr = prev;
}
}
INcurr = OUT = OUT->next;
if(NULL == OUT)
return;
}

}

void main(){
int choice;
// int i;
char ch[100];
polynode *polyA,*polyB;

polyA = polyB = NULL;

menu();
scanf("%d",&choice);
while(!IsChoice(choice)){
menu();
printf("输入错误,重新输入.\n");
scanf("%d",&choice);
}
while(8 != choice){
switch(choice){
case 1:
if(NULL != polyA || NULL != polyB){
destroy(&polyA);
destroy(&polyB);
printf("原多项式被销毁.\n");
}
printf("多项式输入格式:4x^3+7x^2+x+6--x不分大小写.\n输入多项式A:\n");
scanf("%s",&ch);

while(!isPut(ch)){
printf("输入错误!重新输.\n");
scanf("%s",&ch);
}
createPoly(&polyA,ch);//建立多项式A链表

printf("输入多项式B:\n");
scanf("%s",&ch);
while(!isPut(ch)){
printf("输入错误!重新输.\n");
scanf("%s",&ch);
}
createPoly(&polyB,ch);//建立多项式B链表
order(&polyB);
order(&polyA);//整理排序多项式,默认降幂

printf("建立多项式成功!多项式:\nA为:");
display(polyA);
printf("B为:");
display(polyB);
break;
case 2:
polyAdd(polyA,polyB);
break;
case 3:
polyMinus(polyA,polyB);
break;
case 4:
polyMulti(polyA,polyB);
break;
case 5:
printf("PS:系数只支持整数,计算除法存在误差;\n如果除数所有项系数为1,能得到正确答案,或者某些情况系数刚好被整除.");
polyDiv(polyA,polyB);
break;
case 6:
printf("------显示多项式------\nA :");
display(polyA);
printf("B :");
display(polyB);
break;
case 7:
destroy(&polyA);
destroy(&polyB);
printf("此多项式已被清空.\n");
break;
default:
return ;
}
choice = 0;
menu();
scanf("%d",&choice);
while(!IsChoice(choice) || 0 == choice){
menu();
printf("输入错误,重新输入.\n");
scanf("%d",&choice);
}
}
}

❸ 两个多项式相加运算(用c语言)

#include<stdio.h>
#include<malloc.h>
#define Null 0
typedef struct Node
{
int coeff;
int expo;
Node *next;
}listNode,*list;

list createList()
{
list head;
head = (list)malloc(sizeof(listNode));
head = NULL;
printf("want to create a new node?y/n\n");
char ch;
fflush(stdin);
scanf("%c",&ch);
while(ch=='Y' || ch== 'y')
{
list p;
p = (list)malloc(sizeof(listNode));
printf("input coeff\n");
int coeff;
scanf("%d",&coeff);
p->coeff = coeff;
printf("input expo\n");
int expo;
scanf("%d",&expo);
p->expo = expo;
p->next = NULL;
//链表为空的时候,即添加首个元素
if(head == NULL)
{
head=p;//添加代码
}
else
{
list prev,curr;
curr = head;
prev = NULL;
//找到添加位置
while(curr!=NULL && curr->expo>p->expo)
{
prev=curr;
curr=curr->next;//添加代码
}
if(curr!=NULL && curr->expo == p->expo)
{
curr->coeff = curr->coeff + p->coeff;
printf("want to create a new node?y/n\n");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y' || ch== 'y')
continue;
else
return head;
}
//插入结点,结点非首
if(prev != NULL)
{
p->next=curr;
prev->next=p;
//添加代码
}
//插入结点,结点为首
else
{
p->next=curr;
head=p;
//添加代码
}

}
printf("want to create a new node?y/n\n");
fflush(stdin);
scanf("%c",&ch);
}
return head;
}

list add(list head1,list head2)
{
list head,newNode,ptr1,ptr2,ptr3;
ptr1 = head1;
ptr2 = head2;
head = NULL;
while(ptr1 != NULL && ptr2 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
if(ptr1->expo > ptr2->expo)
{
newNode->coeff = ptr1->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr1 = ptr1->next;
}
else if(ptr1->expo < ptr2->expo)
{
newNode->coeff = ptr2->coeff;
newNode->expo = ptr2->expo;
newNode->next = NULL;
ptr2 = ptr2->next;//添加代码
}
else
{
newNode->coeff = ptr1->coeff + ptr2->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
if(head==NULL)
{
head = newNode;
}
else
{
ptr3 = head;
//添加代码
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;
}
}
while(ptr1 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
newNode->coeff = ptr1->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr3 = head;
if(ptr3 == NULL)
head = ptr3 = newNode;
else
{
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;//添加代码
}
ptr1 = ptr1->next;
}
while(ptr2 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
newNode->coeff = ptr2->coeff;
newNode->expo = ptr2->expo;
ptr3 = head;
if(ptr3 == NULL)
head = ptr3 = newNode;
else
{
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;
}
ptr2 = ptr2->next;
}
return head;
}

void display(list head)
{
list ptr = head;
while(ptr != NULL)
{
if(ptr != head )
printf("+");
printf("%d",ptr->coeff);
printf("x^");
printf("%d",ptr->expo);
ptr = ptr->next;
}
printf("\n");
}

int main(int argc, char* argv[])
{
list head,head1,head2;
printf("input the first list\n");
head1 = createList();
display(head1);
printf("input the second list\n");
head2 = createList();
display(head2);
head = add(head1,head2);
display(head);
return 0;
}

❹ C语言写多项式相加怎么写

/*
2007-3-22
一元多项式的加法
*/

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedefstructPolyNode
{
intcoef;
intexp;
structPolyNode*next;
}node;

node*CreatePoly(void)
{
node*h,*tail,*s;
intcoef,exp;
h=(node*)malloc(sizeof(node));
if(!h)
{
exit(-1);
}
h->next=NULL;
tail=h;
printf("请输入每一项额系数和指数(中间以逗号隔开):/n");
printf("coef,exp:");
scanf("%d,%d",&coef,&exp);
while(coef)
{
s=(node*)malloc(sizeof(node));
if(!s)
{
exit(-1);
}
s->coef=coef;
s->exp=exp;

s->next=tail->next;
tail->next=s;
tail=s;

printf("ceof,exp:");
scanf("%d,%d",&coef,&exp);
}
returnh;
}

voidPolyAdd(node*polya,node*polyb)
{
node*p,*q,*pre,*temp;
intsum=0;;
p=polya->next;
q=polyb->next;
pre=polya;
while(p&&q)
{
if(p->exp<q->exp)
{
pre->next=p;
pre=p;
p=p->next;
}
elseif(p->exp>q->exp)
{
pre->next=q;
pre=q;
q=q->next;
}
else
{
sum=p->coef+q->coef;
if(sum)
{
p->coef=sum;
pre->next=p;
pre=p;
p=p->next;

temp=q->next;
free(q);
q=temp;
}
else
{
temp=p->next;
free(p);
p=temp;
temp=q->next;
free(q);
q=temp;
}
}
}
pre->next=p?p:q;
}

intPrin(node*h)
{
node*p=h->next;
while(p)
{
printf("%d*x^%d",p->coef,p->exp);
p=p->next;
}
printf("/n");
return1;
}

intmain(void)
{
node*polya,*polyb;
printf("请输入第一个一元多项式的系数和指数(假定以输入系数为0来结束):/n");
polya=CreatePoly();
printf("请输入的第一个一元多项式为:/n");
Prin(polya);
printf("请输入第二个一元多项式的系数和指数(假定以输入系数为0来结束):/n");
polyb=CreatePoly();
printf("请输入的第二个一元多项式为:/n");
Prin(polyb);
printf("这两个一元多项式相加后的结果为:/n");
PolyAdd(polya,polyb);
Prin(polya);
return1;
}

❺ C语言,描述多项式加法

1. #include"malloc.h"
2. typedef struct Polyn /*定义多项式每一项的类型*/
3. {
4. float cofe; /*每一项系数*/
5. int expn; /*每一项的指数*/
6. struct Polyn *next;
7. struct Polyn *prior;
8. }*Pol.yn,SNode;
9. void setPolyn(Polyn *t1,Polyn *t2,int n) /*建立多项式*/
10. {
11. float a;
12. int i,b;
13. *t1=(SNode *)malloc(sizeof(SNode)); /*创建头节点*/
14. (*t1)->next=NULL;
15. for(i=0;i<n;i++)
16. {
17. *t2=(SNode *)malloc(sizeof(SNode));
18. scanf("%f,%d;",&a,&b);
19. (*t2)->cofe=a; (*t2)->expn=b;
20. (*t1)->next->prior=(*t2);
21. (*t2)->prior=(*t1);
22. (*t2)->next=(*t1)->next;
23. (*t1)->next=(*t2);
24. } /*创建每一项并连接成多项式*/
25. }
26. void print(Polyn *t) /*输出多项式*/
27. {
28. Polyn p;
29. for(p=(*t)->next;p!=NULL;p=p->next)
30. printf("%f,%d;",p->cofe,p->expn);
31. printf("\n");
32. }
33. void arrange(Polyn *t) /*化简多项式*/
34. {
35. float m1;
36. int m2;
37. Polyn p,q,r,s;
38. for(p=(*t)->next;p!=NULL;p=p->next)
39. for(q=p->next;q!=NULL;q=q->next)
40. if((p->expn)>(q->expn))
41. {
42. m1=p->cofe;p->cofe=q->cofe;q->cofe=m1;
43. m2=p->expn;p->expn=q->expn;q->expn=m2;
44. } /*冒泡法多项式指数排序*/
45. for(p=(*t)->next;p!=NULL;p=p->next)
46. if((p->expn)==(p->next->expn))
47. {
48. r=p->next;
49. p->cofe+=p->next->cofe;
50. p->next=p->next->next;
51. p=p->prior; /*指针指向上一结点(须重新处理现在处理 的结点)*/
52. free(r);
53. if((p->next->cofe)==0)
54. {
55. s=p->next;
56. p->next=p->next->next;
57. free(s);
58. }
59. } /*多项式相同指数项系数求和,化简多项式*/
60. }
61. void linkPolyn(Polyn *t1,Polyn *t2) /*两个多项式连接*/
62. {
63. Polyn p,q;
64. for(p=(*t2)->next;p!=NULL;p=p->next)
65. q=p;
66. p=(*t2)->next;
67. q->next=(*t1)->next;
68. (*t1)->next->prior=q;
69. (*t1)->next=p;
70. p->prior=(*t1);
71. }
72. main() /*主函数*/
73. {
74. Polyn La,la,Lb,lb;
75. int n,m; /*多项式项数*/
76. printf("enter La’s lenth:");
77. scanf("%d",&n);
78. setPolyn(&La,&la,n);
79. arrange(&La);
80. printf("after arrange La is:\n");
81. print(&La); /*输入多项式La,化简并输出*/
82. printf("enter Lb’s lenth:");
83. scanf("%d",&m);
84. setPolyn(&Lb,&lb,m);
85. arrange(&Lb);
86. printf("after arrange Lb is:\n");
87. print(&Lb); /*输入多项式Lb,化简并输出*/
88. linkPolyn(&La,&Lb); /*La与Lb连接,形成新的La*/
89. arrange(&La); /*化简La*/
90. printf("after add Polyn is :\n");
91. print(&La); /*输出结果*/
92. }

❻ 多项式相加 链表 C语言

#include#includestructnode{
intcoef; //系数
intindex; //指数
structnode*next;
};
//初始化linklist
structnode*init_ll(){
structnode*p;
p=(structnode*)malloc(sizeof(structnode)); //为节点指针分配内存空间
p->next=NULL; //*为节点的next值赋值为NULL方便表的遍历,作为遍历为结尾的判断条件
returnp; //返回指针,便于操作
}
//头插法插入linklist
voidinsert_ll(structnode*head,intcoef,intindex){
structnode*p;
p=(structnode*)malloc(sizeof(structnode)); //为节点指针分配内存空间
p->coef=coef; //实例化系数
p->index=index; //实例化指数
p->next=head->next; //*该节点的next指针指向head指向的节点
head->next=p; //*插入在头指针的后面
}
//打印输出linklist
voidreverse_ll(structnode*head){
structnode*p;
p=head->next;
while(p!=NULL){ //为空时结束循环
printf("%d次项系数为%d",p->index,p->coef); //打印输出
p=p->next; //指针迭代,进行循环输出
}
printf(" ");
}
//销毁linklist释放空间
voiddestory_ll(structnode*head){
structnode*p,*q;
p=head->next;
while(p!=NULL){
q=p;
p=p->next;
free(q);
}
free(head);
}
//两个多项式进行相加操作
voidadd_poly(structnode*head1,intlength1,structnode*head2,intlength2){
structnode*p1;
structnode*p2;
if(length1>length2){
p1=head1->next;
p2=head2->next;
while(p1!=NULL){
if(p1->index==p2->index){
p1->coef=(p1->coef)+(p2->coef);
p2=p2->next;
}
p1=p1->next;
}
}
else{
p1=head1->next;
p2=head2->next;
while(p2!=NULL){
if(p1->index==p2->index){
p2->coef=(p2->coef)+(p1->coef);
p1=p1->next;
}
p2=p2->next;
}
}
}
//两个多项式进行相减操作
voidsub_poly(structnode*head1,intlength1,structnode*head2,intlength2){
structnode*p1;
structnode*p2;
if(length1>length2){
p1=head1->next;
p2=head2->next;
while(p1!=NULL){
if(p1->index==p2->index){
p1->coef=(p1->coef)-(p2->coef);
p2=p2->next;
}
p1=p1->next;
}
}
else{
p1=head1->next;
p2=head2->next;
while(p2!=NULL){
p2->coef=-(p2->coef);
if(p1->index==p2->index){
p2->coef=(p2->coef)+(p1->coef);
p1=p1->next;
}
p2=p2->next;
}
}
}
main(void){
structnode*head1; //多项式1头节点
structnode*head2; //多项式2头节点
intindex_current=0; //要输入的指数
intcoef_current=0; //要输入的系数
intindex_max1; //多项式1次数
intindex_max2; //多项式2次数
int i=0; //循环控制变量
head1=init_ll();
printf("-请输入第一个多项式的次数- ");
scanf_s("%d",&index_max1);
for(i=0;i<index_max1;i++){
printf("-请输入%d次项系数:",index_current);
scanf_s("%d",&coef_current);
insert_ll(head1,coef_current,index_current); //头插法
index_current++;
}
head2=init_ll();
index_current=0;
printf("-请输入第二个多项式的次数- ");
scanf_s("%d",&index_max2);
for(i=0;i<index_max2;i++){
printf("-请输入%d次项系数:",index_current);
scanf_s("%d",&coef_current);
insert_ll(head2,coef_current,index_current); //头插法
index_current++;
}
add_poly(head1,index_max1,head2,index_max2); //进行相加操作
//sub_poly(head1,index_max1,head2,index_max2); //进行相减操作
printf("多项式1的结果为:");
reverse_ll(head1);
printf("多项式2的结果为:");
reverse_ll(head2);
destory_ll(head1);
destory_ll(head2);
getchar();
getchar();
}

❼ C语言 多项式相加

/*为了一元多项式相加能够使用效率更高的算发,应该在多项式的排序操作中加入合并同类项,除非你可以保证输入不含有同类项*/
/*今天就这些吧,明天争取写出来,走了!*/
#include<stdio.h>
#include<stdlib.h>
typedef struct DXS{
float xs;
int zs;
struct DXS *next;
} multinomial;
//输入函数
multinomial *input(void)
{
multinomial *p,*h;
h=NULL;
printf("please input the xs and zs of the DXS:\n");
for(;;)
{
p=(multinomial *)malloc(sizeof(multinomial));
scanf("%f %d",&p->xs,&p->zs);
if(p->xs==0 && p->zs==0){free(p);break;}
p->next=h;
h=p;//从头部插入,没有问题
}
return(h);
}
//将多项式排序

multinomial *inorder(multinomial *h)
{
multinomial *s,*t,*m,*h3;
h3=h->next;//指向第二个结点
h->next=NULL;//拆下第一个结点,为什么?
while(h3!=NULL)
{
s=h3;
h3=h3->next;
t=h;
m=h;
while(s->zs<t->zs&&t!=NULL)
{
m=t;
t=t->next;
}
if(m=t)
{s->next=t;
h=s;
}
else
{
s->next=t;
m->next=s;
}
}
return(h);
}
//两个多项式相加
multinomial *add(multinomial *h1,multinomial *h2)
{
multinomial *u,*v,*w,*j;
w=h2;j=h2;
while(j!=NULL)
{
u=v=h1;w=j;
while(u->zs>w->zs&&u!=NULL)
{v=u;
u=u->next;
}
if(u->zs==w->zs) //指数相同,则系数相加
{
u->xs=u->xs+w->xs;
if(u->xs==0) //如果系数为0,则做删除操作
{
if(u=v) //删除表头
h1=h1->next;
}
else if(u->next==NULL) // 删除表尾
v=NULL;
else //删除表中
{
u=u->next;
}
}

else if(u=v) //待插入点在在表头
{
w->next=u;
h1->next=w;
}
else //待插入点不是在表头
{
w->next=u;
v->next=w;
}

j=j->next;
}
return(h1);
}

void print(multinomial *h1)
{
printf("f(x)=");
while(h1!=NULL)
{

printf("%f",h1->xs);
if(h1->zs!=0)
printf("x^%f",h1->xs);
else printf("%f",h1->xs);
h1=h1->next;
if(h1!=NULL&&h1->xs>0)
printf("+");
}
}

void main()
{multinomial *h1,*h2;
printf("输入第一个多项式的系数和指数(系数与指数之间用空格号隔开)");
h1=input();
printf("输入第二个多项式的系数和指数(系数与指数之间用空格号隔开");
h2=input();
h1=inorder(h1);
h2=inorder(h2);
h1=add(h1,h2);
print(h1);
}

❽ 多项式相加(C语言)

严蔚敏的数据结构上有

❾ C语言写多项式相加怎么写

分都不给。。。算了,以前写了个就给你吧。/*此程序结构比较清晰,用单链表实现了多项式的加法。具体原理可以参考清华大学严蔚敏数据《结构C语言版》第二章注意:此程序假设输入的多项式已经排好序(从低到高)程序在VC6下编译通过*/#includenbsp;amp;lt;stdio.hamp;gt;#includenbsp;amp;lt;stdlib.hamp;gt;#includenbsp;amp;lt;malloc.hamp;gt;#includenbsp;amp;lt;conio.hamp;gt;nbsp;/*getchnbsp;function*/typedefnbsp;structnbsp;NODEnbsp;{nbsp;floatnbsp;nbsp;coef;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;//系数为实数nbsp;intnbsp;nbsp;nbsp;nbsp;expn;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;//指数为整数nbsp;structnbsp;NODEnbsp;*next;nbsp;nbsp;}NODE;NODEnbsp;*Creat(intnbsp;n);voidnbsp;print(NODEnbsp;*head);NODEnbsp;*AddPolyn(NODEnbsp;*head1,nbsp;NODEnbsp;*head2);NODEnbsp;*Delfirst(NODEnbsp;*head,nbsp;NODEnbsp;*q);voidnbsp;InsertBefore(NODEnbsp;*p1,nbsp;NODEnbsp;*p2);intnbsp;nbsp;compare(intnbsp;a,nbsp;intnbsp;b);main(){nbsp;NODEnbsp;*head1,nbsp;*head2,nbsp;*head3;nbsp;intnbsp;n1,nbsp;n2;nbsp;printf(“<br/>请输入第一个多项式的项数nbsp;n1:“);nbsp;scanf(“%d“,nbsp;amp;n1);nbsp;head1nbsp;=nbsp;Creat(n1);nbsp;printf(“<br/>第一个多项式为:<br/>“);nbsp;print(head1);nbsp;nbsp;nbsp;nbsp;nbsp;printf(“<br/>请输入第二个多项式的项数nbsp;n2:“);nbsp;scanf(“%d“,nbsp;amp;n2);nbsp;head2nbsp;=nbsp;Creat(n2);nbsp;printf(“<br/>第二个多项式为:<br/>“);nbsp;print(head2);nbsp;head3nbsp;=nbsp;AddPolyn(head1,nbsp;head2);nbsp;printf(“<br/>相加结果为:<br/>“);nbsp;print(head3);nbsp;printf(“<br/>“);nbsp;getch();}/*创建链表*/NODEnbsp;*Creat(intnbsp;n){nbsp;NODEnbsp;*current,nbsp;*previous,nbsp;*head;nbsp;intnbsp;i;nbsp;headnbsp;=nbsp;(NODEnbsp;*)malloc(sizeof(NODE));nbsp;/*创建头结点*/nbsp;previousnbsp;=nbsp;head;nbsp;nbsp;for(inbsp;=nbsp;0;nbsp;inbsp;amp;lt;nbsp;n;nbsp;i++)nbsp;{nbsp;nbsp;currentnbsp;=nbsp;(NODEnbsp;*)malloc(sizeof(NODE));nbsp;nbsp;nbsp;nbsp;printf(“请输入项数%d的系数和指数nbsp;:nbsp;“,i+1);nbsp;nbsp;scanf(“%f%d“,nbsp;amp;current-amp;gt;coef,nbsp;amp;current-amp;gt;expn);nbsp;nbsp;previous-amp;gt;nextnbsp;=nbsp;current;nbsp;nbsp;previousnbsp;=nbsp;current;nbsp;nbsp;}nbsp;previous-amp;gt;nextnbsp;=nbsp;NULL;nbsp;returnnbsp;head;}/*nbsp;一元多项式的加法nbsp;,总体考虑,可分qa的指数比qb小,或等于pb(如果系数相加等于0和不等于0),或大于pb里面由InsertBefore和Delfirst两个小模块组成一部分*/NODEnbsp;nbsp;*AddPolyn(NODEnbsp;*head1,nbsp;NODEnbsp;*head2){nbsp;NODEnbsp;*ha,nbsp;*hb,nbsp;*qa,nbsp;*qb;nbsp;intnbsp;a,nbsp;b;nbsp;floatnbsp;sum;nbsp;hanbsp;=nbsp;head1;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;/*ha和hb指向头结点*/nbsp;hbnbsp;=nbsp;head2;nbsp;qanbsp;=nbsp;ha-amp;gt;next;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;/*qa和qb指向头结点的下一个结点*/nbsp;qbnbsp;=nbsp;hb-amp;gt;next;nbsp;while(qanbsp;amp;amp;nbsp;qb)nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;/*qa和qb均非空*/nbsp;{nbsp;nbsp;anbsp;=nbsp;qa-amp;gt;expn;nbsp;nbsp;nbsp;bnbsp;=nbsp;qb-amp;gt;expn;nbsp;nbsp;switch(compare(a,nbsp;b))nbsp;{nbsp;nbsp;casenbsp;-1nbsp;:

❿ 用c语言实现多项式相加

#include<stdio.h>
#include<malloc.h>
typedef struct node
{
float xi;
int n;
struct node *next;
}term;
void sort(term *head)
{
term *p,*q,*s;
p=(term *)malloc(sizeof(term));
q=(term *)malloc(sizeof(term));
s=(term *)malloc(sizeof(term));
p=head;

while(p->next!=NULL)
{
s=p->next;
while(s!=NULL)
{
if(p->n>s->n)
{
q->n=p->n;
p->n=s->n;
s->n=q->n;
q->xi=p->xi;
p->xi=s->xi;
s->xi=q->xi;
}
s=s->next;
}
p=p->next;
}
}
term *polynadd(term *head1,term *head2)
{
term *p,*q,*s,*r,*head3;
float x;
p=head1->next;
q=head2->next;
head3=(term *)malloc(sizeof(term));
r=head3;
while(p!=NULL&&q!=NULL)
{
if(p->n==q->n)
{
x=p->xi+q->xi;
if(x!=0)
{
s=(term *)malloc(sizeof(term));
s->xi=x;
s->n=p->n;
r->next=s;
r=s;
}
p=p->next;
q=q->next;
}
else if(p->n>q->n)
{
s=(term *)malloc(sizeof(term));
s->n=q->n;
s->xi=q->xi;
r->next=s;
r=s;
q=q->next;
}
else
{
s=(term *)malloc(sizeof(term));
s->n=p->n;
s->xi=p->xi;
r->next=s;
r=s;
p=p->next;
}
}
while(p!=NULL)
{
s=(term *)malloc(sizeof(term));
s->n=p->n;
s->xi=p->xi;
r->next=s;
r=s;
p=p->next;
}
while(q!=NULL)
{
s=(term *)malloc(sizeof(term));
s->n=q->n;
s->xi=q->xi;
r->next=s;
r=s;
q=q->next;
}
r->next=NULL;
return head3;

}
term *createpolyn(int m)
{

term *p,*head,*q;
int i;
head=(term *)malloc(sizeof(term));
q=head;
for(i=0;i<m;i++)
{
p=(term *)malloc(sizeof(term));
printf("请输入第%d项数的系数和指数\n",i+1);
scanf("%f%d",&p->xi,&p->n);
q->next=p;
q=p;
}
p->next=NULL;
return head;

}
dayin(term *head)
{
term *p,*q;
q=head->next;
if(q->xi==0)
printf(" ");
if(q->n==0&&q->xi>0&&q->xi!=1)
printf("%0.2f",q->xi);
if(q->n==0&&q->xi==1)
printf("1");
if(q->n==0&&q->xi<0)
printf("%0.2f",q->xi);
if(q->n==1&&q->xi==1)
printf("x");
if(q->n==1&&q->xi!=1&&q->xi>0)
printf("%0.2fx",q->xi);
if(q->n==1&&q->xi!=1&&q->xi<0)
printf("%0.2fx",q->xi);
if(q->n!=1&&q->n!=0&&q->xi>0&&q->xi!=1)
printf("%0.2fx^%d",q->xi,q->n);
if(q->n!=1&&q->n!=0&&q->xi<0)
printf("%0.2fx^%d",q->xi,q->n);
if(q->n!=0&&q->n!=1&&q->xi==1)
printf("x^%d",q->n);
p=q->next;
while(p!=NULL)
{
if(p->xi==0)
printf(" ");
if(p->n==0&&p->xi>0&&p->xi!=1)
printf("+%0.2f",p->xi);
if(p->n==0&&p->xi==1)
printf("+1");
if(p->n==0&&p->xi<0)
printf("%0.2f",p->xi);
if(p->n==1&&p->xi==1)
printf("+x");
if(p->n==1&&p->xi!=1&&p->xi>0)
printf("+%0.2fx",p->xi);
if(p->n==1&&p->xi!=1&&p->xi<0)
printf("%0.2fx",p->xi);
if(p->n!=1&&p->n!=0&&p->xi>0&&p->xi!=1)
printf("+%0.2fx^%d",p->xi,p->n);
if(p->n!=1&&p->n!=0&&p->xi<0)
printf("%0.2fx^%d",p->xi,p->n);
if(p->n!=0&&p->n!=1&&p->xi==1)
printf("+x^%d",p->n);
p=p->next;
}
printf("\n");
}
main()
{
int i,j;
term *head1,*head2,*head3;
printf("请输入第一个多项式的项数:\n");
scanf("%d",&i);
head1=createpolyn(i);
sort(head1);
printf("\n");
dayin(head1);
printf("\n");
printf("请输入第二个多项式的项数:\n");
scanf("%d",&j);
head2=createpolyn(j);
sort(head2);
printf("\n");
dayin(head2);
printf("\n\n");
head3=polynadd(head1,head2);
sort(head3);
printf("\n");
dayin(head3);
printf("\n");
}

热点内容
安卓手机锁了怎么开 发布:2025-01-23 17:21:18 浏览:136
经济学算法 发布:2025-01-23 17:13:46 浏览:420
如何和软件联系服务器 发布:2025-01-23 17:13:00 浏览:799
javacrc16算法 发布:2025-01-23 17:11:31 浏览:224
编程加图片 发布:2025-01-23 17:10:33 浏览:566
中国风网站源码 发布:2025-01-23 17:05:56 浏览:679
pythonfilter用法 发布:2025-01-23 17:04:26 浏览:568
java转number 发布:2025-01-23 16:58:11 浏览:476
解压的英语作文 发布:2025-01-23 16:45:05 浏览:969
湖南首选dns服务器地址 发布:2025-01-23 16:06:39 浏览:874