求救:在C++中用链表实现多项式的加,减,乘
那位高手来帮帮忙,小弟要做一个如题的程序,但遇到不少困难,特别是乘法,不知道怎么实现小弟现在做的代码如下:#include<iostream.h>classterm{pu...
那位高手来帮帮忙,小弟要做一个如题的程序,但遇到不少困难,特别是乘法,不知道怎么实现小弟现在做的代码如下:
#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;
else if(val.coef==1&&val.exp!=0) out<<""; //如果系数为1,就不显示系数
else
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 polysub(polynominal& r);
void polymul(polynominal& r);
private:
term* thelist;
friend ostream & operator <<(ostream &,const polynominal &);
friend istream & operator >>(istream&,polynominal &);
friend polynominal &operator +(polynominal &,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 c,e;
for(;;){
cout<<"请输入一个多项式(coef,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<<"输出多项式为:\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;
p=p->link;
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;
}
}
}
void polynominal::polysub(polynominal & r) //减法
{term* q,*q1=thelist,*p;
p=r.thelist->link;
q=q1->link;
while(p->exp>=0)
{ 展开
#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;
else if(val.coef==1&&val.exp!=0) out<<""; //如果系数为1,就不显示系数
else
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 polysub(polynominal& r);
void polymul(polynominal& r);
private:
term* thelist;
friend ostream & operator <<(ostream &,const polynominal &);
friend istream & operator >>(istream&,polynominal &);
friend polynominal &operator +(polynominal &,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 c,e;
for(;;){
cout<<"请输入一个多项式(coef,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<<"输出多项式为:\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;
p=p->link;
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;
}
}
}
void polynominal::polysub(polynominal & r) //减法
{term* q,*q1=thelist,*p;
p=r.thelist->link;
q=q1->link;
while(p->exp>=0)
{ 展开
2个回答
展开全部
补充上面的程序....
while(p->exp<q->exp)
{
q1=q;q=q->link;
}
if(p->exp==q->exp)
{
q->coef=q->coef-(p->coef);
p=p->link; ///////////////////////////////
if(q->coef==0)
{
q1->link=q->link;delete(q);
q=q1->link;
}
else
{
q1=q;q=q->link;
}
}
else
{p->coef=0-(p->coef); ///////////////////////////////////
q1=q1->insertafter(p->coef,p->exp);
p=p->link;
}
}
}
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&operator -(polynominal &a,polynominal &b)
{a.polysub(b);return a;
}
void main()
{polynominal p,q;
int i,n=0,m=0;
cin>>q;cout<<q;
cin>>p;cout<<p;
th:cout<<"请选择要进行的操作:"<<endl<<"1。运行q+p"<<endl<<"2。运行q-p"<<endl<<"3。运行q*p"<<endl<<"其他:退出"<<endl;
cin>>i;
switch(i){
case 1:if(m){q=q+p+p;m=0;cout<<q;goto th;}
else {q=q+p;cout<<q;n=1;goto th;}
case 2:if(n)
{q=q-p-p;cout<<q;n=0;goto th;}
else {q=q-p;cout<<q;m=1;goto th;}
case 3:cout<<"Byebye~"<<endl;
}
}
主函数里好乱的东西是因为在加减函数里是q=q+p,q的值就发生了变化,开始我想在做运算前就用一个"="重载把q的值赋给另一个变量,无奈重载做不出来...后来想把加减函数里的程序改一下,又觉得麻烦,最后想出来让它连加或连减两次....大侠们帮帮忙,把乘法做出来,上面的那个q值变化的问题也能解决最好啦.
while(p->exp<q->exp)
{
q1=q;q=q->link;
}
if(p->exp==q->exp)
{
q->coef=q->coef-(p->coef);
p=p->link; ///////////////////////////////
if(q->coef==0)
{
q1->link=q->link;delete(q);
q=q1->link;
}
else
{
q1=q;q=q->link;
}
}
else
{p->coef=0-(p->coef); ///////////////////////////////////
q1=q1->insertafter(p->coef,p->exp);
p=p->link;
}
}
}
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&operator -(polynominal &a,polynominal &b)
{a.polysub(b);return a;
}
void main()
{polynominal p,q;
int i,n=0,m=0;
cin>>q;cout<<q;
cin>>p;cout<<p;
th:cout<<"请选择要进行的操作:"<<endl<<"1。运行q+p"<<endl<<"2。运行q-p"<<endl<<"3。运行q*p"<<endl<<"其他:退出"<<endl;
cin>>i;
switch(i){
case 1:if(m){q=q+p+p;m=0;cout<<q;goto th;}
else {q=q+p;cout<<q;n=1;goto th;}
case 2:if(n)
{q=q-p-p;cout<<q;n=0;goto th;}
else {q=q-p;cout<<q;m=1;goto th;}
case 3:cout<<"Byebye~"<<endl;
}
}
主函数里好乱的东西是因为在加减函数里是q=q+p,q的值就发生了变化,开始我想在做运算前就用一个"="重载把q的值赋给另一个变量,无奈重载做不出来...后来想把加减函数里的程序改一下,又觉得麻烦,最后想出来让它连加或连减两次....大侠们帮帮忙,把乘法做出来,上面的那个q值变化的问题也能解决最好啦.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询