主题:同上
polypointer cheng(polypointer a,polypointer b)
{
polypointer p,q,d,c,e,f;
polynode temp;
int x,y;
p=a; q=b;
e=&temp;
e->coef=0;
e->exp=0;
e->link=NULL;
c=new polynode;
f=c;
d=c;
while (p!=NULL)
{
q=b;
while(q!=NULL)
{
x=(p->coef)*(q->coef);
y=(p->exp)+(q->exp);
d=attach(x,y,d);
q=q->link;
}
d->link=NULL;
c=c->link;
e=jia(c,e);
p=p->link;
}
delete f;
return e;
}
polypointer chu(polypointer a,polypointer b,polypointer &c,polypointer &d)
{
polypointer p,q,f,g,h,i,j,e;
polypointer m,n,o,z;
int x,y;
p=a; q=b;
c=new polynode; e=new polynode; g=new polynode; i=new polynode;
m=d=c; n=f=e; o=h=g; z=j=i;
while ((p!=NULL)&&(q!=NULL))
{
q=b;
switch(compare(p->exp,q->exp))
{
case '=':
x=(p->coef)/(q->coef);
while (q!=NULL)
{
d=attach((q-> coef)*x,q->exp,d);
q=q->link;
}
d->link=NULL;
c=c->link;
p=jian(p,c);
f=attach(x,0,f);
break;
case '<':
h=attach (p->coef,p->exp,h);
break;
case '>':
x=(p->coef)/(q->coef);
y=(p->exp)-(q->exp);
while (q!=NULL)
{
j=attach((q->coef)*x,(q->exp+y),j);
q=q->link;
}
j->link=NULL;
i=i->link;
p=jian(p,i);
f=attach(x,y,f);
break;
default:
cout<<"error!"<<endl;
}
}
e=e->link;
delete m,n,o,z;
return f;
}
int main()
{
char number;
polypointer a=NULL,b=NULL,c=NULL,d=NULL,e=NULL;
a=input();
b=input();
cout<<"请选择要进行的运算"<<endl;
cout<<" 1,加法"<<endl;
cout<<" 2,减法"<<endl;
cout<<" 3,乘法"<<endl;
cout<<" 4,除法"<<endl;
do
{
cin>>number;
switch (number)
{
case '1': e=jia(a,b); break;
case '2': e=jian(a,b); break;
case '3': e=cheng(a,b); break;
case '4': e=chu(a,b,c,d); break;
default:
cout<<"输入错误,请再输一遍"<<endl;
break;
}
} while(number!='1' && number!='2' && number!='3' && number!='4');
output(e);
return 0;
}
{
polypointer p,q,d,c,e,f;
polynode temp;
int x,y;
p=a; q=b;
e=&temp;
e->coef=0;
e->exp=0;
e->link=NULL;
c=new polynode;
f=c;
d=c;
while (p!=NULL)
{
q=b;
while(q!=NULL)
{
x=(p->coef)*(q->coef);
y=(p->exp)+(q->exp);
d=attach(x,y,d);
q=q->link;
}
d->link=NULL;
c=c->link;
e=jia(c,e);
p=p->link;
}
delete f;
return e;
}
polypointer chu(polypointer a,polypointer b,polypointer &c,polypointer &d)
{
polypointer p,q,f,g,h,i,j,e;
polypointer m,n,o,z;
int x,y;
p=a; q=b;
c=new polynode; e=new polynode; g=new polynode; i=new polynode;
m=d=c; n=f=e; o=h=g; z=j=i;
while ((p!=NULL)&&(q!=NULL))
{
q=b;
switch(compare(p->exp,q->exp))
{
case '=':
x=(p->coef)/(q->coef);
while (q!=NULL)
{
d=attach((q-> coef)*x,q->exp,d);
q=q->link;
}
d->link=NULL;
c=c->link;
p=jian(p,c);
f=attach(x,0,f);
break;
case '<':
h=attach (p->coef,p->exp,h);
break;
case '>':
x=(p->coef)/(q->coef);
y=(p->exp)-(q->exp);
while (q!=NULL)
{
j=attach((q->coef)*x,(q->exp+y),j);
q=q->link;
}
j->link=NULL;
i=i->link;
p=jian(p,i);
f=attach(x,y,f);
break;
default:
cout<<"error!"<<endl;
}
}
e=e->link;
delete m,n,o,z;
return f;
}
int main()
{
char number;
polypointer a=NULL,b=NULL,c=NULL,d=NULL,e=NULL;
a=input();
b=input();
cout<<"请选择要进行的运算"<<endl;
cout<<" 1,加法"<<endl;
cout<<" 2,减法"<<endl;
cout<<" 3,乘法"<<endl;
cout<<" 4,除法"<<endl;
do
{
cin>>number;
switch (number)
{
case '1': e=jia(a,b); break;
case '2': e=jian(a,b); break;
case '3': e=cheng(a,b); break;
case '4': e=chu(a,b,c,d); break;
default:
cout<<"输入错误,请再输一遍"<<endl;
break;
}
} while(number!='1' && number!='2' && number!='3' && number!='4');
output(e);
return 0;
}