回复 2楼 l01w29
#include<iostream.h>
class Term
{
public:
Term(int c,int e);
Term(int c,int e,Term *nxt);
Term* InsertAfter(int c,int e);
private:
int coef;
int exp;
Term *link;
friend ostream &operator<<(ostream &,const Term &);
friend class Polynominal;
};
Term::Term(int c,int e):coef(c),exp(e)
{
link=0;
}
Term::Term(int c,int e,Term *nxt):coef(c),exp(e)
{
link=nxt;
}
Term* Term::InsertAfter(int c,int e)
{
link=new Term(c,e,link);
return link;
}
ostream &operator<<(ostream &out,const Term &val)
{
if(val.coef==0)return out;
out<<val.coef;
switch(val.exp)
{
case 0:break;
case 1:out<<"X";break;
default:out<<"X^"<<val.exp;break;
}
return out;
}
class Polynominal
{
public:
Polynominal();
~Polynominal();
void AddTerms(istream& in);
void Output(ostream& out)const;
void PolyAdd(Polynominal& r);
void PolyMultiply(Polynominal& r);
private:
Term* theList;
friend ostream& operator<<(ostream&,const Polynominal&);
friend istream& operator>>(istream&,Polynominal&);
friend Polynominal& operator +(Polynominal&,Polynominal&);
};
Polynominal::Polynominal()
{
theList=new Term(0,-1);
theList->link=theList;
}
Polynominal::~Polynominal()
{
Term* p=theList->link;
while(p!=theList)
{
theList->link=p->link;
delete p;
p=theList->link;
}
delete theList;
}
void Polynominal::AddTerms(istream &in)
{
Term* q=theList;
int e,c;
for(;;)
{
cout<<"Input a term(ceof,exp):\n"<<endl;
cin>>c>>e;
if(e<0) break;
q=q->InsertAfter(c,e);
}
}
void Polynominal::Output(ostream& out)const
{
int first=1;Term *p=theList->link;
cout<<"The polynominal is:\n"<<endl;
for(;p!=theList;p=p->link)
{
if(!first&&(p->coef>0)) out<<"+";
first=0;
out<<*p;
}
cout<<"\n"<<endl;
}
void Polynominal::PolyAdd(Polynominal& r)
{
Term* q,*q1=theList,*p;
p=r.theList->link;
q=q1->link;
while(p->exp=0)
{
while(p->exp<q->exp)
{
q1=q;
q=q->link;
}
if(p->exp==q->exp)
{
q->coef=q->coef+p->coef;
if(q->coef==0)
{
q1->link=q->link;
delete(q);
q=q1->link;
//
p=p->link;
}
else
{
q1=q;
q=q->link;
//
p=p->link;
}
}
else
q1=q1->InsertAfter(p->coef,p->exp);
p=p->link;
}
}
void Polynominal::PolyMultiply(Polynominal& r)
{
Term *q,*p,*q2,*p1,*p2;
int coef,exp;
p=r.theList->link;
q=theList->link;
p1=new Term(0,-1);
p1->link=p1;
p2=p1;
while(q->exp>=0)
{
p=r.theList->link;
while(p->exp>=0)
{
coef=q->coef*p->coef;
exp=q->exp+p->exp;
for(p2=p1->link;p2->exp>=0&&p2->exp!=exp;p2=p2->link);
if(p2->exp==-1)
p2->InsertAfter(coef,exp);
else
{
p2->coef=p2->coef+coef;
if(p2->coef==0)
{
q2=p2->link;
p2->link=q2->link;
delete(q2);
}
}
p=p->link;
}
q=q->link;
}
theList=p1;
}
ostream& operator<<(ostream &out,const Polynominal &x)
{
x.Output(out);
return out;
}
istream& operator>>(istream& in,Polynominal &x)
{
x.AddTerms(in);
return in;
}
Polynominal& operator+(Polynominal &a,Polynominal &b)
{
a.PolyAdd(b);
cout<<"after add"<<endl;
return a;
}
Polynominal& operator*(Polynominal &a,Polynominal &b)
{
a.PolyMultiply(b);
cout<<"after mul"<<endl;
return a;
}
int main()
{
Polynominal p,q;
cin>>p;cout<<p;
cin>>q;cout<<q;
q=p+q;cout<<q;
q=p*q;cout<<q;
return 0;
}
这是代码,可是执行时加法时而会出错。