用链表编译一个多项式相加的程序,我弄了好几天了都没弄出来,请高人指点一二,请求程序源代码,谢过!
什么叫多项式相加,可以说得具体一点不?
我的源程序如下:
#include<iostream.h>
class mploy
{
protected:
int coef;
int expn;
mploy *next;
public:
mploy *creat()
{
mploy *p,*r,*s;
p=r=new mploy;
int x,y;
cout<<"输入多项式的系数和指数";
cin>>x>>y;
while(x)
{
s=new mploy;
s->coef=x;
s->expn=y;
r->next=s;
r=s;
cout<<"输入多项式的系数和指数";
cin>>x>>y;
}
r->next=NULL;
return p;
}
void print(mploy *h)
{
mploy *p=h->next;
if(p->coef!=0)
{
if(p->expn!=0)
cout<<p->coef<<"x**"<<p->expn;
else
cout<<p->coef;
}
p=p->next;
while(p!=NULL)
{
if(p->coef>0)
cout<<"+";
cout<<p-coef<<"x**"<<p->expn;
p=p->next;
}
cout<<endl;
}
void add(mploy *A,mploy *B)
{
mploy *p,*q,*u,*pre;
int x;
p=A->next;q=B->next;
while((p!=NULL)&&(q!=NULL))
{
if(p->nxet<q->next)
{
pre=p;p=p->next;
}
else
if(p->nxet==q->next)
{
x=p->coef+q->coef;
if(x!=0)
{
p->coef=x;pre=p;
}
else
{
pre->next=p->next;delete p;
}
p=pre->next;u=q;q=q->next;delete u;
}
else
{
u=q->next;q->next=p;pre->next=q;pre=q;q=u;
}
if(q!=NULL)
{
pre->next=q;
}
}
};
void main()
{
mploy *A,*B,c;
cout<<"建立第一个多项式链表"<<endl;
A=c.creat();
cout<<"建立第二个多项式链表"<<endl;
B=c.creat();
c.print(A);
c.print(B);
c.add(A,B);
c.print(A);
}
可运行老有一错误,请高手指点,急!!!
明早要交的!