02-線性結構1 一元多項式的乘法與加法運算

//本代碼存在BUG,只能得分16,正在調試中

#include <iostream>

#include <math.h>
using namespace std;
struct Term             //多項式節點類的定義
{
float coef;         //係數
int exp;            //指數
Term *link;
Term(float c,int e,Term *next = NULL)
{coef=c;exp=e;link =next;}
Term *InsertAfter(float c,int e);
friend ostream& operator<< (ostream&,const Term&);
};
class Polynomal        //多項式的類定義
{
public:
Polynomal(){first = new Term(0,-1);}     //構造函數,建立空鏈表
Polynomal(Polynomal& R);                //拷貝構造函數
int maxOrder();                          //獲取多項式最大指數
Term *getHead() const {return first;}    //獲取表頭指針
void reverse(Term*& head); 
private:
Term *first;
friend ostream& operator<<(ostream&,const Polynomal&);
friend istream& operator>>(istream&,Polynomal&);
// friend Polynomal operator+(Polynomal&,Polynomal&);
friend Polynomal operator*(Polynomal&,Polynomal&);
};
Term* Term::InsertAfter(float c,int e){
link=new Term(c,e,link);          //創建一個新節點
return link;
}
void Polynomal::reverse(Term*& head){
if(head ==NULL)
return;
Term *pre, *cur, *ne;
pre=head;
cur=head->link;
while(cur)
{
ne = cur->link;
cur->link = pre;
pre = cur;
cur = ne;
}
head->link = NULL;
head = pre;
}
ostream& operator<<(ostream& out,const Term& x){
// if (x.coef==0.0) return out;           //零係數項不輸出
out<<x.coef;                           //輸出係數
switch(x.exp){                        //輸出指數
// case 0: break;
// case 1: out<<" ";break;
default:
{
if (x.link!=NULL)
{
out<<" "<<x.exp<<" ";break;
}
out<<" "<<x.exp;break;



}
return out;
}
Polynomal::Polynomal(Polynomal& R){
//拷貝構造函數
first = new Term(0,-1);
Term *destptr = first,*srcptr=R.getHead()->link;
while(srcptr!=NULL){
destptr->link=srcptr->InsertAfter(srcptr->coef,srcptr->exp);
srcptr=srcptr->link;
destptr=destptr->link;
}
}
int Polynomal::maxOrder(){                //獲取最大指數,鏈表最後一項
Term *current =first;
// while(current->link!=NULL) 
current=current->link;
return current->exp;
}
istream& operator>>(istream& in,Polynomal& x){        //輸入符重載
Term *rear = x.getHead();
int c,e,N;
cin>>N;
while(N--){
// cout<<"Input a term(coef,exp):"<<endl;
in>>c>>e;
// if (e<0)break;                               //控制結束
rear =rear->InsertAfter(c,e);

}
return in;
}
ostream& operator<<(ostream& out,Polynomal& x){     //輸出符重載
Term *current = x.getHead()->link;
// cout<<"The Polynomal is:"<<endl;
bool h=true;
while(current!=NULL){
// if(h=false&&current->coef>0.0){
// out<<"+";
// }
h=false;
out<< *current;
current= current->link;
}
out<<endl;
return out;
}
Polynomal operator+(Polynomal& A,Polynomal& B){
Term *pa,*pb,*pc,*p,*pr;
float temp;
Polynomal C;
pc=C.getHead();
pa=A.getHead()->link;
A.reverse(pa);
pb=B.getHead()->link;
B.reverse(pb);
while(pa!=NULL&&pb!=NULL){
if (pa->exp==pb->exp)
{
temp=pa->coef+pb->coef;
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->coef,pa->exp);
pa=pa->link;
}
else{
pc=pc->InsertAfter(pb->coef,pb->exp);
pb=pb->link;
}
}
if (pa!=NULL)
{
p=pa;
}
else
p=pb;
while(p!=NULL){
pc=pc->InsertAfter(p->coef,p->exp);
p=p->link;
}
pr=C.getHead();
C.reverse(pr->link);
return C;
}
Polynomal operator*(Polynomal& A,Polynomal& B){
Term *pa,*pb,*pc,*pr;
int AL,BL,i,k,maxExp;
Polynomal 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->coef*pb->coef;
pb=pb->link;
}
pa=pa->link;
}
for (i = 0; i <= maxExp; i++)
if(fabs(result[i])>0.001)
pc=pc->InsertAfter(result[i],i);
delete []result;
}
pc->link=NULL;
pr=C.getHead();
C.reverse(pr->link);
return C;
}




//測試主函數
int main()
{
Polynomal a,b,c,d;
cin>>a;
// cout<<a<<endl;
cin>>b;
// cout<<b<<endl;
c=a*b;
cout<<c;
d=a+b;
cout<<d;
return 0;
 } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章