自己不咋地的多项式加法

typedef struct term{ term(){coef=0.0;expn=0;}; float coef; int expn; }ElemType; typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList; typedef LinkList polynomial; polynomial CreatPolyn(LinkList &p1,int m) { p1 = (LinkList)malloc (sizeof (LNode)); if(p1==NULL)exit(-1); LinkList p,pl; p=p1; p->next = NULL; printf("输入多项式的系数和次数:"); for(int i=0;i<m;i++) { pl=(LinkList)malloc(sizeof(LNode)); if(NULL==p)exit(-1); scanf("%f",&(pl->data.coef)); scanf("%d",&(pl->data.expn)); p->next=pl; pl->next=NULL; p=pl; } return pl; } int cmp(int a,int b) { if(a>b)i=1; if(a<b)i=(-1); if(a==b)i=0; return i; } void ShowPolyn(LinkList &pl) { LinkList p; p=pl->next; while(p->next) { printf("%fx^%d+",p->data.coef,p->data.expn); p=p->next; } printf("%fx^%d",p->data.coef,p->data.expn); printf("/n"); } void AddPolyn (LinkList &pa,LinkList &pb) { polynomial qa; polynomial qb; polynomial pre; polynomial temp; qa=pa->next; qb=pb->next; pre = pa; while((NULL!=qa) && (NULL!=qb)) { int j=cmp(qa->data.expn,qb->data.expn); switch(j) { case -1: { pre=qa; qa=qa->next; }break; case 0: { qa->data.coef = qa->data.coef + qb->data.coef; if(qa->data.coef==0.0) { temp=qa; qa=qa->next; free(temp); temp=qb; qb=qb->next; free(temp); } pre = qa; qa = qa->next; temp=qb; qb=qb->next; free(temp); }break; case 1: { pre->next=qb; pre=qb; qb=qb->next; }break; default: break; } } if(qb!=NULL) { pre->next=qb;//如果是qa=qb,则不可以,好像是因为qa==null, } free(pb); }

你可能感兴趣的:(struct,null,float)