单链表例题 多项式加法乘法
insertafter函数写的不错,加法时分情况讨论,乘法时用双层循环乘并以result数组累加。
代码块
#include <iostream>
#include <cmath>
using namespace std;
struct term
{
float coef;
int exp;
term *link;
term(float c,int e,term *next=NULL)
{
coef=c;
exp=e;
link=next;
}
term *insertafter(float c,int e);
friend ostream& operator <<(ostream&,const term &);
};
class polynomial
{
public:
polynomial()
{
first=new term(0,-1);
}
polynomial(polynomial& R);
int maxorder();
term* gethead() const
{
return first;
}
private:
term *first;
friend ostream& operator <<(ostream &,const polynomial&);
friend istream& operator >>(istream&,polynomial&);
friend polynomial operator +(polynomial &,polynomial&);
friend polynomial operator *(polynomial &,polynomial &);
};
term * term::insertafter(float c,int e)
{
link=new term(c,e);
return link;
}
ostream& operator<<(ostream& out,const term& x)
{
if(x.coef==0.0) return out;
out<<x.coef;
switch (x.exp)
{
case 0:
break;
case 1:
out<<"x";
break;
default:
out<<"x"<<x.exp;
break;
}
return out;
};
polynomial::polynomial(polynomial& R)
{
first=new term(0,-1);
term *destptr=first,*srcptr=R.gethead()->link;
while(srcptr!=NULL)
{
destptr->insertafter(srcptr->coef,srcptr->exp);
srcptr=srcptr->link;
destptr=destptr->link;
}
};
int polynomial::maxorder()
{
term* current=first;
while(current->link!=NULL) current=current->link;
return current->exp;
}
istream& operator>>(istream& in,polynomial& x)
{
term *rear=x.gethead();
int c,e;
while(1)
{
cout<<"input a term(coef,exp)"<<endl;
in>>c>>e;
if(e<0) break;
rear=rear->insertafter(c,e);
}
return in;
}
ostream & operator <<(ostream& out,polynomial& x)
{
term *current=x.gethead()->link;
cout<<"the polynomail is:"<<endl;
bool h=true;
while(current!=NULL)
{
if(h==false&¤t->coef>0.0) out<<"+";
h=false;
out<<*current;
current=current->link;
}
out<<endl;
return out;
};
polynomial operator +(polynomial &A,polynomial&B)
{
term* pa,*pb,*pc,*p;
float temp;
polynomial C;
pc=C.first;
pa=A.gethead()->link;
pb=B.gethead()->link;
while(pa!=NULL&&pb!=NULL)
{
if(pa->exp==pb->exp)
{
temp=pa->coef+pb->coef;
if(fabs(temp)>0.001)
pc=pc->insertafter(temp,pa->exp);
pa=pa->link;
pb=pb->link;
}
else if(pa->exp<pb->exp)
{
pc=pc->insertafter(pa->coef,pa->exp);
pa=pa->link;
}
else
{
pc=pc->insertafter(pb->coef,pb->exp);
pb=pb->link;
}
}
if(pa!=NULL) p=pa;
else p=pb;
while(p!=NULL)
{
pc=pc->insertafter(p->coef,p->exp);
p=p->link;
}
return C;
}
polynomial operator *(polynomial &A,polynomial&B)
{
term* pa,*pb,*pc;
int AL,BL,i,k,maxexp;
polynomial C;
pc=C.gethead();
AL=A.maxorder();
BL=B.maxorder();
if(AL!=-1||BL!=-1)
{
maxexp=AL+BL;
float *result=new float[maxexp+1];
for(i=0; i<=maxexp; i++) result[i]=0.0;
pa=A.gethead()->link;
while(pa!=NULL)
{
pb=B.gethead()->link;
while(pb!=NULL)
{
k=pa->exp+pb->exp;
result[k]+=pa->coef*pb->coef;
pb=pb->link;
}
pa=pa->link;
}
for(int i=0; i<=maxexp; i++)
if(fabs(result[i]>0.001))
pc=pc->insertafter(result[i],i);
delete result;
}
pc->link=NULL;
return C;
}
int main()
{
polynomial pp1,pp2,pp3;
cin>>pp1>>pp2;
pp3=pp1+pp2;
cout<<pp3;
return 0;
}