这是我学数据结构编的!有多项式的加法与乘法!!
#include <iostream>
using namespace std;
class Term
{
private:
int coef;
int exp;
Term *link;
friend ostream & operator << (ostream & ,const Term &);
friend class Polynominal;
public:
Term(int c,int e):coef(c),exp(e)
{
link=0;
}
Term(int c,int e,Term* nxt):coef(c),exp(e)
{
link=nxt;
}
Term* InsertAfter(int c,int e)
{
link=new Term(c,e,link);
return link;
//return new Term(c,e,this);
}
// 这里是重载的 InsertAfter
/*
Term* InsertAfter(int c,int e, Term* ln)
{
ln=new Term(c,e,ln);
return ln;
}
*/
};
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
{
private:
Term* thelist;
int n;
friend ostream & operator <<(ostream&, const Polynominal &);
friend istream & operator >>(istream&,Polynominal &);
friend Polynominal & operator +(Polynominal &, Polynominal &);
friend Polynominal & operator * (Polynominal &a ,Polynominal &b);
public:
Polynominal()
{
thelist=new Term(0,-1);
thelist->link=thelist;
n=0;
// 你几乎从来没有给 n 其他任何值,而你在乘法函数中
// 却希望它记录下多项式的大小,你在文件中查找 n++ 或 ++n
// 可以看出我在那些地方修改了 n 的值,你还可以用其他方法
// 实现
}
/*~Polynominal()
{
Term* p=thelist->link;
while(p!=thelist)
{
thelist->link=p->link;
delete p;
p=thelist->link;
}
delete thelist;
}*/
//======================================================================
// 合并次数相同的项, 只需在加法中调用
void simplify()
{
Term* target, *iter, *post_iter;
target = thelist->link;
while(target->exp != -1)
{
post_iter = target;
iter = target->link;
while(iter->exp != -1)
{
if(iter->exp == target->exp)
{
target->coef += iter->coef;
post_iter->link = iter->link;
delete iter;
iter = post_iter->link;
}
else
{
iter = iter->link;
post_iter = post_iter->link;
}
}
target = target->link;
}
}
//========================================================================
//
//
void AddTerms(istream& in)
{
Term* q=thelist;
int c,e;
for(;;)
{
cout<<"Input a term(coef,exp):\n";
cin>>c>>e;
if(e<0) break;
q=q->InsertAfter(c,e);
n++;
}
}
void Output(ostream& out) const
{
int first=1;
Term *p=thelist->link;
cout<<"The Polynominal is :\n";
for(;p!=thelist; p=p->link)
{
if(!first&&(p->coef>0)) out<<"+";
first=0;
out<<*p;
}
cout<<endl;
}
void 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;
}
else
{
q1=q;q=q->link;
}
}
else
{
n++;
q1=q1->InsertAfter(p->coef,p->exp);
}
p=p->link;
}
//=======================================================
simplify();
//=======================================================
}
Polynominal& ploymul(Polynominal& sum, Polynominal& r)
{
Term* q,*q1=thelist,*p;
p=r.thelist->link;
q=q1->link;
Polynominal* s;
s=new Polynominal[n];
for(int i=0;i<n;i++)
{
int j=0;
p=r.thelist->link;
//=======================================================
// 在这里添加了一个 temp 变量,最后将 temp->link 的值给 s[i]
// 这样不需要那个重载的 InsertAfter 函数
Term* v,*temp;
temp=v=s[i].thelist->link;
while(j<r.n)
{
// 下面指数应该是相加, 另外我给你重载了一个
// InsertAfter 函数,这是你的问题的根源所在
// 你想一想这两个函数的区别
// 另外 v 的声明及初始化给你移到循环的外面
// 去了
v=v->InsertAfter(q->coef*p->coef,q->exp+p->exp);
// v=v->InsertAfter(q->coef*p->coef,q->exp+p->exp, v);
++s[i].n;
p=p->link;
j++;
}
// 你不该忘记将新创建的多项式记录到 s[i] 中
//=======================================================
s[i].thelist->link = temp->link;
//=======================================================
v = 0;
q=q->link;
}
for(int i=0;i<n;i++)
{
// 这里给你删掉了几行你没有使用的代码
sum+s[i];
}
return sum;
}
};
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);
return a;
}
Polynominal sum;
// 下面这个函数(及ploymul)返回的是一个引用,所以返回类型不能是临时对象,
// 所以我将 sum 的声明从 ploymul 中移出来了(在这两行注释的上面)
Polynominal& operator * ( Polynominal &a ,Polynominal &b)
{
return a.ploymul(sum,b);
}
int main()
{
Polynominal p,q;
cin>>p;
cout<<p;
cin>>q;
cout<<q;
q+p;
cout<<q;
cout<<(p*q);
// 注意这个 p * q 实际上是 p*(p+q)
}