#includeusingnamespacestd;structnode{intzhi;intxi;node*next;};voidinput(node*p);node*sum(node*p,node*u);node*mult(node*p,node*u);voidprint(node*p);intmain(){str...
#include
using namespace std;
struct node{
int zhi;
int xi;
node *next;
};
void input(node *p);
node* sum(node *p,node *u);
node* mult(node *p,node *u);
void print(node *p);
int main(){
struct node *a=new node;
input(a);
struct node *b=new node;
input(b);
print(sum(a,b));
//mult(a,b);
print(mult(a,b));
return 0;
}
void input(node *p)
{
p->next=NULL;
node *u=new node;
while(cin>>u->xi>>u->zhi)
{
if(u->xi==0)break;
p->next=u;
u->next=NULL;
p=u;
u=new node;
}
}
node* sum(node *p,node *u)
{
node *a,*b,*l;
node *h=new node;
h->next=NULL;
l=h;
a=p->next;b=u->next;
while(a!=NULL&&b!=NULL)
{
if(a->zhi>b->zhi)
{
node *m=new node;
m->zhi=a->zhi;
m->xi=a->xi;
m->next=NULL;
l->next=m;
l=m;
a=a->next;
}
else if(a->zhizhi)
{
node *m=new node;
m->zhi=b->zhi;
m->xi=b->xi;
m->next=NULL;
l->next=m;
l=m;
b=b->next;
}
else
{
if(a->xi+b->xi!=0)
{
node *m=new node;
m->xi=a->xi+b->xi;
m->zhi=a->zhi;
m->next=NULL;
l->next=m;
l=m;
b=b->next;
a=a->next;
}
else
{
b=b->next;
a=a->next;
}
}
}
if(a==NULL&&b!=NULL){
while(b!=NULL)
{
node *m=new node;
m->xi=b->xi;
m->zhi=b->zhi;
m->next=NULL;
l->next=m;
l=m;
b=b->next;
}
}
else if(a!=NULL&&b==NULL){
while(a!=NULL)
{
node *m=new node;
m->xi=a->xi;
m->zhi=a->zhi;
m->next=NULL;
l->next=m;
l=m;
a=a->next;
}
}
return h;
}
node* mult(node *p,node *u)
{
int i=0;
node *a=p->next,*b=u->next,*d;
node *t=new node;
t->next=NULL;
d=t;
node *y=new node;
node *r=new node;
r->zhi=0;
r->xi=0;
r->next=NULL;
y->next=r;
while(a!=NULL)
{
b=u->next;
d=t;
while(b!=NULL)
{
node *c=new node;
c->zhi=a->zhi+b->zhi;
c->xi=a->xi*b->xi;
c->next=NULL;
d->next=c;
d=c;
b=b->next;
}
//print(t);
y=sum(y,t);//ÕâÀï³ö´í
//print(sum(g,t));
a=a->next;
}
return y;
}
void print(node *p)
{
node *u;
for(u=p->next;u!=NULL;u=u->next)
{
if(u->xi!=1&&u->xi!=-1&&u->xi!=0)cout<xi;
else if(u->xi==-1)cout<<"-";
else if(u->zhi==0&&u->xi!=0)cout<xi;
if(u->zhi>1)cout<<"x^"<zhi;
else if(u->zhi==1)cout<<"x";
if(u->next!=NULL&&u->next->xi>0)cout<<"+";
}
cout<
}
展开