多項式的表示與運算:
在計算機中表示這個多項式是,可以用一塊連續的存儲空間(例如用一維數組)來依次存放n+1個係數ai(i=0,2...n),這種表示方式中,即使某次項的係數爲0,該係數也必須要存儲。當多項式中存在大量的零係數是,這樣太浪費空間了;
而採用鏈表表示多項式是,對多項式每個非零係數的項構成鏈表中的一個結點,而對於係數爲零的項就不用表示。
一.多項式類:文件名 Poly.h
#include <iostream>
using namespace std;
//定義結點類型
struct node
{
int exp;//指數爲整型
double coef;//係數爲雙精度型
node * next;
};
//多項式循環鏈表類
class Poly
{
private:
node * head;
public:
Poly();//構造函數,建立空多項式鏈表
void in1_Poly();//鍵盤輸入多項式鏈表
void in2_Poly(int,int[],double[]);
void del_Poly();
void prt_Poly();
Poly operator +(Poly &);
Poly operator *(Poly &);
};
Poly::Poly()
{
node *p;
p=new node;
p->exp=-1;
p->coef=0;
p->next=p;
head=p;
return;
}
//鍵盤輸入多項式鏈表
void Poly::in1_Poly()
{
node *p,*k;
int e;
double c;
k=head;
cout<<"輸入:係數<空格>指數。輸入指數-1結束!"<<endl;
cin>>c>>e;
while (e!=-1)
{
p=new node;
p->exp=e;
p->coef=c;
k->next=p;
k=p;
cin>>c>>e;
}
return;
}
//由數組複製多項式鏈表
//void Poly::in2_Poly(int n,int e[],int c[])
void Poly::in2_Poly(int n,int e[],double c[])
{
int k;
node *p;
for(k=n-1;k>=0;k--)
{
p=new node;
p->exp=e[k];
p->coef=c[k];
p->next=head->next;
head->next=p;
}
return;
}
//釋放多項式鏈表
void Poly::del_Poly()
{
node *p,*q;
q=head->next;
while(q!=head)
{
p=q->next;
delete q;
q=p;
}
q->next=head;
return;
}
//輸出多項式鏈表
void Poly::prt_Poly()
{
node *k;
if(head->next==head)
cout<<"空表"<<endl;
k=head->next;
while(k!=head)
{
cout<<"("<<k->coef<<","<<k->exp<<")"<<endl;
k=k->next;
}
return;
}
//多項式相加
Poly Poly::operator +(Poly &p2)
{
Poly p;
node *k,*q,*m,*n;
int e;
double c;
k=p.head;//記住和多項式鏈尾
m=head->next;
n=p2.head->next;
while((m->exp!=-1)||(n->exp!=-1))
{
if(m->exp==n->exp)
{
c=m->coef+n->coef;
e=m->exp;
m=m->next;
n=n->next;
}
else if(m->exp>n->exp)
{
c=m->coef;
e=m->exp;
m=m->next;
}
else
{
c=n->coef;
e=n->exp;
n=n->next;
}
if(c!=0)
{
q=new node;
q->exp=e;
q->coef=c;
q->next=p.head;
k->next=q;
k=q;
}
}
return(p);
}
//多項式相乘
Poly Poly::operator*(Poly &p2)
{
Poly p,p1,p3;
node *q,*k,*m,*n;
m=head->next;
while(m->exp!=-1)
{
p3=p;
k=p1.head;
n=p2.head->next;
while(n->exp!=-1)
{
q=new node;
q->exp=m->exp+n->exp;
q->coef=m->coef*n->coef;
q->next=p1.head;
k->next=q;
k=q;
n=n->next;
}
p=p1+p3;
p1.del_Poly();
p3.del_Poly();
m=m->next;
}
return p;
}
二.應用實例:
設有兩個稀疏多項式如下:
p1(x)=3X10+4X8-5X5+2X4-3X+10;
P2(X)=4X14+3X8-7X6-2X4+5X-6
完成它們的加乘:
#include "Poly.h"
#include <stdlib.h>
int main()
{
Poly p1,p2,add_p,mul_p;
int e1[6]={10,8,5,4,1,0};
double c1[6]={3.0,4.0,-5.0,2.0,-3.0,10.0};
int e2[6]={14,8,6,4,1,0};
double c2[6]={4.0,3.0,-7.0,-2.0,5.0,-6.0};
p1.in2_Poly(6,e1,c1);
p2.in2_Poly(6,e2,c2);
//p1.in1_Poly();//鍵盤輸入多項式p1的係數和指數
//p2.in1_Poly();
cout<<"輸出多項式p1:"<<endl;
p1.prt_Poly();
cout<<"輸出多項式p2:"<<endl;
p2.prt_Poly();
add_p=p1+p2;
cout<<"輸出多項式p=p1+p2:"<<endl;
add_p.prt_Poly();
mul_p=p1*p2;
cout<<"輸出多項式p=p1*p2;"<<endl;
mul_p.prt_Poly();
p1.del_Poly();
cout<<"輸出多項式p1:"<<endl;
p1.prt_Poly();
p2.del_Poly();
cout<<"輸出多項式p2:"<<endl;
p2.prt_Poly();
system("pause");
return 0;
}
三. 實驗結果:
輸出多項式p1:
(3,10)
(4,8)
(-5,5)
(2,4)
(-3,1)
(10,0)
輸出多項式p2:
(4,14)
(3,8)
(-7,6)
(-2,4)
(5,1)
(-6,0)
輸出多項式p=p1+p2:
(4,14)
(3,10)
(7,8)
(-7,6)
(-5,5)
(2,1)
(4,0)
輸出多項式p=p1*p2;
(12,24)
(16,22)
(-20,19)
(17,18)
(-9,16)
(-12,15)
(6,14)
(-15,13)
(-2,12)
(50,11)
(-32,10)
(21,9)
(2,8)
(21,7)
(-95,6)
(46,5)
(-32,4)
(-15,2)
(68,1)
(-60,0)
輸出多項式p1:
空表
輸出多項式p2:
空表
請按任意鍵繼續. . .