实验题1-1 多项式运算
【问题描述】有两个指数递减的一元多项式,写一程序先求这两个多项式的和,再求它们的积。
【提示】 用带表头结点的单链表作为多项式的存储表示;要建立两个单链表;多项式相加就是要把一个单链表中的结点插入到另一个单链表中去,要注意插入、删除操作中指针的正确修改。
#include <iostream>
using namespace std;
int k = 1;
typedef struct {
float coef; //系数
int expn; //指数
}term;
struct LNode{
term data;
struct LNode *next;
};
typedef struct LNode* Polynomial;
//合并同类项
Polynomial MergePolyn(Polynomial P)
{
Polynomial p = NULL,q = NULL,r = NULL,Q = NULL;
if(P == NULL||P->next == NULL){
return NULL;
}
else{
for(p = P->next;p!=NULL;p=p->next)
for(q = p->next,r = p;q!=NULL;){
if(p->data.expn == q->data.expn){
p->data.coef+=q->data.coef;
Q = q;
r->next = q->next;
q = r->next;//?至关重要
delete Q;
}
else
r=r->next,q = q->next;
}
return P;
}
}
//1.创建一个一元多项式
Polynomial CreatePolyn(int m)
{
if(m>=0){
Polynomial head = new LNode;
head->next = NULL;
Polynomial s,r = head;
for(int i=0;i<m;i++){
cout<<"第"<<i+1<<"项的系数和指数是:";
float c;
int e;
cin>>c>>e;
s = new LNode;
s->next = NULL;
s->data.coef = c;
s->data.expn = e;
r->next = s;
r = r->next;
}
r->next = NULL;
return head;
}
else{
cout<<"输入m值非法,创建多项式失败\n";
k = 0;
return NULL;
}
}
//2.两个一元多项式相加
Polynomial AddPolyn(const Polynomial pa,const Polynomial pb)
{
if(pa&&pa->next&&pb&&pb->next){
Polynomial head = new LNode;
Polynomial s = pa->next,t = pb->next,r = head;
r->next = NULL;
while(s!=NULL){
Polynomial n = new LNode;
n->next = NULL;
n->data.coef = s->data.coef;
n->data.expn = s->data.expn;
r->next = n;
r = r->next;
s = s->next;
}
while(t!=NULL){
Polynomial n = new LNode;
n->next = NULL;
n->data.coef = t->data.coef;
n->data.expn = t->data.expn;
r->next = n;
r = r->next;
t = t->next;
}
r->next = NULL;
head = MergePolyn(head);
return head;
}
else if((pa == NULL||pa->next == NULL)&&(pb!=NULL&&pb->next!=NULL))
return pb;
else if((pb == NULL||pb->next == NULL)&&(pa!=NULL&&pa->next!=NULL))
return pa;
else
return NULL;
}
//3.两个一元多项式相乘
Polynomial MultiplyPolyn(const Polynomial pa,const Polynomial pb)
{
if(pa==NULL||pa->next==NULL||pb==NULL||pb->next==NULL)
return NULL;
else{
Polynomial head = new LNode;
head->next = NULL;
Polynomial p,q,r,s = head;
for(p = pa->next;p!=NULL;p=p->next)
for(q = pb->next;q!=NULL;q=q->next){
r = new LNode;
r->data.coef = p->data.coef * q->data.coef;
r->data.expn = p->data.expn + q->data.expn;
r->next = NULL;//千万记住将new的结点指针域置空
s->next = r;
s = s->next;
}
s->next = NULL;
head = MergePolyn(head);
return head;
}
}
//打印多项式
void PrintPolyn(Polynomial P)
{
if(P==NULL||P->next==NULL){
cout<<"无项\n";
}
else{
cout<<"Y=";
int iszero = 1;
Polynomial p = NULL,q = NULL;
p = P->next;
if(p->data.coef&&p->data.coef!=1&&p->data.coef!=-1&&p->data.expn&&p->data.expn!=1)
{cout<<p->data.coef<<"X^"<<p->data.expn;iszero = 0;}
else if(p->data.coef&&p->data.coef!=1&&p->data.coef!=-1&&p->data.expn==1)
{cout<<p->data.coef<<"X";iszero = 0;}
else if(p->data.coef&&p->data.coef!=1&&p->data.coef!=-1&&p->data.expn==0)
{cout<<p->data.coef;iszero = 0;}
else if(p->data.coef==1&&p->data.expn&&p->data.expn!=1)
{cout<<"X^"<<p->data.expn;iszero = 0;}
else if(p->data.coef==1&&p->data.expn==1)
{cout<<"X";iszero = 0;}
else if(p->data.coef==1&&p->data.expn==0)
{cout<<"1";iszero = 0;}
else if(p->data.coef==-1&&p->data.expn&&p->data.expn!=1)
{cout<<"-X^"<<p->data.expn;iszero = 0;}
else if(p->data.coef==-1&&p->data.expn==1)
{cout<<"-X";iszero = 0;}
else if(p->data.coef==-1&&p->data.expn==0)
{cout<<"-1";iszero = 0;}
else if(p->data.coef==0)
;
else
{cout<<p->data.coef<<"X^"<<p->data.expn;iszero = 0;}
if(p->next)
q = p->next;
while(q){
if(q->data.coef>0&&q->data.coef!=1&&q->data.expn&&q->data.expn!=1)
{cout<<"+"<<q->data.coef<<"X^"<<q->data.expn;iszero = 0;}
else if(q->data.coef>0&&q->data.expn==0)
{cout<<"+"<<q->data.coef;iszero = 0;}
else if(q->data.coef>0&&q->data.coef!=1&&q->data.expn==1)
{cout<<"+"<<q->data.coef<<"X";iszero = 0;}
else if(q->data.coef==1&&q->data.expn&&q->data.expn!=1)
{cout<<"+"<<"X^"<<q->data.expn;iszero = 0;}
else if(q->data.coef==1&&q->data.expn==1)
{cout<<"+"<<"X";iszero = 0;}
else if(q->data.coef==1&&q->data.expn==0)
{cout<<"1";iszero = 0;}
else if(q->data.coef<0&&q->data.coef!=-1&&q->data.expn==1)
{cout<<q->data.coef<<"X";iszero = 0;}
else if(q->data.coef==-1&&q->data.expn&&q->data.expn!=1)
{cout<<"-X^"<<q->data.expn;iszero = 0;}
else if(q->data.coef==-1&&q->data.expn==1)
{cout<<"-X";iszero = 0;}
else if(q->data.coef==-1&&q->data.expn==0)
{cout<<"-1";iszero = 0;}
else if(q->data.coef==0)
;
else
{cout<<q->data.coef<<"X^"<<q->data.expn;iszero = 0;}
q=q->next;
}
if(iszero)
cout<<"0";
cout<<endl;
}
}
int main()
{
Polynomial pa = NULL,pb = NULL,P = NULL,Q = NULL,R = NULL,T = NULL;
do{
cout<<"1.创建两个一元多项式"<<endl;
cout<<"2.两个一元多项式相加得一新多项式"<<endl;
cout<<"3.两个一元多项式相乘得一新多项式"<<endl;
cout<<"请选择:";
int n,m;
cin >> n;
switch(n)
{
case 1:
cout<<"请输入第一个多项式的项数:";
cin>>m;
P = CreatePolyn(m);
pa = MergePolyn(P);
PrintPolyn(pa);
cout<<"请输入第二个多项式的项数:";
cin>>m;
Q = CreatePolyn(m);
pb = MergePolyn(Q);
PrintPolyn(pb);
break;
case 2:
R = AddPolyn(pa,pb);
PrintPolyn(R);
break;
case 3:
T = MultiplyPolyn(pa,pb);
PrintPolyn(T);
break;
default:
k = 0;
cout<<"输入了无效字符\n";
break;
}
}while(k);
return 0;
}
注意点:不能随意修改pa,pb的值,两多项式的加法运算与乘法运算应该互不干扰,尤其注意在多项式加法中不能采用两链表合为一个链表的算法(会改变pa的链表)。
P.S 对每一个new的结点的指针域要置空,同理,对每一个delete后的悬垂指针也应置空,否则后面的程序使用的时候以为还是没释放的对象, 访问就会出错。