用多项试实现数组的加法和剩法

来源:互联网 发布:python shell变量互传 编辑:程序博客网 时间:2024/06/05 16:41

好吧,程序很烂,但我一点点在努力,呵呵~~加油~~

//单键表#include<iostream>using namespace std;typedef struct Node* PtrToNode;typedef PtrToNode Polynomail;//多项式ADT键表实现在的类型声明struct Node{int Cofficient;int Exponent;PtrToNode next;};//生成一个空的多项式,用系数和阶-1来表示头结点void Zero(Polynomail P){P->Cofficient=-1;P->Exponent=-1;P->next=NULL;}//两个多项式相加void AddPolynomial(const Polynomail Poly1,const Polynomail Poly2,Polynomail Poly3){PtrToNode p1,p2,p3;p1=Poly1->next;p2=Poly2->next ;p3=Poly3;while (p1!=NULL&&p2!=NULL){if(p1->Exponent>p2->Exponent){PtrToNode temp=(PtrToNode)malloc(sizeof(struct Node));temp->Cofficient=p1->Cofficient;temp->Exponent=p1->Exponent;temp->next=NULL;p3->next=temp;p1=p1->next;p3=p3->next;}else if(p1->Exponent<p2->Exponent){PtrToNode temp=(PtrToNode)malloc(sizeof(struct Node));temp->Cofficient=p2->Cofficient;temp->Exponent=p2->Exponent;temp->next=NULL;p3->next=temp;p2=p2->next;p3=p3->next;}else if(p1->Exponent==p2->Exponent){PtrToNode temp=(PtrToNode)malloc(sizeof(struct Node));temp->Cofficient=p1->Cofficient+p2->Cofficient;temp->Exponent=p1->Exponent;temp->next=NULL;p3->next=temp;p1=p1->next;p2=p2->next;p3=p3->next;}}if(p1==NULL&&p2!=NULL){PtrToNode temp;while(p2!=NULL){temp=(PtrToNode)malloc(sizeof(struct Node));temp->Cofficient=p2->Cofficient;temp->Exponent=p2->Exponent;temp->next=NULL;p3->next=temp;p2=p2->next;p3=p3->next;}}else if(p1!=NULL&&p2==NULL){PtrToNode temp;while(p1!=NULL){temp=(PtrToNode)malloc(sizeof(struct Node));temp->Cofficient=p1->Cofficient;temp->Exponent=p1->Exponent;temp->next=NULL;p3->next=temp;p1=p1->next;p3=p3->next;}}}void MulPolynomial(const Polynomail Poly1,const Polynomail Poly2,Polynomail Poly3){PtrToNode p1,p2,p3;p3=Poly3;PtrToNode temp; for(p1=Poly1->next;p1!=NULL;p1=p1->next){for(p2=Poly2->next;p2!=NULL;p2=p2->next){temp=(PtrToNode)malloc(sizeof(struct Node));temp->Cofficient=(p1->Cofficient)*(p2->Cofficient);temp->Exponent=p1->Exponent+p2->Exponent;temp->next=NULL;PtrToNode bPoly3=Poly3->next;while(bPoly3!=NULL)//合并同类项{if(bPoly3->Exponent==temp->Exponent){bPoly3->Cofficient=bPoly3->Cofficient+temp->Cofficient;break;}bPoly3=bPoly3->next;}p3->next=temp;p3=p3->next;}}//把得到的多项式进行排序for(PtrToNode pai=Poly3->next;pai!=NULL;pai=pai->next){p3=Poly3->next;while(p3->next!=NULL){if(p3->next->Exponent>p3->Exponent){ int k;k=p3->next->Cofficient;p3->next->Cofficient=p3->Cofficient;p3->Cofficient=k;k=p3->next->Exponent;p3->next->Exponent=p3->Exponent;p3->Exponent=k;}else{p3=p3->next;}}}}


继续努力,哈哈~~

原创粉丝点击