数据结构实验:一元多项式的运算
2014-12-04 22:18
141 查看
#include<iostream.h>
class Term
{
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;
}
private:
int coef;
int exp;
Term *link;
friend ostream & operator << (ostream &,const Term &);
friend class Polynominal;
};
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()
{
theList=new Term(0,-1);
theList->link=theList;
}
~Polynominal()
{
Term *p=theList->link;
while(p!=theList)
{
theList->link=p->link;
delete p;
p=theList->link;
}
delete theList;
}
void AddTerms(istream &in)
{
Term *q=theList;
int c,e;
for(;;)
{
cout<<"Input a term(coef,exp):\n"<<endl;
cin>>c>>e;
if(e<0) break;
q=q->InsertAfter(c,e);
}
}
void 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 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
q1=q1->InsertAfter(p->coef,p->exp);
p=p->link;
}
}
private:
Term *theList;
friend ostream & operator << (ostream &,const Polynominal &);
friend istream & operator >> (istream &,Polynominal &);
friend Polynominal & operator + (Polynominal &,Polynominal &);
};
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;
}
void main()
{
Polynominal q,p;
cin>>p;cout<<p;
cin>>q;cout<<q;
q=q+p;
cout<<q;
}
class Term
{
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;
}
private:
int coef;
int exp;
Term *link;
friend ostream & operator << (ostream &,const Term &);
friend class Polynominal;
};
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()
{
theList=new Term(0,-1);
theList->link=theList;
}
~Polynominal()
{
Term *p=theList->link;
while(p!=theList)
{
theList->link=p->link;
delete p;
p=theList->link;
}
delete theList;
}
void AddTerms(istream &in)
{
Term *q=theList;
int c,e;
for(;;)
{
cout<<"Input a term(coef,exp):\n"<<endl;
cin>>c>>e;
if(e<0) break;
q=q->InsertAfter(c,e);
}
}
void 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 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
q1=q1->InsertAfter(p->coef,p->exp);
p=p->link;
}
}
private:
Term *theList;
friend ostream & operator << (ostream &,const Polynominal &);
friend istream & operator >> (istream &,Polynominal &);
friend Polynominal & operator + (Polynominal &,Polynominal &);
};
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;
}
void main()
{
Polynominal q,p;
cin>>p;cout<<p;
cin>>q;cout<<q;
q=q+p;
cout<<q;
}
相关文章推荐
- PAT数据结构_02-线性结构2 一元多项式的乘法与加法运算 (20分)
- 02-线性结构1 一元多项式的乘法与加法运算 (20分)
- 一元多项式的加减乘运算
- 线性表在一元多项式运算中的应用
- 一元多项式的乘法与加法运算
- 02-线性结构2 一元多项式的乘法与加法运算 (20分)
- 一元多项式的表示及加减乘除运算
- 7-2 一元多项式的乘法与加法运算(20 分)
- 实验项目3-4:一元多项式的乘法与加法运算
- 02-线性结构1 一元多项式的乘法与加法运算 -数据结构
- PAT3-04. 一元多项式的乘法与加法运算
- PAT 一元多项式的乘法与加法运算(链表 c++版)
- 一元多项式的运算
- 浙江大学PAT上机题解析之3-04. 一元多项式的乘法与加法运算
- 02-线性结构2 一元多项式的乘法与加法运算-中国大学MOOC-陈越、何钦铭-数据结构-2017秋
- 一元多项式加减乘求导微分简单运算
- PTA 一元多项式的乘法与加法运算
- 02-线性结构1 一元多项式的乘法与加法运算
- 5-2 一元多项式的乘法与加法运算 (20分)
- 线性链表实现一元多项式的加法运算