# 多项式求值

#include <iostream>

using namespace std;
#include"Ploy.h"
int main()
{
Polynomial pa,pb;
int i;
//cout << "Hello world!" << endl;
cout<<" 请输入第一个多项式的项数;"<<endl;
cin>>i;
cout<<"请依次输入第一个多项式每一项;"<<endl;
GreatPolyn(pa ,i);
cout<<" 请输入第二个多项式的项数;"<<endl;
cin>>i;
cout<<"请依次输入第二个多项式每一项;"<<endl;
GreatPolyn(pb ,i);
print(pa);
return 0;
}

#ifndef PLOY_H_INCLUDED
#define PLOY_H_INCLUDED
typedef struct PNode{
float coef;
int expn;
struct PNode *next;
}PNode,*Polynomial;

void GreatPolyn(Polynomial &p,int m ){
p=new PNode;
p->next=NULL;
for(int i=0;i<m;i++){
Polynomial s;
s=new PNode;
cin>>s->coef>>s->expn;
Polynomial pre=p;
Polynomial q=p->next;
while(q && q->expn < s->expn){
pre=p;
q=q->next;
}
s->next=q;
pre->next=s;
}
}
Polynomial p1,p2,p3,r;
int sum;
p1=pa->next;
p2=pb->next;
p3=pa;
while(p1 && p2){
if(p1->expn==p2->expn){
sum=p1->coef+p2->coef;
if(sum!=0){
p1->coef=sum;
p3->next=p1;
p3=p1;
p1=p1->next;
r=p2;
p2=p2->next;
delete r;
}
else{
r=p1;
p1=p1->next;
delete r;
r=p2;
p2=p2->next;
delete r;
}
}
else if(p1->expn<p2->expn){
p3->next=p1;
p3=p1;
p1=p1->next;
}
else{
p3->next=p2;
p3=p2;
p2=p2->next;
}
}
p3->next=p1?p1:p2;
delete pb;
}

void print(Polynomial p)
{
p=p->next;
cout<<p->coef<<p->expn;
p=p->next;
while(p){
cout<<'+'<<p->coef<<p->expn;
p=p->next;
}
cout<<endl;
}

#endif // PLOY_H_INCLUDED