Polynomial_add.cpp//多项式相加

#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;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值