说明,完全照搬殷人昆C++数据结构中的内容,但是不能正确计算及正确输出,需要完善……
====================================
#include <iostream>
#include <math.h>
using namespace std;
struct Term
{
float ceof; //系数
int exp; //指数
Term *link;
Term(float c,int e,Term *next = NULL)
{
ceof = c;
exp = e;
link = next;
}
Term* InsertAfter(float c,int e);
friend ostream& operator<<(ostream&,const Term&);
};
Term* Term::InsertAfter( float c,int e )
{
//在当前由this指针指向的项后面插入一个新项
link = new Term(c,e,link);
return link;
}
ostream& operator<<( ostream& out,const Term& x )
{
//输出一个项到输出流out中
if(x.ceof == 0.0)
return out;
out<<x.ceof; //输出系数
switch(x.exp) //输出指数
{
case 0:
break;
case 1:
out<<"X";
break;
default:
out<<"X^"<<x.exp;
break;
}
return out;
}
class Polynomial
{
public:
Polynomial()
{
first = new Term(0,-1); //建立空链表
}
Polynomial(Polynomial& R);
int MaxOrder(); //计算最大阶数
Term* getHead()const //取得多项式单链表的表头指针
{
return first;
}
private:
Term *first;
friend ostream& operator <<(ostream& out,Polynomial& x);
friend istream& operator >>(istream& in,Polynomial& x);
friend Polynomial operator +(Polynomial& A,Polynomial& B);
friend Polynomial operator *(Polynomial& A,Polynomial& B);
};
ostream& operator<<( ostream& out,Polynomial& x )
{
//输出带附加头节点的多项式的链表x
Term* current = x.getHead()->link;
cout<<"The polynomial is:"<<endl;
bool h = true;
while(current != NULL)
{
if(h=false && current->ceof > 0.0)
out<<"+";
h = false;
out<<*current; //调用Term类的重载操作"<<"
current = current->link;
}
out<<endl;
return out;
}
Polynomial::Polynomial( Polynomial& R )
{
//复制构造函数,用已有的多项式对象来初始化当前的多项式对象
first = new Term(0,-1);
Term* destptr = first;
Term* srcptr = R.getHead()->link;
while(srcptr != NULL)
{
destptr->InsertAfter(srcptr->ceof,srcptr->exp);
//在destptr所指的节点后插入新节点,再让destptr指向这个新的节点
srcptr = srcptr->link;
destptr = destptr->link;
}
}
int Polynomial::MaxOrder()
{
//当多项式按照升序排列时,最后一项中是指数最大的项
Term* current = first;
while(current->link != NULL)
current = current->link; //空表时current停留在first,否则停在表尾节点
return current->exp;
}
istream& operator>>( istream& in,Polynomial& x )
{
//从输入流in输入各项,用尾插法建立一个多项式
Term* rear = x.getHead(); //rear是尾指针
int c,e;
while(1)
{
cout<<"Input a term(coef,exp):"<<endl;
in>>c>>e;
if(e<0)
break; //用e<0控制输入的结束
rear = rear->InsertAfter(c,e); //链接到rear所指节点后
}
return in;
}
//************************************
//返回: Polynomial
//参数:两个带附加头节点的按升幂排列的多项式链表的头指针分别是A.first和B.first
//************************************
Polynomial operator+( Polynomial& A,Polynomial& B )
{
Term *pa,*pb,*pc,*p;
float temp;
Polynomial C;
pc = C.first; //pc为结果多项式R在创建过程中的尾指针
//pa、pb分别定位于A与B的第一个节点,是两个链的检测指针
pa = A.getHead()->link;
pb = B.getHead()->link;
while(pa != NULL && pb != NULL)
{
if(pa->exp == pb->exp) //系数相等
{
temp = pa->ceof + pb->ceof;
if (fabs(temp)>0.001) //相加后系数不为零
pc = pc->InsertAfter(temp,pa->exp);
pa = pa->link;
pb = pb->link;
}
else if(pa->exp < pb->exp)
{
pc = pc->InsertAfter(pa->ceof,pa->exp);
pa = pa->link;
}
else
{
pc = pc->InsertAfter(pa->ceof,pa->exp);
pb = pb->link;
}
}
if(pa != NULL)
p = pa; //p指向剩余链的对象
else
p = pb;
while(p != NULL) //处理剩余链的部分
{
pc = pc->InsertAfter(p->ceof,p->exp);
p = p->link;
}
return C;
}
//************************************
// 返回:返回指向存储乘积多项式的单链表的头指针
// 参数:一元多项式A和B相乘
//************************************
Polynomial operator*( Polynomial& A,Polynomial& B )
{
Term *pa,*pb,*pc;
int AL,BL,i,k,maxExp;
Polynomial C;
pc = C.getHead(); //结果多项式在创建过程中的尾指针
//两个多项式的阶数
AL = A.MaxOrder();
BL = B.MaxOrder();
if(AL != -1 && BL != -1) //没有零多项式执行相乘运算
{
maxExp = AL+BL; //结果多项式的最高阶
float * result = new float[maxExp+1];
for(i=0;i<=maxExp;i++)
result[i] = 0.0;
pa = A.getHead()->link; //多项式A的检测指针
while(pa != NULL)
{
pb = B.getHead()->link; //多项式B的检测指针
while(pb != NULL)
{
k = pa->exp + pb->exp; //乘积的指数
result[k] = result[k] + pa->ceof*pb->ceof;
pb = pb->link;
}
pa = pa->link;
}
//在result[]中形成计算结果
for(i = 0;i<=maxExp;i++)
if(fabs(result[i])>0.001) //该项可以插入结果多项式链表
pc = pc->InsertAfter(result[i],i);
delete []result;
}
pc->link = NULL;
return C;
}
int main()
{
Polynomial A,B,C;
cout<<"Input Polynomial A:"<<endl;
cin>>A;
cout<<"Input Polynomial B:"<<endl;
cin>>B;
C = A+B;
cout<<"A+B = "<<C<<endl;
C = A*B;
cout<<"A*B = "<<C<<endl;
return 1;
}