#2
林月儿2021-03-28 09:23
程序代码: #include<stdio.h> #include<malloc.h> typedef struct Polynode { int coef; int exp; struct Polynode *next; }Polynode,*Polylist; Polylist PolyCreate(); void polyadd(Polylist polya,Polylist polyb); void output(Polylist poly); Polylist PolyCreate(int w) { Polynode *head,*rear,*s; int c=w,e=2; head=(Polynode *)malloc(sizeof(Polynode)); rear=head; printf("输入系数与指数,系数为零时结束\n"); //scanf("%d,%d",&c,&e); while(c>0) { s=(Polynode *)malloc(sizeof(Polynode)); s->coef=c; s->exp=e; rear->next=s; rear=s; //scanf("%d,%d",&c,&e); c=c-2; } rear->next=NULL; return(head); } void output(Polylist poly) { Polynode *p; //p=(Polynode *)malloc(sizeof(Polynode)); p=poly->next; while(p!=NULL) { printf("%d,%d\n",p->coef,p->exp); if(p->exp=0) { if(p->coef>0) printf("+%d",p->coef); else printf("%d",p->coef); } if(p->exp>0) { if(p->coef>0) printf("+%d*x^%d",p->coef,p->exp); else printf("%d*x^%d",p->coef,p->exp); } p=p->next; } } void polyadd(Polylist polya,Polylist polyb) { Polynode *p,*q,*tail,*temp; int sum; p=polya->next; q=polyb->next; tail=polya; while(p!=NULL&&q!=NULL) { if(p->exp<q->exp) { tail->next=p; tail=p; p=p->next; } else if(p->exp==q->exp) { sum=p->coef+q->coef; if(sum!=0) { p->coef=sum; tail->next=p; tail=p; p=p->next; temp=q; q=q->next; free(temp); } else { temp=p; p=p->next; free(temp); temp=q; q=q->next; free(temp); } } else { tail->next=q; tail=q; q=q->next; } } if(p!=NULL) tail->next=p; else tail->next=q; } int main() { Polynode *a,*b; a=PolyCreate(5); output(a); b=PolyCreate(6); output(b); polyadd(a,b); printf("\n"); output(a); return (0); } |
#include<stdio.h>
#include<malloc.h>
typedef struct Polynode
{
int coef;
int exp;
struct Polynode *next;
}Polynode,*Polylist;
Polylist PolyCreate();
void polyadd(Polylist polya,Polylist polyb);
void output(Polylist poly);
Polylist PolyCreate()
{
Polynode *head,*rear,*s;
int c,e;
head=(Polynode *)malloc(sizeof(Polynode));
rear=head;
printf("输入系数与指数,系数为零时结束\n");
scanf("%d,%d",&c,&e);
while(c!=0)
{
s=(Polynode *)malloc(sizeof(Polynode));
s->coef=c;
s->exp=e;
rear->next=s;
rear=s;
scanf("%d,%d",&c,&e);
}
rear->next=NULL;
return(head);
}
void output(Polylist poly)
{
Polynode *p;
p=(Polynode *)malloc(sizeof(Polynode));
p=poly->next;
while(p!=NULL)
{
if(p->exp=0)
{
if(p->coef>0)
printf("+%d",p->coef);
else
printf("%d",p->coef);
}
if(p->exp>0)
{
if(p->coef>0)
printf("+%d*x^%d",p->coef,p->exp);
else
printf("%d*x^%d",p->coef,p->exp);
}
p=p->next;
}
}
void polyadd(Polylist polya,Polylist polyb)
{
Polynode *p,*q,*tail,*temp;
int sum;
p=polya->next;
q=polyb->next;
tail=polya;
while(p!=NULL&&q!=NULL)
{
if(p->exp<q->exp)
{
tail->next=p;
tail=p;
p=p->next;
}
else if(p->exp==q->exp)
{
sum=p->coef+q->coef;
if(sum!=0)
{
p->coef=sum;
tail->next=p;
tail=p;
p=p->next;
temp=q;
q=q->next;
free(temp);
}
else
{
temp=p;
p=p->next;
free(temp);
temp=q;
q=q->next;
free(temp);
}
}
else
{
tail->next=q;
tail=q;
q=q->next;
}
}
if(p!=NULL)
tail->next=p;
else
tail->next=q;
}
int main()
{
Polynode *a,*b;
a=PolyCreate();
output(a);
b=PolyCreate();
output(b);
polyadd(a,b);
output(a);
return (0);
}