#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct polynode
{
float coef;
int exp;
struct polynode *next;
}polynode,*PLinklist;
PLinklist Create(int n)
{
PLinklist L,p;
int i;
L=(PLinklist)malloc(sizeof(polynode));
L->next=NULL;
for(i=n;i>0;--i)
{
p=(PLinklist)malloc(sizeof(polynode));
scanf("%f %d",&p->coef,&p->exp);
p->next=L->next;
L->next=p;
}
return(L);
}
PLinklist Attach(float co,int ex,PLinklist o)
{
PLinklist c;
c=(PLinklist)malloc(sizeof(polynode));
c->coef=co;
c->exp=ex;
o->next=c;
return c;
}
PLinklist Print(PLinklist L)
{
int i=0;
PLinklist p=L;
while(p->next!=NULL)
{
i++;
printf("%fx^%d ",p->coef,p->exp);
p=p->next;
}
printf("\n");
}
PLinklist Add(PLinklist A,PLinklist B)
{
PLinklist C;
PLinklist o;
PLinklist p=A;
PLinklist q=B;
int sum;
C=(PLinklist)malloc(sizeof(polynode));
o=C;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
sum=p->coef+q->coef;
if(sum!=0)
o=Attach(sum,p->exp,o);
p=p->next;
q=q->next;
}
else if(p->exp<q->exp)
{
o=Attach(q->coef,q->exp,o);
q=q->next;
}
else{
o=Attach(p->coef,p->exp,o);
p=p->next;
}
}
while(p!=NULL)
{
o=Attach(p->coef,p->exp,o);
p=p->next;
}
while(q!=NULL)
{
o=Attach(q->coef,q->exp,o);
q=q->next;
}
o->next=NULL;
p=C;
C=C->next;
return C;
}
void main()
{
int n,m;
PLinklist *A,*B,*C;
printf("Input the length of A :\n");
scanf("%d",&n);
A=Create(n);
Print(A);
printf("Input the length of B :\n");
scanf("%d",&n);
b=Create(n);
Print(b);
C=Add(A,B);
Print(C);
}
这是我写的程序,弄了好久出不来,请高手看看吧
Cè¯è¨ä»£ç ï¼
#include "stdio.h"è¿è¡æµè¯ï¼