# 多项式的加法

```#include "iostream.h"
#include "stdlib.h"
typedef struct trem{
float coef;
int   expf;}trem;
typedef struct dxsjd{
trem  data;
struct dxsjd *next;}dxsjd;
typedef struct dxs{
dxsjd *h;
dxsjd *t;
int  length;
}dxs;
void create(dxs  &a){
int i,j;
dxsjd *p,*q;
cout<<"input number of node:";
cin>>i;
p=(dxsjd *)malloc(sizeof(dxsjd));
p->data.coef=0;
(p->data).expf=-1;
p->next=NULL;
a.h=p;
for(j=1;j<=i;j++)
{q=(dxsjd *)malloc(sizeof(dxsjd));
cin>>(q->data).coef>>(q->data).expf;
cout<<endl;
p->next=q; p=q;}
a.length=i;a.t=q;q->next=NULL;
}
void dxsjf(dxs &a, dxs b)
{dxsjd *ha,*hb,*pa,*pb,*p; trem m,n;
ha=a.h; hb=b.h;
pa=ha->next;  pb=hb->next;
while(pa&&pb)
{m=pa->data;  n=pb->data;
if(m.expf<n.expf){ha=pa;pa=pa->next;}
if(m.expf==n.expf)
{
float sum=m.coef+n.coef;
if(sum!=0.0){pa->data.coef=sum;ha=pa;
pa=pa->next;hb=pb;pb=pb->next;}
}
if(m.expf>n.expf)
{
p=pb;	pb=pb->next;hb->next=p->next;  p->next=ha->next;ha->next=p;
ha=ha->next; }
}//endwhile
if(pa==NULL){ha->next=pb;a.t=b.t;}

a.length=a.length+b.length;
}
void outputt(dxs a){int i;dxsjd *p=a.h->next;
for(i=0;i<a.length;i++) {cout<<p->data.coef<<"x"<<p->data.expf<<"+";p=p->next;}
}
void main()
{dxs a,b;
create(a);
outputt(a);
create(b);
outputt(b);cout<<endl;
dxsjf(a,b);
outputt(a);
}```

相关文章
﻿

﻿