c++ 多项式类用链表的实现
7月4号前要交了!!!定义一个多项式类CPolynominal,多项式的每一项用链表的结点表示,每项包含一个系数和一个指数,例如:3x^2的指数为2,系数为3。开发一个完...
7月4号前要交了!!!
定义一个多项式类CPolynominal,多项式的每一项用链表的结点表示,每项包含一个系数和一个指数,例如:3x^2的指数为2,系数为3。开发一个完整的多项式类,包括构造函数、析构函数以及get()函数(读取值)和set()函数(设置值)。
该类还提供下述重载的运算符:
(1)重载加法运算符+,将两个多项式相加。
(2)重载减法运算符-,将两个多项式相减。
(3)重载赋值运算符=,将一个多项式赋给另一个多项式。
(4)重载乘法运算符*,将两个多项式相乘。
(5)重载复合赋值运算符:+=、-=、*=。
【要求】
1. 完成重载减法运算符-的函数;
2. 完成求多项式f(x)的值的成员函数;
3. 完成插入运算符“<<”的重载;
4. 在主函数中完成相应的测试。
参考程序如下,请按上述要求修改、完善之。
我这有%90的代码 只需要大神添加几个函数就OK了!!
我的QQ :403177553
请尽快吧 因为7月4号就要交了 我还得修改的。。。。 展开
定义一个多项式类CPolynominal,多项式的每一项用链表的结点表示,每项包含一个系数和一个指数,例如:3x^2的指数为2,系数为3。开发一个完整的多项式类,包括构造函数、析构函数以及get()函数(读取值)和set()函数(设置值)。
该类还提供下述重载的运算符:
(1)重载加法运算符+,将两个多项式相加。
(2)重载减法运算符-,将两个多项式相减。
(3)重载赋值运算符=,将一个多项式赋给另一个多项式。
(4)重载乘法运算符*,将两个多项式相乘。
(5)重载复合赋值运算符:+=、-=、*=。
【要求】
1. 完成重载减法运算符-的函数;
2. 完成求多项式f(x)的值的成员函数;
3. 完成插入运算符“<<”的重载;
4. 在主函数中完成相应的测试。
参考程序如下,请按上述要求修改、完善之。
我这有%90的代码 只需要大神添加几个函数就OK了!!
我的QQ :403177553
请尽快吧 因为7月4号就要交了 我还得修改的。。。。 展开
2个回答
展开全部
#include
<iostream>
#include
<cstdlib>
#include
<cmath>
using
namespace
std;
struct
Node//链表结点
{
int
exp;//指数
double
coef;//系数
Node
*next;
};
class
CPolynominal
{
public:
CPolynominal(){
head=0;
}
CPolynominal(const
CPolynominal
&);//复制构造函数
~CPolynominal()
{
Node
*q,*p;
p=head;
while
(p)//删除链表
{
q=p->next;
if
(p)
delete
p;
p=q;
}
head=NULL;
}
Node*
get(int
e);//读取指数为e的项,返回其指针
void
set(double
c,int
e);//设置指数为e的项的系数为c
void
Create(double
*c,int
*e,int
n);//创建多项式
friend
CPolynominal
operator
+(CPolynominal
a,CPolynominal
b);//重载+运算符
friend
CPolynominal
operator
-(CPolynominal
a,CPolynominal
b);//重载-运算符
friend
CPolynominal
operator
*(CPolynominal
a,CPolynominal
b);/旅启/重载*运算符
friend
ostream&
operator<<(ostream&
os,
const
CPolynominal&
a);
CPolynominal&
operator
=(CPolynominal
a);//重载=运算符
CPolynominal&
operator
+=(CPolynominal
a);//重载+=运算符
void
operator
-=(CPolynominal
a);//重载-=运算符
void
operator
*=(CPolynominal
a);//重载*=运算符
void
Print(ostream&
os
=
cout)
const;
void
Copy(const
CPolynominal
&a);
double
Val(double
x);
//求多行败项式f(x)的值
void
Addr(){cout<<"链表头指针值为:"<<head<<endl;}
private:
Node
*head;//链表头指针
};
double
CPolynominal::Val(double
x)
{
Node
*p
=
head;
double
sum
=
0.0;
while
(p)
{
sum
+=
p->coef
*
pow(x,
p->exp);
p
=
p->next;
}
return
sum;
}
void
CPolynominal::Copy(const
CPolynominal
&a)
{
//复制对象(公用函数)
Node
*t,*s,*p;
p=a.head;s=head=0;
while(p)
{
t=new
Node;
if
(head==0)
head=t;
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
if(s)
s->next=t;
s=t;
}
if
(s)
s->next=0;
}
CPolynominal::CPolynominal(const
CPolynominal&
a)//复制构造函数
{
Copy(a);
}
void
CPolynominal::Create(double
*c,int
*e,int
n)//创档镇颤建多项式
{
if
(n<1)
cout<<"错误:要创建多项式的项数必须大于零。\n";
else
{
head=new
Node;//创建头结点
head->coef=c[0];
head->exp=e[0];
Node
*q,*p=head;
for
(int
i=1;i<n;i++)
{
q=new
Node;
q->coef=c[i];
q->exp=e[i];
p->next=q;
p=q;
}
p->next=0;
}
}
Node*
CPolynominal::get(int
e)//读取指数为e的项
{
Node
*p=head;
while
(p)
{
if
(p->exp==e)
return
p;
p=p->next;
}
return
p;//没有找到,返回空指针
}
void
CPolynominal::set(double
c,int
e)
{//将指数为e系数为c的项加入多项式(原多项式无指数为e的项)
Node
*q,*p,*t;
t=new
Node;
t->coef=c;
t->exp=e;
if
(head==0)
{
head=t;
head->next=0;
return;
}
if
(e>head->exp)
{
t->next=head;
head=t;//插入头部
return;
}
p=head;
while
(p
&&
e<p->exp)
{
q=p;
p=p->next;
}
if
(p)
{
//新的项插入p所指结点前
t->next=p;
q->next=t;
}
else//插入尾部
{
q->next=t;
t->next=0;
}
}
CPolynominal
operator
+(CPolynominal
a,CPolynominal
b)//重载+运算符
{
CPolynominal
temp;
Node
*p,*q,*s,*t;
double
x;
s=new
Node;temp.head=s;//先增加一个头结点
p=a.head;
q=b.head;
while
(p
&&
q)
{
if
(p->exp==q->exp)
{
x=p->coef+q->coef;
if
(x!=0.0)
{
t=new
Node;
t->exp=p->exp;
t->coef=x;
s->next=t;
s=t;
}
p=p->next;
q=q->next;
}
else
{
t=new
Node;
if(p->exp>q->exp)
{
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
}
else
{
t->coef=q->coef;
t->exp=q->exp;
q=q->next;
}
s->next=t;
s=t;
}
}
if
(q)
p=q;//p恒指向余下的项
while
(p)
{
t=new
Node;
t->coef=p->coef;
t->exp=p->exp;
s->next=t;
s=t;
p=p->next;
}
s->next=0;
//链表尾标记
s=temp.head;
temp.head=s->next;
delete
s;
//删除多余的头结点
return
temp;
}
CPolynominal
operator
-(CPolynominal
a,CPolynominal
b)//重载-运算符
{
CPolynominal
temp;
Node
*p,*q,*s=0,*t;
//在此处添加代码,完成此函数(模仿上述重载"+"的函数)
double
x;
s=new
Node;temp.head=s;//先增加一个头结点
p=a.head;
q=b.head;
while
(p
&&
q)
{
if
(p->exp==q->exp)
{
x=p->coef-q->coef;
if
(x!=0.0)
{
t=new
Node;
t->exp=p->exp;
t->coef=x;
s->next=t;
s=t;
}
p=p->next;
q=q->next;
}
else
{
t=new
Node;
if(p->exp>q->exp)
{
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
}
else
{
t->coef=-q->coef;
t->exp=q->exp;
q=q->next;
}
s->next=t;
s=t;
}
}
if
(q)
p=q;//p恒指向余下的项
while
(p)
{
t=new
Node;
t->coef=p->coef;
t->exp=p->exp;
s->next=t;
s=t;
p=p->next;
}
s->next=0;
//链表尾标记
s=temp.head;
temp.head=s->next;
delete
s;
//删除多余的头结点
return
temp;
}
CPolynominal
operator
*(CPolynominal
a,CPolynominal
b)//重载*运算符
{
CPolynominal
temp;
Node
*p,*q,*s;
int
e;double
c;
p=a.head;
while
(p)
{
q=b.head;
while
(q)
{
c=p->coef*q->coef;
e=p->exp+q->exp;
s=temp.get(e);//查temp中有无指数为e的项
if
(s)//temp中无指数为e的项
s->coef+=c;
else//temp中无指数为e的项
temp.set(c,e);
q=q->next;
}
p=p->next;
}
return
temp;
}
ostream&
operator<<(ostream&
os,
const
CPolynominal&
a)
{
a.Print(os);
return
os;
}
CPolynominal&
CPolynominal::operator
=(CPolynominal
a)//重载=运算符
{
Node
*s,*p;
if
(head)
{
//若原多项式存在,先撤消它
p=head;
while
(p)
{
s=p->next;
delete
p;
p=s;
}
}
Copy(a);
return
*this;
}
CPolynominal&
CPolynominal::operator
+=(CPolynominal
a)//重载+=运算符
{
*this
=
operator
+(*this,a);
}
void
CPolynominal::operator
-=(CPolynominal
a)//重载-=运算符
{
*this
=
operator
-(*this,a);
}
void
CPolynominal::operator
*=(CPolynominal
a)//重载*=运算符
{
*this
=
operator
*(*this,a);
}
void
CPolynominal::Print(ostream&
os)
const//显示多项式
{
Node*
p;int
e;
if
(head)
{
e=head->exp;
os<<"f(x)="<<head->coef;
if
(e>1)
os<<"x^"<<e;
if
(e==1)
os<<"x";
}
else
{
os
<<
"f(x)=0"
<<
endl;
return;
}
p=head->next;
while
(p)
{
e=p->exp;
if
(p->coef!=0.0)//系数非零
{
if
(p->coef>0.0)
os<<'+'<<p->coef;
else
os<<p->coef;
if
(e>1)
os<<"x^"<<e;
if
(e==1)
os<<"x";
}
p=p->next;
}
os<<endl;
}
int
main()
{
int
e1[]={6,5,4,3,1};
double
c1[]={2.5,3,-5,1.6,-2};
int
e2[]={6,3,2,1,0};
double
c2[]={-2.5,6,2.8,-1,8};
CPolynominal
p1,p2,p3;
p1.Create(c1,e1,5);//创建多项式p1
p2.Create(c2,e2,5);//创建多项式p2
cout<<"多项式p1为";p1.Print();//显示多项式p1
cout<<"多项式p2为";p2.Print();//显示多项式p2
p3
=
p1
+
p2;
cout<<"多项式p1+p2为"<<
p3;//显示多项式p3(p1+p2)
p3
=
p1
-
p2;
cout<<"多项式p1-p2为"<<
p3;//显示多项式p3(p1-p2)
p3
=
p1*p2;
cout<<"多项式p1*p2为"<<p3;//显示多项式p3(p1*p2)
p1
+=
p2;
cout<<"执行p1+=p2后,多项式p1为"
<<
p1;
p1
-=
p2;
cout<<"执行p1-=p2后,多项式p1为"
<<
p1;
p1*=p2;
cout<<"执行p1*=p2后,多项式p1为"
<<
p1;
cout
<<
"p2(1)
=
"
<<
p2.Val(1)
<<
endl;
system("pause");
return
0;
}
<iostream>
#include
<cstdlib>
#include
<cmath>
using
namespace
std;
struct
Node//链表结点
{
int
exp;//指数
double
coef;//系数
Node
*next;
};
class
CPolynominal
{
public:
CPolynominal(){
head=0;
}
CPolynominal(const
CPolynominal
&);//复制构造函数
~CPolynominal()
{
Node
*q,*p;
p=head;
while
(p)//删除链表
{
q=p->next;
if
(p)
delete
p;
p=q;
}
head=NULL;
}
Node*
get(int
e);//读取指数为e的项,返回其指针
void
set(double
c,int
e);//设置指数为e的项的系数为c
void
Create(double
*c,int
*e,int
n);//创建多项式
friend
CPolynominal
operator
+(CPolynominal
a,CPolynominal
b);//重载+运算符
friend
CPolynominal
operator
-(CPolynominal
a,CPolynominal
b);//重载-运算符
friend
CPolynominal
operator
*(CPolynominal
a,CPolynominal
b);/旅启/重载*运算符
friend
ostream&
operator<<(ostream&
os,
const
CPolynominal&
a);
CPolynominal&
operator
=(CPolynominal
a);//重载=运算符
CPolynominal&
operator
+=(CPolynominal
a);//重载+=运算符
void
operator
-=(CPolynominal
a);//重载-=运算符
void
operator
*=(CPolynominal
a);//重载*=运算符
void
Print(ostream&
os
=
cout)
const;
void
Copy(const
CPolynominal
&a);
double
Val(double
x);
//求多行败项式f(x)的值
void
Addr(){cout<<"链表头指针值为:"<<head<<endl;}
private:
Node
*head;//链表头指针
};
double
CPolynominal::Val(double
x)
{
Node
*p
=
head;
double
sum
=
0.0;
while
(p)
{
sum
+=
p->coef
*
pow(x,
p->exp);
p
=
p->next;
}
return
sum;
}
void
CPolynominal::Copy(const
CPolynominal
&a)
{
//复制对象(公用函数)
Node
*t,*s,*p;
p=a.head;s=head=0;
while(p)
{
t=new
Node;
if
(head==0)
head=t;
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
if(s)
s->next=t;
s=t;
}
if
(s)
s->next=0;
}
CPolynominal::CPolynominal(const
CPolynominal&
a)//复制构造函数
{
Copy(a);
}
void
CPolynominal::Create(double
*c,int
*e,int
n)//创档镇颤建多项式
{
if
(n<1)
cout<<"错误:要创建多项式的项数必须大于零。\n";
else
{
head=new
Node;//创建头结点
head->coef=c[0];
head->exp=e[0];
Node
*q,*p=head;
for
(int
i=1;i<n;i++)
{
q=new
Node;
q->coef=c[i];
q->exp=e[i];
p->next=q;
p=q;
}
p->next=0;
}
}
Node*
CPolynominal::get(int
e)//读取指数为e的项
{
Node
*p=head;
while
(p)
{
if
(p->exp==e)
return
p;
p=p->next;
}
return
p;//没有找到,返回空指针
}
void
CPolynominal::set(double
c,int
e)
{//将指数为e系数为c的项加入多项式(原多项式无指数为e的项)
Node
*q,*p,*t;
t=new
Node;
t->coef=c;
t->exp=e;
if
(head==0)
{
head=t;
head->next=0;
return;
}
if
(e>head->exp)
{
t->next=head;
head=t;//插入头部
return;
}
p=head;
while
(p
&&
e<p->exp)
{
q=p;
p=p->next;
}
if
(p)
{
//新的项插入p所指结点前
t->next=p;
q->next=t;
}
else//插入尾部
{
q->next=t;
t->next=0;
}
}
CPolynominal
operator
+(CPolynominal
a,CPolynominal
b)//重载+运算符
{
CPolynominal
temp;
Node
*p,*q,*s,*t;
double
x;
s=new
Node;temp.head=s;//先增加一个头结点
p=a.head;
q=b.head;
while
(p
&&
q)
{
if
(p->exp==q->exp)
{
x=p->coef+q->coef;
if
(x!=0.0)
{
t=new
Node;
t->exp=p->exp;
t->coef=x;
s->next=t;
s=t;
}
p=p->next;
q=q->next;
}
else
{
t=new
Node;
if(p->exp>q->exp)
{
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
}
else
{
t->coef=q->coef;
t->exp=q->exp;
q=q->next;
}
s->next=t;
s=t;
}
}
if
(q)
p=q;//p恒指向余下的项
while
(p)
{
t=new
Node;
t->coef=p->coef;
t->exp=p->exp;
s->next=t;
s=t;
p=p->next;
}
s->next=0;
//链表尾标记
s=temp.head;
temp.head=s->next;
delete
s;
//删除多余的头结点
return
temp;
}
CPolynominal
operator
-(CPolynominal
a,CPolynominal
b)//重载-运算符
{
CPolynominal
temp;
Node
*p,*q,*s=0,*t;
//在此处添加代码,完成此函数(模仿上述重载"+"的函数)
double
x;
s=new
Node;temp.head=s;//先增加一个头结点
p=a.head;
q=b.head;
while
(p
&&
q)
{
if
(p->exp==q->exp)
{
x=p->coef-q->coef;
if
(x!=0.0)
{
t=new
Node;
t->exp=p->exp;
t->coef=x;
s->next=t;
s=t;
}
p=p->next;
q=q->next;
}
else
{
t=new
Node;
if(p->exp>q->exp)
{
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
}
else
{
t->coef=-q->coef;
t->exp=q->exp;
q=q->next;
}
s->next=t;
s=t;
}
}
if
(q)
p=q;//p恒指向余下的项
while
(p)
{
t=new
Node;
t->coef=p->coef;
t->exp=p->exp;
s->next=t;
s=t;
p=p->next;
}
s->next=0;
//链表尾标记
s=temp.head;
temp.head=s->next;
delete
s;
//删除多余的头结点
return
temp;
}
CPolynominal
operator
*(CPolynominal
a,CPolynominal
b)//重载*运算符
{
CPolynominal
temp;
Node
*p,*q,*s;
int
e;double
c;
p=a.head;
while
(p)
{
q=b.head;
while
(q)
{
c=p->coef*q->coef;
e=p->exp+q->exp;
s=temp.get(e);//查temp中有无指数为e的项
if
(s)//temp中无指数为e的项
s->coef+=c;
else//temp中无指数为e的项
temp.set(c,e);
q=q->next;
}
p=p->next;
}
return
temp;
}
ostream&
operator<<(ostream&
os,
const
CPolynominal&
a)
{
a.Print(os);
return
os;
}
CPolynominal&
CPolynominal::operator
=(CPolynominal
a)//重载=运算符
{
Node
*s,*p;
if
(head)
{
//若原多项式存在,先撤消它
p=head;
while
(p)
{
s=p->next;
delete
p;
p=s;
}
}
Copy(a);
return
*this;
}
CPolynominal&
CPolynominal::operator
+=(CPolynominal
a)//重载+=运算符
{
*this
=
operator
+(*this,a);
}
void
CPolynominal::operator
-=(CPolynominal
a)//重载-=运算符
{
*this
=
operator
-(*this,a);
}
void
CPolynominal::operator
*=(CPolynominal
a)//重载*=运算符
{
*this
=
operator
*(*this,a);
}
void
CPolynominal::Print(ostream&
os)
const//显示多项式
{
Node*
p;int
e;
if
(head)
{
e=head->exp;
os<<"f(x)="<<head->coef;
if
(e>1)
os<<"x^"<<e;
if
(e==1)
os<<"x";
}
else
{
os
<<
"f(x)=0"
<<
endl;
return;
}
p=head->next;
while
(p)
{
e=p->exp;
if
(p->coef!=0.0)//系数非零
{
if
(p->coef>0.0)
os<<'+'<<p->coef;
else
os<<p->coef;
if
(e>1)
os<<"x^"<<e;
if
(e==1)
os<<"x";
}
p=p->next;
}
os<<endl;
}
int
main()
{
int
e1[]={6,5,4,3,1};
double
c1[]={2.5,3,-5,1.6,-2};
int
e2[]={6,3,2,1,0};
double
c2[]={-2.5,6,2.8,-1,8};
CPolynominal
p1,p2,p3;
p1.Create(c1,e1,5);//创建多项式p1
p2.Create(c2,e2,5);//创建多项式p2
cout<<"多项式p1为";p1.Print();//显示多项式p1
cout<<"多项式p2为";p2.Print();//显示多项式p2
p3
=
p1
+
p2;
cout<<"多项式p1+p2为"<<
p3;//显示多项式p3(p1+p2)
p3
=
p1
-
p2;
cout<<"多项式p1-p2为"<<
p3;//显示多项式p3(p1-p2)
p3
=
p1*p2;
cout<<"多项式p1*p2为"<<p3;//显示多项式p3(p1*p2)
p1
+=
p2;
cout<<"执行p1+=p2后,多项式p1为"
<<
p1;
p1
-=
p2;
cout<<"执行p1-=p2后,多项式p1为"
<<
p1;
p1*=p2;
cout<<"执行p1*=p2后,多项式p1为"
<<
p1;
cout
<<
"p2(1)
=
"
<<
p2.Val(1)
<<
endl;
system("pause");
return
0;
}
展开全部
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
struct Node//链表源陆结点
{
int exp;//指数
double coef;//系数
Node *next;
};
class CPolynominal
{
public:
CPolynominal(){ head=0; }
CPolynominal(const CPolynominal &);//复制构造函数
~CPolynominal()
{
Node *q,*p;
p=head;
while (p)//删除链表
{
q=p->next;
if (p) delete p;
p=q;
}
head=NULL;
}
Node* get(int e);//读取指数为e的项,返回其指针
void set(double c,int e);//设置指数为e的项的系数为c
void Create(double *c,int *e,int n);//创建多项式
friend CPolynominal operator +(CPolynominal a,CPolynominal b);//重载+运算符
friend CPolynominal operator -(CPolynominal a,CPolynominal b);//重载-运算符
friend CPolynominal operator *(CPolynominal a,CPolynominal b);//重载*运算符
friend ostream& operator<<(ostream& os, const CPolynominal& a);
CPolynominal& operator =(CPolynominal a);//重载=运算符
CPolynominal& operator +=(CPolynominal a);//重激好载+=运算符
void operator -=(CPolynominal a);//重载-=运算符
void operator *=(CPolynominal a);//重明裂铅载*=运算符
void Print(ostream& os = cout) const;
void Copy(const CPolynominal &a);
double Val(double x); //求多项式f(x)的值
void Addr(){cout<<"链表头指针值为:"<<head<<endl;}
private:
Node *head;//链表头指针
};
double CPolynominal::Val(double x)
{
Node *p = head;
double sum = 0.0;
while (p)
{
sum += p->coef * pow(x, p->exp);
p = p->next;
}
return sum;
}
void CPolynominal::Copy(const CPolynominal &a)
{ //复制对象(公用函数)
Node *t,*s,*p;
p=a.head;s=head=0;
while(p)
{
t=new Node;
if (head==0) head=t;
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
if(s) s->next=t;
s=t;
}
if (s) s->next=0;
}
CPolynominal::CPolynominal(const CPolynominal& a)//复制构造函数
{
Copy(a);
}
void CPolynominal::Create(double *c,int *e,int n)//创建多项式
{
if (n<1)
cout<<"错误:要创建多项式的项数必须大于零。\n";
else
{
head=new Node;//创建头结点
head->coef=c[0];
head->exp=e[0];
Node *q,*p=head;
for (int i=1;i<n;i++)
{
q=new Node;
q->coef=c[i];
q->exp=e[i];
p->next=q;
p=q;
}
p->next=0;
}
}
Node* CPolynominal::get(int e)//读取指数为e的项
{
Node *p=head;
while (p)
{
if (p->exp==e) return p;
p=p->next;
}
return p;//没有找到,返回空指针
}
void CPolynominal::set(double c,int e)
{//将指数为e系数为c的项加入多项式(原多项式无指数为e的项)
Node *q,*p,*t;
t=new Node;
t->coef=c;
t->exp=e;
if (head==0)
{
head=t;
head->next=0;
return;
}
if (e>head->exp)
{
t->next=head;
head=t;//插入头部
return;
}
p=head;
while (p && e<p->exp)
{
q=p;
p=p->next;
}
if (p)
{
//新的项插入p所指结点前
t->next=p;
q->next=t;
}
else//插入尾部
{
q->next=t;
t->next=0;
}
}
CPolynominal operator +(CPolynominal a,CPolynominal b)//重载+运算符
{
CPolynominal temp;
Node *p,*q,*s,*t;
double x;
s=new Node;temp.head=s;//先增加一个头结点
p=a.head;
q=b.head;
while (p && q)
{
if (p->exp==q->exp)
{
x=p->coef+q->coef;
if (x!=0.0)
{
t=new Node;
t->exp=p->exp;
t->coef=x;
s->next=t;
s=t;
}
p=p->next;
q=q->next;
}
else
{
t=new Node;
if(p->exp>q->exp)
{
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
}
else
{
t->coef=q->coef;
t->exp=q->exp;
q=q->next;
}
s->next=t;
s=t;
}
}
if (q) p=q;//p恒指向余下的项
while (p)
{
t=new Node;
t->coef=p->coef;
t->exp=p->exp;
s->next=t;
s=t;
p=p->next;
}
s->next=0; //链表尾标记
s=temp.head;
temp.head=s->next;
delete s; //删除多余的头结点
return temp;
}
CPolynominal operator -(CPolynominal a,CPolynominal b)//重载-运算符
{
CPolynominal temp;
Node *p,*q,*s=0,*t;
//在此处添加代码,完成此函数(模仿上述重载"+"的函数)
double x;
s=new Node;temp.head=s;//先增加一个头结点
p=a.head;
q=b.head;
while (p && q)
{
if (p->exp==q->exp)
{
x=p->coef-q->coef;
if (x!=0.0)
{
t=new Node;
t->exp=p->exp;
t->coef=x;
s->next=t;
s=t;
}
p=p->next;
q=q->next;
}
else
{
t=new Node;
if(p->exp>q->exp)
{
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
}
else
{
t->coef=-q->coef;
t->exp=q->exp;
q=q->next;
}
s->next=t;
s=t;
}
}
if (q) p=q;//p恒指向余下的项
while (p)
{
t=new Node;
t->coef=p->coef;
t->exp=p->exp;
s->next=t;
s=t;
p=p->next;
}
s->next=0; //链表尾标记
s=temp.head;
temp.head=s->next;
delete s; //删除多余的头结点
return temp;
}
CPolynominal operator *(CPolynominal a,CPolynominal b)//重载*运算符
{
CPolynominal temp;
Node *p,*q,*s;
int e;double c;
p=a.head;
while (p)
{
q=b.head;
while (q)
{
c=p->coef*q->coef;
e=p->exp+q->exp;
s=temp.get(e);//查temp中有无指数为e的项
if (s)//temp中无指数为e的项
s->coef+=c;
else//temp中无指数为e的项
temp.set(c,e);
q=q->next;
}
p=p->next;
}
return temp;
}
ostream& operator<<(ostream& os, const CPolynominal& a)
{
a.Print(os);
return os;
}
CPolynominal& CPolynominal::operator =(CPolynominal a)//重载=运算符
{
Node *s,*p;
if (head)
{ //若原多项式存在,先撤消它
p=head;
while (p)
{
s=p->next;
delete p;
p=s;
}
}
Copy(a);
return *this;
}
CPolynominal& CPolynominal::operator +=(CPolynominal a)//重载+=运算符
{
*this = operator +(*this,a);
}
void CPolynominal::operator -=(CPolynominal a)//重载-=运算符
{
*this = operator -(*this,a);
}
void CPolynominal::operator *=(CPolynominal a)//重载*=运算符
{
*this = operator *(*this,a);
}
void CPolynominal::Print(ostream& os) const//显示多项式
{
Node* p;int e;
if (head)
{
e=head->exp;
os<<"f(x)="<<head->coef;
if (e>1) os<<"x^"<<e;
if (e==1) os<<"x";
}
else
{
os << "f(x)=0" << endl;
return;
}
p=head->next;
while (p)
{
e=p->exp;
if (p->coef!=0.0)//系数非零
{
if (p->coef>0.0) os<<'+'<<p->coef;
else os<<p->coef;
if (e>1) os<<"x^"<<e;
if (e==1) os<<"x";
}
p=p->next;
}
os<<endl;
}
int main()
{
int e1[]={6,5,4,3,1};
double c1[]={2.5,3,-5,1.6,-2};
int e2[]={6,3,2,1,0};
double c2[]={-2.5,6,2.8,-1,8};
CPolynominal p1,p2,p3;
p1.Create(c1,e1,5);//创建多项式p1
p2.Create(c2,e2,5);//创建多项式p2
cout<<"多项式p1为";p1.Print();//显示多项式p1
cout<<"多项式p2为";p2.Print();//显示多项式p2
p3 = p1 + p2;
cout<<"多项式p1+p2为"<< p3;//显示多项式p3(p1+p2)
p3 = p1 - p2;
cout<<"多项式p1-p2为"<< p3;//显示多项式p3(p1-p2)
p3 = p1*p2;
cout<<"多项式p1*p2为"<<p3;//显示多项式p3(p1*p2)
p1 += p2;
cout<<"执行p1+=p2后,多项式p1为" << p1;
p1 -= p2;
cout<<"执行p1-=p2后,多项式p1为" << p1;
p1*=p2;
cout<<"执行p1*=p2后,多项式p1为" << p1;
cout << "p2(1) = " << p2.Val(1) << endl;
system("pause");
return 0;
}
#include <cstdlib>
#include <cmath>
using namespace std;
struct Node//链表源陆结点
{
int exp;//指数
double coef;//系数
Node *next;
};
class CPolynominal
{
public:
CPolynominal(){ head=0; }
CPolynominal(const CPolynominal &);//复制构造函数
~CPolynominal()
{
Node *q,*p;
p=head;
while (p)//删除链表
{
q=p->next;
if (p) delete p;
p=q;
}
head=NULL;
}
Node* get(int e);//读取指数为e的项,返回其指针
void set(double c,int e);//设置指数为e的项的系数为c
void Create(double *c,int *e,int n);//创建多项式
friend CPolynominal operator +(CPolynominal a,CPolynominal b);//重载+运算符
friend CPolynominal operator -(CPolynominal a,CPolynominal b);//重载-运算符
friend CPolynominal operator *(CPolynominal a,CPolynominal b);//重载*运算符
friend ostream& operator<<(ostream& os, const CPolynominal& a);
CPolynominal& operator =(CPolynominal a);//重载=运算符
CPolynominal& operator +=(CPolynominal a);//重激好载+=运算符
void operator -=(CPolynominal a);//重载-=运算符
void operator *=(CPolynominal a);//重明裂铅载*=运算符
void Print(ostream& os = cout) const;
void Copy(const CPolynominal &a);
double Val(double x); //求多项式f(x)的值
void Addr(){cout<<"链表头指针值为:"<<head<<endl;}
private:
Node *head;//链表头指针
};
double CPolynominal::Val(double x)
{
Node *p = head;
double sum = 0.0;
while (p)
{
sum += p->coef * pow(x, p->exp);
p = p->next;
}
return sum;
}
void CPolynominal::Copy(const CPolynominal &a)
{ //复制对象(公用函数)
Node *t,*s,*p;
p=a.head;s=head=0;
while(p)
{
t=new Node;
if (head==0) head=t;
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
if(s) s->next=t;
s=t;
}
if (s) s->next=0;
}
CPolynominal::CPolynominal(const CPolynominal& a)//复制构造函数
{
Copy(a);
}
void CPolynominal::Create(double *c,int *e,int n)//创建多项式
{
if (n<1)
cout<<"错误:要创建多项式的项数必须大于零。\n";
else
{
head=new Node;//创建头结点
head->coef=c[0];
head->exp=e[0];
Node *q,*p=head;
for (int i=1;i<n;i++)
{
q=new Node;
q->coef=c[i];
q->exp=e[i];
p->next=q;
p=q;
}
p->next=0;
}
}
Node* CPolynominal::get(int e)//读取指数为e的项
{
Node *p=head;
while (p)
{
if (p->exp==e) return p;
p=p->next;
}
return p;//没有找到,返回空指针
}
void CPolynominal::set(double c,int e)
{//将指数为e系数为c的项加入多项式(原多项式无指数为e的项)
Node *q,*p,*t;
t=new Node;
t->coef=c;
t->exp=e;
if (head==0)
{
head=t;
head->next=0;
return;
}
if (e>head->exp)
{
t->next=head;
head=t;//插入头部
return;
}
p=head;
while (p && e<p->exp)
{
q=p;
p=p->next;
}
if (p)
{
//新的项插入p所指结点前
t->next=p;
q->next=t;
}
else//插入尾部
{
q->next=t;
t->next=0;
}
}
CPolynominal operator +(CPolynominal a,CPolynominal b)//重载+运算符
{
CPolynominal temp;
Node *p,*q,*s,*t;
double x;
s=new Node;temp.head=s;//先增加一个头结点
p=a.head;
q=b.head;
while (p && q)
{
if (p->exp==q->exp)
{
x=p->coef+q->coef;
if (x!=0.0)
{
t=new Node;
t->exp=p->exp;
t->coef=x;
s->next=t;
s=t;
}
p=p->next;
q=q->next;
}
else
{
t=new Node;
if(p->exp>q->exp)
{
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
}
else
{
t->coef=q->coef;
t->exp=q->exp;
q=q->next;
}
s->next=t;
s=t;
}
}
if (q) p=q;//p恒指向余下的项
while (p)
{
t=new Node;
t->coef=p->coef;
t->exp=p->exp;
s->next=t;
s=t;
p=p->next;
}
s->next=0; //链表尾标记
s=temp.head;
temp.head=s->next;
delete s; //删除多余的头结点
return temp;
}
CPolynominal operator -(CPolynominal a,CPolynominal b)//重载-运算符
{
CPolynominal temp;
Node *p,*q,*s=0,*t;
//在此处添加代码,完成此函数(模仿上述重载"+"的函数)
double x;
s=new Node;temp.head=s;//先增加一个头结点
p=a.head;
q=b.head;
while (p && q)
{
if (p->exp==q->exp)
{
x=p->coef-q->coef;
if (x!=0.0)
{
t=new Node;
t->exp=p->exp;
t->coef=x;
s->next=t;
s=t;
}
p=p->next;
q=q->next;
}
else
{
t=new Node;
if(p->exp>q->exp)
{
t->coef=p->coef;
t->exp=p->exp;
p=p->next;
}
else
{
t->coef=-q->coef;
t->exp=q->exp;
q=q->next;
}
s->next=t;
s=t;
}
}
if (q) p=q;//p恒指向余下的项
while (p)
{
t=new Node;
t->coef=p->coef;
t->exp=p->exp;
s->next=t;
s=t;
p=p->next;
}
s->next=0; //链表尾标记
s=temp.head;
temp.head=s->next;
delete s; //删除多余的头结点
return temp;
}
CPolynominal operator *(CPolynominal a,CPolynominal b)//重载*运算符
{
CPolynominal temp;
Node *p,*q,*s;
int e;double c;
p=a.head;
while (p)
{
q=b.head;
while (q)
{
c=p->coef*q->coef;
e=p->exp+q->exp;
s=temp.get(e);//查temp中有无指数为e的项
if (s)//temp中无指数为e的项
s->coef+=c;
else//temp中无指数为e的项
temp.set(c,e);
q=q->next;
}
p=p->next;
}
return temp;
}
ostream& operator<<(ostream& os, const CPolynominal& a)
{
a.Print(os);
return os;
}
CPolynominal& CPolynominal::operator =(CPolynominal a)//重载=运算符
{
Node *s,*p;
if (head)
{ //若原多项式存在,先撤消它
p=head;
while (p)
{
s=p->next;
delete p;
p=s;
}
}
Copy(a);
return *this;
}
CPolynominal& CPolynominal::operator +=(CPolynominal a)//重载+=运算符
{
*this = operator +(*this,a);
}
void CPolynominal::operator -=(CPolynominal a)//重载-=运算符
{
*this = operator -(*this,a);
}
void CPolynominal::operator *=(CPolynominal a)//重载*=运算符
{
*this = operator *(*this,a);
}
void CPolynominal::Print(ostream& os) const//显示多项式
{
Node* p;int e;
if (head)
{
e=head->exp;
os<<"f(x)="<<head->coef;
if (e>1) os<<"x^"<<e;
if (e==1) os<<"x";
}
else
{
os << "f(x)=0" << endl;
return;
}
p=head->next;
while (p)
{
e=p->exp;
if (p->coef!=0.0)//系数非零
{
if (p->coef>0.0) os<<'+'<<p->coef;
else os<<p->coef;
if (e>1) os<<"x^"<<e;
if (e==1) os<<"x";
}
p=p->next;
}
os<<endl;
}
int main()
{
int e1[]={6,5,4,3,1};
double c1[]={2.5,3,-5,1.6,-2};
int e2[]={6,3,2,1,0};
double c2[]={-2.5,6,2.8,-1,8};
CPolynominal p1,p2,p3;
p1.Create(c1,e1,5);//创建多项式p1
p2.Create(c2,e2,5);//创建多项式p2
cout<<"多项式p1为";p1.Print();//显示多项式p1
cout<<"多项式p2为";p2.Print();//显示多项式p2
p3 = p1 + p2;
cout<<"多项式p1+p2为"<< p3;//显示多项式p3(p1+p2)
p3 = p1 - p2;
cout<<"多项式p1-p2为"<< p3;//显示多项式p3(p1-p2)
p3 = p1*p2;
cout<<"多项式p1*p2为"<<p3;//显示多项式p3(p1*p2)
p1 += p2;
cout<<"执行p1+=p2后,多项式p1为" << p1;
p1 -= p2;
cout<<"执行p1-=p2后,多项式p1为" << p1;
p1*=p2;
cout<<"执行p1*=p2后,多项式p1为" << p1;
cout << "p2(1) = " << p2.Val(1) << endl;
system("pause");
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询