| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 679 人关注过本帖
标题:看看这个关于多项式运算程序的错误
只看楼主 加入收藏
lijianbo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2006-3-25
收藏
 问题点数:0 回复次数:0 
看看这个关于多项式运算程序的错误

#include<iostream.h>
struct Term
{
Term(int c,int e):coef(c),exp(e){link=NULL;}
Term(int c,int e,Term* next):coef(c),exp(e){link=next;}
Term * InsertAfter(int c,int e);
int c;
int e;
int coef;
int exp;
Term *link;


};

class Polynominal:public Term
{
public:
Polynominal();
~Polynominal();
int ExpComp(int x,int y);
void AddTerms(istream& in);
void Output(ostream& out)const;
void PolyAdd(const Polynominal& r);
Polynominal& operator+(const Polynominal& x);

void operator=(const Polynominal&x);

private:
Term* theList;
friend ostream& operator<<(ostream& output,const Polynominal&);
friend istream& operator>>(istream&,Polynominal&);

};


Term* Term::InsertAfter(int c,int e)
{
link=new Term(c,e,link);
return link;
}


template<class T>
ostream &operator<<(ostream & out,const Term<T>& val)
{
if(val.coef==0) return out;
switch(val.exp){
case 0:out<<val.coef;break;
case 1:if(val.coef!=1)out<<val.coef;
out<<"X";break;
default:if(val.coef!=1)out<<val.coef;
out<<"X^"<<val.exp;break;
}
return out;
}
Polynominal::Polynominal()
{
theList=new Term(0,-1){link=NULL;};
theList->link=theList;
}

void Polynominal::AddTerms(istream& in)
{
Term* q=theList;
int c,e;
for(;;)
{
cout<<"Input a term(coef,exp):\n"<<endl;
in>>c>>e;
if(e<0) break;
q=q->InsertAfter(c,e);
}
}
void Polynominal::Output(ostream& out)const
{
bool start=true;
Term *p=theList->link;
out<<"The polynominal is:\n"<<endl;
for(;p!=theList;p=p->link)
{
if(!start&&p->coef>0) out<<'+';
start=false;
out<<p->coef,p->exp;
}
out<<endl;
}
int ExpComp(int x,int y)
{
if(x==y) return 0;
else if(x>y) return 1;
else return -1;
}
void Polynominal::PolyAdd(const Polynominal& r)
{
Term* q2=NULL,

*q1=theList,*q=theList->link,
*p=r.theList->link;
while(p->exp >=0){
switch(ExpComp(p->exp,q->exp)){
case -1:q1=q;q=q->link;break;
case 0:q->coef=q->coef+p->coef;
if(q->coef==0){
q2=q;q1->link=q->link;
q=q->link;p=p->link;
delete(q2);
}
else{
q1=q;q=q->link;p=p->link;
}
break;
case 1:
q1=q1->InsertAfter(p->coef,p->exp);
p=p->link;break;
}
}
}
void Polynominal::operator=(const Polynominal& x)
{
if(this==&x) return;
Term *q=theList,*p=x.theList->link,*r=NULL;
for(q=q->link;q!=theList;q=r){
r=q->link;delete q;
}
theList->link=theList;
for(;p!=x.theList;p=p->link)
q=q->InsertAfter(p->coef,p->exp);
}
Polynominal& Polynominal::operator+(const Polynominal& b)
{
PolyAdd(b);
return *this;
}

ostream& operator<<(ostream &out,const Polynominal &x)
{
x.Output(out);
return out;
}
istream& operator>>(istream& in,Polynominal &x)
{
x.AddTerms(in);
return in;
}
void main()
{

Polynominal p,q;
cin>>p;cout<<p;
cin>>q;cout<<q;
q=q+p;cout<<q;
}
大家帮忙看看有什么错阿!!谢谢呐!

搜索更多相关主题的帖子: 多项式 运算 
2006-03-25 10:32
快速回复:看看这个关于多项式运算程序的错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017066 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved