资源简介
(数据结构)用链表结构(C++)实现多项式的加法和乘法运算。
代码片段和文件信息
#include
class Term
{
private:
int coef;
int exp;
Term *link;
public:
Term(int cint e);
Term(int cint eTerm *nxt);
Term *InsertAfter(int cint e);
friend ostream & operator<<(ostream &const Term &);
friend class Polynominal;
};
Term::Term(int cint e):coef(c)exp(e)
{
link=0;
}
Term::Term(int cint eTerm *nxt):coef(c)exp(e)
{
link=nxt;
}
Term *Term::InsertAfter(int cint e)
{
link=new Term(celink);
return link;
}
ostream & operator<<(ostream &outconst Term &val)
{
out< switch(val.exp)
{
case 0:break;
case 1:out<<“X“;
break;
default:out<<“X^“< break;
}
return out;
}
class Polynominal
{
private:
Term *theList;
public:
Polynominal();
~Polynominal();
void AddTerms(istream &in);
void Output(ostream &out) const;
void PolyAdd(Polynominal &r);
void PolyMul (Polynominal &r);
friend ostream &operator<<(ostream &const Polynominal &);
friend istream &operator>>(istream &const Polynominal &);
friend Polynominal &operator +(Polynominal &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 ce;
for(;;)
{
cout<<“Input a trem(coefexp):\n“< cin>>c>>e;
if(e<0)
break;
q=q->InsertAfter(ce);
}
}
void Polynominal::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<<“\n“< }
void Polynominal::PolyAdd(Polynominal &r)
{
Term *q*q1=theList*p;
p=r.theList->link;
q=q1->link;
while(p->exp>=0)
{
while(p->expexp)
{
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->coefp->exp);
p=p->link;
}
}
void Polynominal::PolyMul(Polynominal &r)
{
Term *p*q*m*n*s;
int c=0e=0;
p=r.theList->link;
q=theList->link;
m=new Term(0-1);
m->link=m;
n=m;
s=n->link;
while(q->exp>=0)
{
p=r.theList->link;
while(p->exp>=0)
{
c=q->coef*p->coef;
e=q->exp+p->exp;
while(s->exp>=0&&s->exp>e)
{
n=s;
s=s->link;
}
if(s->exp==e)
{
s->coef=s->coef+c;
if(s->coef==0)
{
n->link=s->link;
delete s;
}
}
else
n->InsertAfter(ce);
n=m;
s=n->link;
p=p->link;
}
q
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
I.A.... 213089 2010-03-27 22:48 多项式的算术运算\Debug\Polynominal.exe
I.A.... 19935 2010-03-27 22:48 多项式的算术运算\Debug\Polynominal.obj
I.A.... 549888 2010-03-27 22:48 多项式的算术运算\Debug\Polynominal.pdb
I.A.... 61440 2010-03-27 22:48 多项式的算术运算\Debug\vc60.pdb
I.A.... 3514 2010-03-24 20:33 多项式的算术运算\Polynominal.cpp
I.A.... 3461 2010-03-24 20:14 多项式的算术运算\Polynominal.dsp
I.A.... 547 2010-03-24 20:50 多项式的算术运算\Polynominal.dsw
I.A.... 50176 2010-03-27 22:49 多项式的算术运算\Polynominal.ncb
I.A.... 48640 2010-03-27 22:49 多项式的算术运算\Polynominal.opt
I.A.... 777 2010-03-27 22:49 多项式的算术运算\Polynominal.plg
I..D... 0 2010-08-11 14:30 多项式的算术运算\Debug
I..D... 0 2010-08-11 14:30 多项式的算术运算
----------- --------- ---------- ----- ----
951467 12
- 上一篇:数据库课程设计 校园用电管理系统.rar
- 下一篇:基于动态规划的TSP问题求解源码
评论
共有 条评论