#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct{
float coef;
int expn;
}term,ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
typedef LinkList polynomial;
void InitList(polynomial &P){
P=new LNode;
if(!P){
cout<<"InitList error"<<endl;
exit(1);
}
P->data.coef=0.0;
P->data.expn=0;
P->next=NULL;
}
polynomial GetHead(polynomial P){
if(!P){
cout<<"The polynomial is empty ,no head node"<<endl;
exit(1);
}
else return P;
}
void SetCurElem(polynomial &h,ElemType e){
if(!h){
cout<<"EetCurElem error the h is NULL\n";
exit(1);
}
h->data=e;
}
void SetCurElem(polynomial &h,float coef){
if(!h){
cout<<"EetCurElem error the h is NULL\n";
exit(1);
}
h->data.coef=coef;
}
int cmp(term a,term b){
if(a.expn==b.expn)return 0;
else if(a.expn<b.expn)return -1;
else return 1;
}
bool MakeNode(LinkList &s,ElemType e){
s=new LNode;
if(!s)return false;
s->data=e;//just test
s->next=NULL;
return true;
}
void InsFirst(LinkList h,LinkList s){//insert s to be next node of h
if(!s){
cout<<"InsFirst(),the node you instret is NULL"<<endl;
exit(1);
}
s->next=h->next;
h->next=s;
}
void DelFirst(LinkList h,LinkList &q){
q=h->next;
h->next=q->next;
}
void FreeNode(LinkList &q){
q->next=NULL;
delete q;
q=NULL;
}
LinkList LocateElem(polynomial P,ElemType e,LinkList &q,int (*cmp)(term,term)){
q=P;
while(q->next){
if(!cmp(q->next->data,e)){
cout<<"already have a node data whose expn equal to e.expn"<<endl;
return q->next;//already have a node data whose expn equal to e.expn
}
else if(cmp(e,q->next->data)==-1){
return NULL;
}
q=q->next;
}
return NULL;
}
void CreatPolyn(polynomial &P,int m){
InitList(P);
polynomial h=GetHead(P);
ElemType e;
LinkList s,q;//MakeNode()
e.coef=0.0;
e.expn=-1;
SetCurElem(h,e);
for(int i=0;i<m;i++){
cin>>e.coef;
cin>>e.expn;
if(!LocateElem(P,e,q,cmp)){
if(MakeNode(s,e)) InsFirst(q,s);
}
}
}
void Append(LinkList L,LinkList s){
while(L->next){
L=L->next;
}
L->next=s;
}
bool ListEmpty(LinkList L){
if(L->next)return false;
return true;
}
void AddPolyn(polynomial &Pa,polynomial &Pb){
polynomial ha=GetHead(Pa);
polynomial hb=GetHead(Pb);
polynomial qa=ha->next;
polynomial qb=hb->next;
float sum;
while(qa&&qb){
term a=qa->data;
term b=qb->data;
switch(cmp(a,b)){
case -1:
ha=qa;
qa=qa->next;
break;
case 0:
sum=a.coef+b.coef;
if(sum!=0.0){
SetCurElem(qa,sum);
ha=qa;
}
else{
DelFirst(ha,qa);
FreeNode(qa);
}
DelFirst(hb,qb);
FreeNode(qb);
qb=hb->next;
qa=ha->next;
break;
case 1:
DelFirst(hb,qb);
InsFirst(ha,qb);
qb=hb->next;
ha=ha->next;
break;
}
}
if(!ListEmpty(Pb)){
Append(Pa,qb);
FreeNode(hb);
}
}
void PrintPolyn(polynomial q){
q=q->next;
while(q){
cout<<q->data.coef<<'E'<<q->data.expn;
if(q->next!=NULL)cout<<" + ";
q=q->next;
}
cout<<endl;
}
void DestroyPolyn(polynomial &p){
polynomial temp;
while(p){
temp=p->next;
delete p;
p=temp;
}
}
int main(){
int m=0;
polynomial a,b;
cout<<"输入要建立多项式 a 的项数"<<endl;
cin>>m;
cout<<"输入多项式 a 每一项的 系数 和 次数"<<endl;
CreatPolyn(a,m);
cout<<"你输入的多项式 a 是:"<<endl;
cout<<" ";
PrintPolyn(a);
cout<<"输入要建立多项式 b 的项数"<<endl;
cin>>m;
cout<<"输入多项式 b 每一项的 系数 和 次数"<<endl;
CreatPolyn(b,m);
cout<<"你输入的多项式 b 是:"<<endl;
cout<<" ";
PrintPolyn(b);
AddPolyn(a,b);
cout<<"a , b 相加的结果是:"<<endl;
cout<<" ";
PrintPolyn(a);
DestroyPolyn(a);
return 0;
}
Polynomial_add.cpp//多项式相加
最新推荐文章于 2022-12-19 23:59:04 发布