主题:哪位好心人,帮忙调试一下..
#include<iostream.h>
struct Term{
float coef;//系数
int exp;//指数
struct Term *next;
Term(){};
Term(float c,int e,Term *p):coef(c),exp(e){next=p;}
Term*InsertAfter(float c,int e);//返回指针类型函数
};//结点类
Term* Term::InsertAfter(float c,int e)
{
next=new Term(c,e,next);//构造新项结点<c,e>,插在当前结点及其后继结点之间
return next; //函数返回新结点地址
}
ostream& operator<<(ostream& out, Term&A)
{
if(A.coef==0)return out;
switch(A.exp){
case 0:out<<A.coef;break;
case 1:if(A.coef!=1)out<<A.coef;
out<<"X";break;
default:if(A.coef!=1)out<<A.coef;
out<<"X^"<<A.exp;break;
}
return out;
}
//----------------------------------------------------------------------------------------
class Polynominal
{
public:
Polynominal();
~Polynominal();
void AddTerms(istream& in);//添加多项式项,即构造一个多项式,按降幂次序输入
void Output(ostream& out);//输出多项式
Polynominal& PolyAdd(const Polynominal& x,const Polynominal& y);//加法函数
Polynominal& PolySub(const Polynominal& x,const Polynominal& y);//减法函数
Polynominal& PolyMul(const Polynominal& x,const Polynominal& y);//乘法函数
//void operator=(const Polynominal &x);
int comp(int x,int y);
void Clear();
private:
struct Term *head;
friend ostream& operator<<(ostream&,Polynominal&);
friend istream& operator>>(istream&,Polynominal&);
};//多项式类
Polynominal::Polynominal()//构造函数,构造一个表头结点,形成一个只有表头结点的空链表
{
head=new struct Term;
head->next=NULL;
}
Polynominal::~Polynominal()//析构函数,释放多项式各项结点和表头结点
{
Clear();
delete head;
}
void Polynominal::Clear()//释放多项式各项结点,但保留头结点,得空多项式
{
Term *q=head,*r;
for(r=q;r!=NULL;r=q->next)
q->next=r->next;
delete r;
}
//..................
void Polynominal::AddTerms(istream& in)
{
Term *q=head;
float c;
int e;
for(;;){
cout<<"Input a term(coef,exp):"<<endl;
in>>c>>e;//输入系数c和指数e
if(e<0)break;//指数为负,退出
else q=q->InsertAfter(c,e);//构造新项结点<c,e>,插入*q之后,q指向新结点
}
}
void Polynominal::Output(ostream& out)//输出多项式
{
Term *q=head;
out<<"the poltnominal is :"<<endl;
q=q->next;
out<<*q; //第一个结点前不加加号
for(q=q->next;q!=NULL;q=q->next){
if(q->coef>0)out<<"+";
out<<*q;
}
out<<endl;
}
Polynominal& Polynominal::PolyAdd(const Polynominal& x,const Polynominal& y)//多项式加法
{
Clear();
Term *p=x.head->next;
Term *q=y.head->next;
Term *r=head;
while(p->exp>=0||q->exp>=0){
switch(comp(p->exp,q->exp)){
case -1:
r=r->InsertAfter(q->coef,q->exp);
q=q->next;break;
case 0:
if(p->coef+q->coef!=0)r=r->InsertAfter(p->coef+q->coef,p->exp);
p=p->next;q=q->next;
break;
case 1:
r=r->InsertAfter(p->coef,p->exp);
p=p->next;break;
}
}
return *this;//...........
}
Polynominal& Polynominal::PolyMul(const Polynominal& x,const Polynominal& y)//乘法
{
Polynominal s,t;
Clear();
Term *p,*q=y.head->next;
for(;q->next!=NULL;q=q->next){//将乘数的每一个项与被乘数多项式相成
s=x; //复制被乘数多项式
for(p=s.head->next;p->next!=NULL;p=p->next){ //s=x*(*q)
p->coef=p->coef*q->coef;
p->exp=p->exp+q->exp;
}
t=PolyAdd(s,t); //将多项式的每一个与被乘数多项式相乘的结果累加
}
return *this; //*this为乘积多项式
}
Polynominal& Polynominal::PolySub(const Polynominal& x,const Polynominal& y)
{
Clear();
Term *p=x.head->next;
Term *q=y.head->next;
Term *r=head;
while(p->exp>=0||q->exp>=0){
switch(comp(p->exp,q->exp)){
case 1:
r=r->InsertAfter(p->coef,p->exp);p=p->next;break;
case -1:
r=r->InsertAfter(-(q->coef),q->exp);q=q->next;break;
case 0:
/*if(p->coef+q->coef!=0){
r=r->InsertAfter(p->coef-q->coef,p->exp);
p=p->next;q=q->next;
}
if(p->coef+q->coef==0)p=p->next;q=q->next;*/
if(p->coef+q->coef!=0)r=r->InsertAfter(p->coef-q->coef,p->exp);
p=p->next;q=q->next;
break;
}
}
return *this;//...........
}
int Polynominal::comp(int x,int y){
if(x==y)return 0;
else if(x>y)return 1;
else return -1;
}
ostream& operator<<(ostream&out,Polynominal &x)
{
x.Output(out);
return out;
}
istream& operator>>(istream &in,Polynominal &x)
{
x.AddTerms(in);
return in;
}
void main()
{
Polynominal p,q,r;
cin>>p;cout<<p<<endl;
cin>>q;cout<<q<<endl;
cout<<r.PolyAdd(p,q);
cout<<r.PolyMul(p,q);
cout<<r.PolySub(p,q);
}
这是我修改后的程序,可是还是有错,输完两个多项式后,它就出来一个框说:什么什么遇到问题需关闭,这是怎么回事??是不是加法乘法程序有问题??
帮忙调试一下.谢谢啦...[em8]
struct Term{
float coef;//系数
int exp;//指数
struct Term *next;
Term(){};
Term(float c,int e,Term *p):coef(c),exp(e){next=p;}
Term*InsertAfter(float c,int e);//返回指针类型函数
};//结点类
Term* Term::InsertAfter(float c,int e)
{
next=new Term(c,e,next);//构造新项结点<c,e>,插在当前结点及其后继结点之间
return next; //函数返回新结点地址
}
ostream& operator<<(ostream& out, Term&A)
{
if(A.coef==0)return out;
switch(A.exp){
case 0:out<<A.coef;break;
case 1:if(A.coef!=1)out<<A.coef;
out<<"X";break;
default:if(A.coef!=1)out<<A.coef;
out<<"X^"<<A.exp;break;
}
return out;
}
//----------------------------------------------------------------------------------------
class Polynominal
{
public:
Polynominal();
~Polynominal();
void AddTerms(istream& in);//添加多项式项,即构造一个多项式,按降幂次序输入
void Output(ostream& out);//输出多项式
Polynominal& PolyAdd(const Polynominal& x,const Polynominal& y);//加法函数
Polynominal& PolySub(const Polynominal& x,const Polynominal& y);//减法函数
Polynominal& PolyMul(const Polynominal& x,const Polynominal& y);//乘法函数
//void operator=(const Polynominal &x);
int comp(int x,int y);
void Clear();
private:
struct Term *head;
friend ostream& operator<<(ostream&,Polynominal&);
friend istream& operator>>(istream&,Polynominal&);
};//多项式类
Polynominal::Polynominal()//构造函数,构造一个表头结点,形成一个只有表头结点的空链表
{
head=new struct Term;
head->next=NULL;
}
Polynominal::~Polynominal()//析构函数,释放多项式各项结点和表头结点
{
Clear();
delete head;
}
void Polynominal::Clear()//释放多项式各项结点,但保留头结点,得空多项式
{
Term *q=head,*r;
for(r=q;r!=NULL;r=q->next)
q->next=r->next;
delete r;
}
//..................
void Polynominal::AddTerms(istream& in)
{
Term *q=head;
float c;
int e;
for(;;){
cout<<"Input a term(coef,exp):"<<endl;
in>>c>>e;//输入系数c和指数e
if(e<0)break;//指数为负,退出
else q=q->InsertAfter(c,e);//构造新项结点<c,e>,插入*q之后,q指向新结点
}
}
void Polynominal::Output(ostream& out)//输出多项式
{
Term *q=head;
out<<"the poltnominal is :"<<endl;
q=q->next;
out<<*q; //第一个结点前不加加号
for(q=q->next;q!=NULL;q=q->next){
if(q->coef>0)out<<"+";
out<<*q;
}
out<<endl;
}
Polynominal& Polynominal::PolyAdd(const Polynominal& x,const Polynominal& y)//多项式加法
{
Clear();
Term *p=x.head->next;
Term *q=y.head->next;
Term *r=head;
while(p->exp>=0||q->exp>=0){
switch(comp(p->exp,q->exp)){
case -1:
r=r->InsertAfter(q->coef,q->exp);
q=q->next;break;
case 0:
if(p->coef+q->coef!=0)r=r->InsertAfter(p->coef+q->coef,p->exp);
p=p->next;q=q->next;
break;
case 1:
r=r->InsertAfter(p->coef,p->exp);
p=p->next;break;
}
}
return *this;//...........
}
Polynominal& Polynominal::PolyMul(const Polynominal& x,const Polynominal& y)//乘法
{
Polynominal s,t;
Clear();
Term *p,*q=y.head->next;
for(;q->next!=NULL;q=q->next){//将乘数的每一个项与被乘数多项式相成
s=x; //复制被乘数多项式
for(p=s.head->next;p->next!=NULL;p=p->next){ //s=x*(*q)
p->coef=p->coef*q->coef;
p->exp=p->exp+q->exp;
}
t=PolyAdd(s,t); //将多项式的每一个与被乘数多项式相乘的结果累加
}
return *this; //*this为乘积多项式
}
Polynominal& Polynominal::PolySub(const Polynominal& x,const Polynominal& y)
{
Clear();
Term *p=x.head->next;
Term *q=y.head->next;
Term *r=head;
while(p->exp>=0||q->exp>=0){
switch(comp(p->exp,q->exp)){
case 1:
r=r->InsertAfter(p->coef,p->exp);p=p->next;break;
case -1:
r=r->InsertAfter(-(q->coef),q->exp);q=q->next;break;
case 0:
/*if(p->coef+q->coef!=0){
r=r->InsertAfter(p->coef-q->coef,p->exp);
p=p->next;q=q->next;
}
if(p->coef+q->coef==0)p=p->next;q=q->next;*/
if(p->coef+q->coef!=0)r=r->InsertAfter(p->coef-q->coef,p->exp);
p=p->next;q=q->next;
break;
}
}
return *this;//...........
}
int Polynominal::comp(int x,int y){
if(x==y)return 0;
else if(x>y)return 1;
else return -1;
}
ostream& operator<<(ostream&out,Polynominal &x)
{
x.Output(out);
return out;
}
istream& operator>>(istream &in,Polynominal &x)
{
x.AddTerms(in);
return in;
}
void main()
{
Polynominal p,q,r;
cin>>p;cout<<p<<endl;
cin>>q;cout<<q<<endl;
cout<<r.PolyAdd(p,q);
cout<<r.PolyMul(p,q);
cout<<r.PolySub(p,q);
}
这是我修改后的程序,可是还是有错,输完两个多项式后,它就出来一个框说:什么什么遇到问题需关闭,这是怎么回事??是不是加法乘法程序有问题??
帮忙调试一下.谢谢啦...[em8]