N个一元多项式的加减乘除执行代码

合集下载

c语言一元多项式的加法,减法,乘法的实现

c语言一元多项式的加法,减法,乘法的实现

c语言一元多项式的加法,减法,乘法的实现一元多项式是代数学中的重要概念,它由各项式的系数和幂次构成。

在C语言中,我们可以通过定义结构体来表示一元多项式,并实现加法、减法和乘法运算。

我们定义一个结构体来表示一元多项式。

结构体中包含两个成员变量,一个是整数类型的系数coeff,另一个是整数类型的幂次exp。

```ctypedef struct{int coeff; // 系数int exp; // 幂次} Polynomial;```接下来,我们可以实现一元多项式的加法运算。

加法运算的规则是将两个多项式中幂次相同的项的系数相加,若幂次不同的项,则直接将其添加到结果多项式中。

具体实现如下:```cPolynomial addPolynomial(Polynomial poly1, Polynomial poly2){Polynomial result;result.coeff = poly1.coeff + poly2.coeff;result.exp = poly1.exp;return result;}```然后,我们可以实现一元多项式的减法运算。

减法运算的规则是将被减多项式的各项的系数取相反数,然后再与减数多项式相加。

具体实现如下:```cPolynomial subtractPolynomial(Polynomial poly1, Polynomial poly2){Polynomial result;result.coeff = poly1.coeff - poly2.coeff;result.exp = poly1.exp;return result;}```我们可以实现一元多项式的乘法运算。

乘法运算的规则是将两个多项式的每一项相乘,然后将结果相加。

具体实现如下:```cPolynomial multiplyPolynomial(Polynomial poly1, Polynomialpoly2){Polynomial result;result.coeff = poly1.coeff * poly2.coeff;result.exp = poly1.exp + poly2.exp;return result;}```通过上述的实现,我们可以对一元多项式进行加法、减法和乘法运算。

一元多项式的各种运算实现(c++,c)

一元多项式的各种运算实现(c++,c)
{
float a=1.0;
for(int i=1;i<=k;i++)
a=a*1e-1;
return a;
}
//赋值,参数是指向头结点的指针
void fuzhi(node *&toup)
{
float fumi(int k);
void init(node *&toup);
if(!toup) init(toup);
if(toup) destory(toup);//假如有头结点或元素节点,销毁结点
toup=(node*)malloc(sizeof(node));
if(!toup){cout<<"溢出错误"<<endl;exit(0ext=NULL;
}
//销毁全部,包括头结点
cout<<"*******************************************************************************"<<endl;
cout<<"举例:3 x3 9.3x-3 -x....."<<endl;
}
//求未知数指数是-k时的值
float fumi(int k)
while(p)
{
if(p->data ==0)
{
if(!pr)
{
tem=p;
toup->next =p->next ;
p=p->next ;
toup->length--;
free(tem);
tem=NULL;

一元多项式加法、减法、乘法实现源代码

一元多项式加法、减法、乘法实现源代码

⼀元多项式加法、减法、乘法实现源代码////链接程序:#include#include#include//using namespace std;#define N 1000//#define INF 65535void link();void shunxu();void Menu();typedef struct{int a[N];//记录多项式int len;//记录多项式的长度}Ploy;typedef struct //项的表⽰{ float coef; //系数int expn; //指数}term;typedef struct LNode{ term data; //term多项式值struct LNode *next;}LNode,*LinkList; //两个类型名typedef LinkList polynomail; //⽤带头结点的有序链表表⽰多项式/*⽐较指数*/int cmp(term a,term b){ if(a.expn>b.expn) return 1;if(a.expn==b.expn) return 0;if(a.expnelse exit(-2);}/*⼜⼩到⼤排列*/void arrange1(polynomail pa){ polynomail h=pa,p,q,r;if(pa==NULL) exit(-2);for(p=pa;p->next!=NULL;p=p->next); r=p;for(h=pa;h->next!=r;)//⼤的沉底{ for(p=h;p->next!=r&&p!=r;p=p->next)if(cmp(p->next->data,p->next->next->data)==1){ q=p->next->next;p->next->next=q->next;q->next=p->next;p->next=q;}r=p;//r指向参与⽐较的最后⼀个,不断向前移动} }/*由⼤到⼩排序*/void arrange2(polynomail pa){ polynomail h=pa,p,q,r;if(pa==NULL) exit(-2);for(p=pa;p->next!=NULL;p=p->next); r=p;for(h=pa;h->next!=r;)//⼩的沉底{ for(p=h;p->next!=r&&p!=r;p=p->next)if(cmp(p->next->next->data,p->next->data)==1){ q=p->next->next;p->next->next=q->next;q->next=p->next;p->next=q;}r=p;//r指向参与⽐较的最后⼀个,不断向前移动} }/*打印多项式,求项数*/int printpolyn(polynomail P){ int i;polynomail q;if(P==NULL) printf("⽆项!\n");else if(P->next==NULL) printf("Y=0\n");else{ printf("该多项式为Y=");q=P->next;i=1;if(q->data.coef!=0&&q->data.expn!=0){ printf("%.2fX^%d",q->data.coef,q->data.expn); i++; }if(q->data.expn==0&&q->data.coef!=0)printf("%.2f",q->data.coef);//打印第⼀项q=q->next;if(q==NULL){printf("\n");return 1;}while(1)//while中,打印剩下项中系数⾮零的项,{ if(q->data.coef!=0&&q->data.expn!=0){ if(q->data.coef>0) printf("+");printf("%.2fX^%d",q->data.coef,q->data.expn); i++;}if(q->data.expn==0&&q->data.coef!=0){ if(q->data.coef>0) printf("+");printf("%f",q->data.coef);}q=q->next;if(q==NULL){ printf("\n"); break; }}}return 1;}/*1、创建并初始化多项式链表*/polynomail creatpolyn(polynomail P,int m){//输⼊m项的系数和指数,建⽴表⽰⼀元多项式的有序链表P polynomail r,q,p,s,Q;int i;P=(LNode*)malloc(sizeof(LNode));r=P;for(i=0;i{s=(LNode*)malloc(sizeof(LNode));printf("请输⼊第%d项的系数和指数:",i+1);scanf("%f%d",&s->data.coef,&s->data.expn);r->next=s;r=s;}r->next=NULL;if(P->next->next!=NULL){for(q=P->next;q!=NULL/*&&q->next!=NULL*/;q=q->next)//合并同类项for(p=q->next,r=q;p!=NULL;)if(q->data.expn==p->data.expn){q->data.coef=q->data.coef+p->data.coef;r->next=p->next;Q=p;p=p->next;free(Q);}else{r=r->next;p=p->next;}}return P;}/*2、两多项式相加*/polynomail addpolyn(polynomail pa,polynomail pb) {//完成多项式相加运算,即:Pa=Pa+Pb polynomail s,newp,q,p,r;int j;p=pa->next;q=pb->next;newp=(LNode*)malloc(sizeof(LNode));r=newp;while(p&&q)//p&&q都不为空{s=(LNode*)malloc(sizeof(LNode));switch(cmp(p->data,q->data)){case -1: s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s;r=s;p=p->next;break;case 0: s->data.coef=p->data.coef+q->data.coef;if(s->data.coef!=0.0){s->data.expn=p->data.expn;r->next=s;r=s;}p=p->next;q=q->next;break;case 1: s->data.coef=q->data.coef;s->data.expn=q->data.expn;r->next=s;r=s;q=q->next;break;}//switch}//while p||q有⼀个跑完就跳出该循环while(p)//p没跑完{s=(LNode*)malloc(sizeof(LNode));s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s;r=s;p=p->next;}//p跑完跳出循环while(q)//q没跑完{s=(LNode*)malloc(sizeof(LNode));s->data.coef=q->data.coef;s->data.expn=q->data.expn;r=s;q=q->next;}//q跑完跳出循环//p&&q都跑完r->next=NULL;for(q=newp->next;q->next!=NULL;q=q->next)//合并同类项{for(p=q;p!=NULL&&p->next!=NULL;p=p->next)if(q->data.expn==p->next->data.expn){q->data.coef=q->data.coef+p->next->data.coef;r=p->next;p->next=p->next->next;free(r);}}printf("升序 1 , 降序 2\n");printf("选择排序⽅式:");scanf("%d",&j);if(j==1)arrange1(newp);elsearrange2(newp);return newp;}/*3、两多项式相减*/polynomail subpolyn(polynomail pa,polynomail pb){//完成多项式相减运算,即:Pa=Pa-Pbpolynomail s,newp,q,p,r,Q; int j;p=pa->next;q=pb->next;newp=(LNode*)malloc(sizeof(LNode));r=newp;while(p&&q){s=(LNode*)malloc(sizeof(LNode));switch(cmp(p->data,q->data)){case -1: s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s;r=s;p=p->next;break;case 0: s->data.coef=p->data.coef-q->data.coef;if(s->data.coef!=0.0){s->data.expn=p->data.expn;r->next=s;r=s;}p=p->next;q=q->next;break;case 1: s->data.coef=-q->data.coef;s->data.expn=q->data.expn;r->next=s;r=s;}//switch}//whilewhile(p){s=(LNode*)malloc(sizeof(LNode));s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s;r=s;p=p->next;}while(q){s=(LNode*)malloc(sizeof(LNode));s->data.coef=-q->data.coef;s->data.expn=q->data.expn;r->next=s;r=s;q=q->next;}r->next=NULL;if(newp->next!=NULL&&newp->next->next!=NULL)//合并同类项{for(q=newp->next;q!=NULL;q=q->next)for(p=q->next,r=q;p!=NULL;)if(q->data.expn==p->data.expn){q->data.coef=q->data.coef+p->data.coef;r->next=p->next;Q=p;p=p->next;free(Q);}else{r=r->next;p=p->next;}}printf("升序 1 , 降序 2\n");printf("选择:");scanf("%d",&j);if(j==1)arrange1(newp);elsearrange2(newp);return newp;}/*4两多项式相乘*/polynomail mulpolyn(polynomail pa,polynomail pb){//完成多项式相乘运算,即:Pa=Pa*Pbpolynomail s,newp,q,p,r;int i=20,j;newp=(LNode*)malloc(sizeof(LNode));r=newp;for(p=pa->next;p!=NULL;p=p->next)for(q=pb->next;q!=NULL;q=q->next){s=(LNode*)malloc(sizeof(LNode));s->data.coef=p->data.coef*q->data.coef;s->data.expn=p->data.expn+q->data.expn;r->next=s;r=s;}r->next=NULL;printf("升序 1 , 降序 2\n");printf("选择:");scanf("%d",&j);if(j==1)arrange1(newp);elsearrange2(newp);for(;i!=0;i--){for(q=newp->next;q->next!=NULL;q=q->next)//合并同类项for(p=q;p!=NULL&&p->next!=NULL;p=p->next)if(q->data.expn==p->next->data.expn){q->data.coef=q->data.coef+p->next->data.coef;r=p->next;p->next=p->next->next;free(r);}}return newp;}/*5、销毁已建⽴的两个多项式*/void delpolyn(polynomail pa,polynomail pb){polynomail p,q;p=pa;while(p!=NULL){q=p;p=p->next;free(q);}p=pb;while(p!=NULL){q=p;p=p->next;free(q);}printf("两个多项式已经销毁\n");}void Menulink(){printf("\n");printf(" ********⼀元多项式链式存储的基本运算********\n"); printf(" 1、创建两个⼀元多项式请按1\n");printf(" 2、两多项式相加得⼀新多项式请按2\n");printf(" 3、两多项式相减得⼀新多项式请按3\n");printf(" 4、两多项式相乘得⼀新多项式请按4\n");printf(" 5、销毁已建⽴的两个多项式请按5\n");printf(" 6、退出该⼦系统返回主菜单请按6\n");printf(" 7、退出该系统请按7\n");printf(" ********************************************\n");printf("\n");}void link() //⼀元多项式链式存储的实现{polynomail pa=NULL,pb=NULL;polynomail p,q;polynomail addp=NULL,subp=NULL,mulp=NULL; int n,m;printf("已进⼊链式存储⼀元多项式运算的⼦系统\n"); Menulink();while(1){printf("请选择你想进⾏的链式存储运算操作:\n"); scanf("%d",&n);switch(n){case 1:if(pa!=NULL){printf("已建⽴两个⼀元多项式,请选择其他操作!"); break;}printf("请输⼊第⼀个多项式:\n");printf("要输⼊⼏项:");scanf("%d",&m);while(m==0){printf("m不能为0,请重新输⼊m:");scanf("%d",&m);}pa=creatpolyn(pa,m);printpolyn(pa);printf("请输⼊第⼆个多项式:\n");printf("要输⼊⼏项:");scanf("%d",&m);while(m==0){printf("m不能为0,请重新输⼊m:");scanf("%d",&m);}pb=creatpolyn(pb,m);printpolyn(pb);break;case 2:if(pa==NULL){printf("请先创建两个⼀元多项式!\n");break;}addp=addpolyn(pa,pb);printpolyn(addp);break;case 3:if(pa==NULL){printf("请先创建两个⼀元多项式!\n");break;}subp=subpolyn(pa,pb);printpolyn(subp);break;case 4:if(pa==NULL){printf("请先创建两个⼀元多项式!\n"); break;}mulp=mulpolyn(pa,pb);printpolyn(mulp);break;case 5:if(pa==NULL){printf("请先创建两个⼀元多项式!\n"); break;}delpolyn(pa,pb);pa=pb=NULL;printf("两个⼀元多项式的销毁成功!\n"); break;case 6:if(addp!=NULL){p=addp;while(p!=NULL){q=p;p=p->next;free(q);}}if(subp!=NULL){p=subp;while(p!=NULL){q=p;p=p->next;free(q);}}printf("返回主菜单\n");Menu();break;case 7:if(addp!=NULL){p=addp;while(p!=NULL){q=p;p=p->next;free(q);}}if(subp!=NULL){p=subp;while(p!=NULL){q=p;p=p->next;free(q);}}printf("已成功退出该系统,谢谢你的使⽤!\n");exit(-2);break;}//switch}//while}//2、顺序程序:void ADD(Ploy A,Ploy B,Ploy *M)/*多项式A与多项式B相加,得到多项式M*/{int la=A.len,lb=B.len,i;M->len=la>lb?la:lb;for(i=0;i<=la&&i<=lb;i++){M->a[i]=A.a[i]+B.a[i];}while(i<=la){M->a[i]=A.a[i];i++;}while(i<=lb){M->a[i]=B.a[i];i++;}return;}void SUB(Ploy A,Ploy B,Ploy *M)/*多项式A与多项式B相减(A-B),得到多项式M*/{int la=A.len,lb=B.len,i;M->len=la>lb?la:lb;for(i=0;i<=la&&i<=lb;i++){M->a[i]=A.a[i]-B.a[i];}while(i<=la) {M->a[i]=A.a[i];i++;}while(i<=lb) {M->a[i]=0-B.a[i];i++;}return ;}void MUL(Ploy A,Ploy B,Ploy *M)/*多项式A与多项式B相乘,得到多项式M*/{int i,j;for(i=0;i<=A.len+B.len+1;i++) M->a[i]=0;for(i=0;i<=A.len;i++)for(j=0;j<=B.len;j++){M->a[i+j]+=A.a[i]*B.a[j];}M->len=A.len+B.len;return ;}void GetPloy(Ploy *A){int i,coef,ex,maxe=0;//ex指指数,maxe指最⼤指数char ch;printf("请输⼊每个项的系数及对应的指数,指数为负数时标志输⼊结束!\n");for(i=0;iA->a[i]=0;scanf("%d%d",&coef,&ex);while(ex>=0){if(ex>maxe)maxe=ex;if(A->a[ex]!=0){printf("你输⼊的项已经存在,是否更新原数据?(Y/N)"); cin>>ch;if(ch=='Y'||ch=='y'){A->a[ex]=coef;printf("更新成功,请继续输⼊!\n");}elseprintf("请继续输⼊!\n");;}elseA->a[ex]=coef;scanf("%d%d",&coef,&ex);}A->len=maxe;return ;}void PrintPloy1(Ploy A)//降序输出顺序⼀元多项式{int i;printf(" %dx^%d ",A.a[A.len],A.len);for(i=A.len-1;i>=1;i--){if(A.a[i]==0) ;else if(A.a[i]==1) printf(" + x^%d ",i);else if(A.a[i]==-1) printf(" - x^%d ",i);else{if(A.a[i]>0)printf("+ %dx^%d ",A.a[i],i);elseprintf("- %dx^%d ",-A.a[i],i);}}if(A.a[0]==0) ;else if(A.a[0]>0)printf(" + %d",A.a[0]);//打印x的0次项elseprintf(" - %d",-A.a[0]);printf("\n");return ;}void PrintPloy2(Ploy A)//升序输出顺序⼀元多项式{int i=0;while(A.a[i]==0)++i;if(i==0)printf("%d",A.a[i]);else{if(A.a[i]==1)printf("x^%d",i);else if(A.a[i]==-1)printf("-x^%d",i);elseprintf("%dx^%d",A.a[i],i);}for(++i;i<=A.len;i++){if(A.a[i]==0) ;else if(A.a[i]==1)printf(" + x^%d",i);else if(A.a[i]==-1)printf(" - x^%d",i);else if(A.a[i]>1)printf(" + %dx^%d",A.a[i],i);else if(A.a[i]<-1)printf(" - %dx^%d",-A.a[i],i);}}void Menushunxu(){printf("\n");printf(" ********⼀元多项式顺序存储的基本运算********\n");printf(" 1、更新两个多项式⼀元多项式请按1\n");printf(" 2、两多项式相加得⼀新多项式请按2\n");printf("3、两多项式相减得⼀新多项式请按3\n");printf(" 4、两多项式相乘得⼀新多项式请按4\n");printf(" 5、退出该⼦系统,返回主菜单请按5\n");printf(" 6、退出该系统请按6\n");printf(" ********************************************\n");printf("\n");return ;}void shunxu() //⼀元多项式顺序存储的实现{Ploy A,B,M;int n,m;printf("进⼊顺序存储⼀元多项式运算⼦系统\n");printf("请输⼊多项式A:\n");GetPloy(&A);printf("请输⼊多项式B:\n");GetPloy(&B);printf("输出两个⼀元多项式A、B,降幂输出请按1,升幂输出请按2!\n"); cin>>m;while(m<1&&m>m){printf("你输⼊的输出新创⼀元多项式的操作号不合法,请重新输⼊\n"); cin>>m;}switch(m){case 1:if(m==1){printf("A降=");PrintPloy1(A);printf("\n");printf("B降=");PrintPloy1(B);break;case 2:if(m==2){printf("A升=");PrintPloy1(A);printf("\n");printf("B升=");PrintPloy1(B);}break;}Menushunxu();while(1){printf("请选择你想进⾏的顺序存储运算操作:\n");cin>>n;while(n<1&&n>6){printf("输⼊的顺序操作号不对,请重新输⼊\n");cin>>n;}switch(n){case 1:if(n==1)printf("更新两个多项式:\n");printf("请输⼊多项式A:\n");GetPloy(&A);printf("请输⼊多项式B:\n");GetPloy(&B);printf("输出两个更新的⼀元多项式A、B,降幂输出请按1,升幂输出请按2!\n"); cin>>m;while(m<1&&m>2){printf("你输⼊的输出排序操作号不合法,请重新输⼊\n");cin>>m;}switch(m){case 1:if(m==1){printf("A降=");PrintPloy1(A);printf("\n");printf("B降=");PrintPloy1(B);}break;case 2:if(m==2){printf("A升=");PrintPloy1(A);printf("\n");printf("B升=");PrintPloy1(B);}break;break;case 2:if(n==2)ADD(A,B,&M);printf("降幂输出请按1,升幂输出请按2!\n");cin>>m;while(m<1&&m>2){printf("你输⼊的输出排序操作号不合法,请重新输⼊\n"); cin>>m;}switch(m){case 1:if(m==1){printf("ADD降=");PrintPloy1(M);printf("\n");}break;case 2:if(m==2){printf("ADD升=");PrintPloy2(M);printf("\n");}break;}break;case 3:if(n==3)SUB(A,B,&M);printf("降幂输出请按1,升幂输出请2!\n");cin>>m;while(m<1&&m>2){printf("你输⼊的输出排序操作号不合法,请重新输⼊\n"); cin>>m;}switch(m){case 1:if(m==1){printf("SUB降=");PrintPloy1(M);printf("\n");}break;case 2:if(m==2){printf("SUB升=");PrintPloy2(M);printf("\n");}break;}break;case 4:if(n==4)MUL(A,B,&M);printf("降幂输出请按1,升幂输出请2!\n");cin>>m;while(m<1&&m>3){printf("你输⼊输出排序操作号不合法,请重新输⼊\n"); cin>>m;}switch(m){case 1:if(m==1){printf("MUL降=");PrintPloy1(M);printf("\n");}break;case 2:if(m==2){printf("MUL升=");PrintPloy2(M);printf("\n");}break;}break;case 5:if(n==5)printf("返回主菜单\n");Menu();break;case 6:if(n==6)printf("已成功退出该系统,谢谢你的使⽤!\n");exit(-2);break;}}}void Menu(){printf("\n");printf(" ************⼀元多项式的基本运算系统************\n"); printf(" 1、⼀元多项式顺序存储的⼦系统请按1\n");printf(" 2、⼀元多项式链式存储的基本运算请按2\n"); printf(" 3、退出系统请按3\n");printf(" ************************************************\n"); printf("\n");printf("请输⼊你想进⾏的操作号:\n");int n;scanf("%d",&n);while(n!=1 && n!=2 && n!=3){printf("对不起,你的输⼊不正确,请重新输⼊!\n"); scanf("%d",&n);}switch(n){case 1:if(n==1)shunxu();break;case 2:if(n==2)link();break;case 3:if(n==3)printf("已成功退出该系统,谢谢你的使⽤!\n"); exit(-2);}}void main(){Menu();}。

c语言数据结构实现——一元多项式的基本运算

c语言数据结构实现——一元多项式的基本运算

c语言数据结构实现——一元多项式的基本运算在C语言中,一元多项式的表示与运算是常见的数据结构操作之一。

一元多项式由一系列具有相同变量的单项式组成,每个单项式由系数和指数组成。

本文将介绍如何使用C语言实现一元多项式的基本运算,包括多项式的创建、求和、差、乘积等操作。

首先,我们需要定义一个结构体来表示单项式。

每个单项式由一个系数和一个指数组成,我们可以将其定义如下:```cstruct term{float coefficient; // 系数int exponent; // 指数};typedef struct term Term;```接下来,我们可以定义一个结构体来表示一元多项式。

一元多项式由一系列单项式组成,可以使用一个动态数组来存储这些单项式。

```cstruct polynomial{Term* terms; // 单项式数组int num_terms; // 单项式数量};typedef struct polynomial Polynomial;```现在,我们可以开始实现一元多项式的基本运算了。

1. 创建一元多项式要创建一元多项式,我们需要输入每个单项式的系数和指数。

我们可以使用动态内存分配来创建一个适应输入的单项式数组。

```cPolynomial create_polynomial(){Polynomial poly;printf("请输入多项式的项数:");scanf("%d", &poly.num_terms);poly.terms = (Term*)malloc(poly.num_terms * sizeof(Term));for(int i = 0; i < poly.num_terms; i++){printf("请输入第%d个单项式的系数和指数:", i+1);scanf("%f %d", &poly.terms[i].coefficient, &poly.terms[i].exponent);}return poly;}```2. 求两个一元多项式的和两个一元多项式的和等于对应指数相同的单项式系数相加的结果。

c++一元多项式乘法

c++一元多项式乘法

在C++中,实现一元多项式的乘法涉及到创建一个表示多项式的数据结构,并实现一个函数来执行乘法操作。

以下是一个简单的示例,展示了如何使用结构体和链表来实现这个功能。

首先,我们可以定义一个结构体来表示多项式的一个项,包括系数和指数:cppstruct Term {int coefficient; // 系数int exponent; // 指数Term* next; // 指向下一个项的指针};然后,我们可以定义一个类来表示整个多项式,并实现乘法操作:cpp#include <iostream>using namespace std;struct Term {int coefficient;int exponent;Term* next;};class Polynomial {private:Term* head; // 多项式的第一个项public:Polynomial() : head(nullptr) {}// 向多项式中添加一个项void addTerm(int coefficient, int exponent) {Term* newTerm = new Term;newTerm->coefficient = coefficient;newTerm->exponent = exponent;newTerm->next = head;head = newTerm;}// 执行多项式乘法并返回结果多项式Polynomial multiply(const Polynomial& other) const {Polynomial result;Term* current1 = head;while (current1 != nullptr) {Term* current2 = other.head;while (current2 != nullptr) {int productCoefficient = current1->coefficient * current2->coefficient;int productExponent = current1->exponent + current2->exponent;result.addTerm(productCoefficient, productExponent);current2 = current2->next;}current1 = current1->next;}return result;}// 打印多项式到控制台void print() const {if (head == nullptr) {cout << "0";return;}bool isFirstTerm = true;Term* current = head;while (current != nullptr) {if (!isFirstTerm) {if (current->coefficient > 0) {cout << " + ";} else {cout << " - ";}} else {isFirstTerm = false;}if (current->exponent == 0) {cout << current->coefficient;} else if (current->exponent == 1) {cout << current->coefficient << "x";} else {cout << current->coefficient << "x^" << current->exponent;}current = current->next;}cout << endl;}};现在你可以使用上面的类来创建两个多项式并将它们相乘:cppint main() {Polynomial poly1, poly2, result;poly1.addTerm(3, 2); // 3x^2 + 4x + 5 的第一个项:3x^2poly1.addTerm(4, 1); // 3x^2 + 4x + 5 的第二个项:4x^1 或4x 或4(取决于你如何表示它)poly1.addTerm(5, 0); // 3x^2 + 4x + 5 的第三个项:5x^0 或5(常数项)poly2.addTerm(2, 3); // 2x^3 + 3x^2 - 1 的第一个项:2x^3 或2(取决于你如何表示它)// 注意这里是一个错误的输入,应为poly2.addTerm(2, 2); 表示x^2 项。

一元多项式相加减c语言

一元多项式相加减c语言

一元多项式相加减c语言一元多项式相加减是一个基础的多项式操作,其基本形式是用数组存储系数和指数(对于非零项来说),然后进行相应的加减操作。

以下是一个简单的C语言实现:```c#include <stdio.h>struct term {int coeff; // 系数int exp; // 指数};struct poly {int len; // 多项式的长度struct term *arr; // 存储多项式每一项的数组};// 创建多项式struct poly create_poly(int coeff[], int exp[]) {struct poly p;p.len = sizeof(coeff) / sizeof(coeff[0]);p.arr = (struct term*)malloc(p.len * sizeof(struct term));for (int i = 0; i < p.len; i++) {p.arr[i].coeff = coeff[i];p.arr[i].exp = exp[i];}return p;}// 多项式相加struct poly poly_add(struct poly p1, struct poly p2) {struct poly result = {0, NULL};int len = p1.len > p2.len ? p1.len : p2.len;result.arr = (struct term*)malloc(len * sizeof(struct term));for (int i = 0; i < len; i++) {if (i < p1.len && i < p2.len && p1.arr[i].exp == p2.arr[i].exp) {result.arr[i].coeff = p1.arr[i].coeff + p2.arr[i].coeff; } else if (i < p1.len) {result.arr[i].coeff = p1.arr[i].coeff;result.arr[i].exp = p1.arr[i].exp;} else { // i < p2.lenresult.arr[i].coeff = p2.arr[i].coeff;result.arr[i].exp = p2.arr[i].exp;}}result.len = len;return result;}// 多项式相减struct poly poly_sub(struct poly p1, struct poly p2) {struct poly result = {0, NULL};int len = p1.len > p2.len ? p1.len : p2.len;result.arr = (struct term*)malloc(len * sizeof(struct term));for (int i = 0; i < len; i++) {if (i < p1.len && i < p2.len && p1.arr[i].exp == p2.arr[i].exp) {result.arr[i].coeff = p1.arr[i].coeff - p2.arr[i].coeff; } else if (i < p1.len) { // 如果p1有该项,而p2没有,则直接复制p1的项到结果中。

一元多项式的计算代码

一元多项式的计算代码

一元多项式的计算代码:#include<iostream.h>#include <stdlib.h>#include <math.h>typedef struct Polynomial{float coe; //系数float exp;//指数struct Polynomial *next;}*Polyn,Polynomial;Polyn ma,mb;void Insert(Polyn p,Polyn h){if(p->coe==0) delete p;else{Polyn q1,q2;q1=h;q2=h->next;while(q2&&p->exp<q2->exp){q1=q2;q2=q2->next;}if(q2&&p->exp==q2->exp){q2->coe+=p->coe;delete p;if(!q2->coe){q1->next=q2->next;delete q2;}}else{p->next=q2;q1->next=p;}}}Polyn CreatePolyn(Polyn head,int m) {int i;Polyn p;p=head=new Polynomial;head->next=NULL;for(i=0;i<m;i++){p=new Polynomial;;cout<<"请输入第"<<i+1<<"项的系数:";cin>>p->coe;cout<<" 指数:";cin>>p->exp;Insert(p,head);}return head;}void DestroyPolyn(Polyn p){Polyn t;while(p!=NULL){t=p;p=p->next;delete t;}}void PrintPolyn(Polyn Pm){Polyn qa=Pm->next;int flag=1;if(!qa){cout<<"0";cout<<endl;return;}while (qa){if(qa->coe>0&&flag!=1) cout<<"+";if(qa->coe!=1&&qa->coe!=-1){cout<<qa->coe;if(qa->exp==1) cout<<"X";else if(qa->exp) cout<<"X^"<<qa->exp;}else{if(qa->coe==1){if(!qa->exp) cout<<"1";else if(qa->exp==1) cout<<"X";else cout<<"X^"<<qa->exp;}if(qa->coe==-1){if(!qa->exp) cout<<"-1";else if(qa->exp==1) cout<<"-X";else cout<<"-X^"<<qa->exp;}}qa=qa->next;flag++;}cout<<endl;}int compare(Polyn a,Polyn b){if(a&&b){if(!b||a->exp>b->exp) return 1;else if(!a||a->exp<b->exp) return -1;else return 0;}else if(!a&&b) return -1;else return 1;}Polyn AddPolyn(Polyn pa,Polyn pb){Polyn qa=pa->next;Polyn qb=pb->next;Polyn headc,hc,qc;hc=new Polynomial;hc->next=NULL;headc=hc;while(qa||qb){qc=new Polynomial;switch(compare(qa,qb)){case 1:{qc->coe=qa->coe;qc->exp=qa->exp;qa=qa->next;break;}case 0:{qc->coe=qa->coe+qb->coe;qc->exp=qa->exp;qa=qa->next;qb=qb->next;break;}case -1:{qc->coe=qb->coe;qc->exp=qb->exp;qb=qb->next;break;}}if(qc->coe!=0){qc->next=hc->next;hc->next=qc;hc=qc;}else delete qc;}return headc;}Polyn SubtractPolyn(Polyn pa,Polyn pb){Polyn h=pb;Polyn p=pb->next;Polyn pd;while(p){p->coe*=-1;p=p->next;pd=AddPolyn(pa,h);for(p=h->next;p;p=p->next)p->coe*=-1;return pd;}Polyn MultiplyPolyn(Polyn pa,Polyn pb){Polyn hf,pf;//Polyn qa=pa->next; //新建一个结点作为pa的后继结点Polyn qb=pb->next; //新建一个结点作为pb的后继结点hf=new Polynomial;hf->next=NULL;while(qa)//使用while循环,使得多项式的每项得以运算{qb=pb->next;while(qb){pf=new Polynomial;pf->coe=qa->coe*qb->coe;pf->exp=qa->exp+qb->exp;Insert(pf,hf);//调用插入函数,将新的结点插入到新建链表中,并合并同类项qb=qb->next;}qa=qa->next;}return hf;//返回所得链表的头指针}void DevicePolyn(Polyn pa,Polyn pb){Polyn quotient,remainder,temp1,temp2;Polyn qa=pa->next;Polyn qb=pb->next;quotient=new Polynomial; //建立头结点,存储商quotient->next=NULL;remainder=new Polynomial; //建立头结点,存储余数remainder->next=NULL;temp1=new Polynomial;temp1->next=NULL;temp2=new Polynomial;temp2->next=NULL;temp1=AddPolyn(temp1,pa);while(qa!=NULL&&qa->exp>=qb->exp)temp2->next=new Polynomial;temp2->next->coe=(qa->coe)/(qb->coe);temp2->next->exp=(qa->exp)-(qb->exp);Insert(temp2->next,quotient);pa=SubtractPolyn(pa,MultiplyPolyn(pb,temp2));qa=pa->next;temp2->next=NULL;}remainder=SubtractPolyn(temp1,MultiplyPolyn(quotient,pb));pb=temp1;cout<<endl<<"shang"<<endl;//printf("\t商:");PrintPolyn(quotient);cout<<"yushu"<<endl;//printf("\t余数:");PrintPolyn(remainder);}float ValuePolyn(Polyn head,float x){Polyn p;p=head->next;float result=0;while(p!=NULL){result+=(p->coe)*(float)pow(x,p->exp);p=p->next;}return result;}void desktop(){system("cls");cout<<endl<<endl<<endl<<" 一元多项式的计算"<<endl;cout<<"**********************************************"<<endl;cout<<" ** 1.输出多项式a和 b **"<<endl;cout<<" ** 2.建立多项式a+b **"<<endl;cout<<" ** 3.建立多项式a-b **"<<endl;cout<<" ** 4.建立多项式a*b **"<<endl;cout<<" ** 5.建立多项式a/b**"<<endl;cout<<" ** 6.计算多项式a的值**"<<endl;cout<<" ** 7.退出**"<<endl;cout<<"**********************************************"<<endl<<endl;cout<<" 执行操作:";}void input(){int m,n;//Polyn pa,pb;cout<<"请输入多项式a的项数:";cin>>m;ma=CreatePolyn(ma,m);cout<<endl;cout<<"请输入多项式b的项数:";cin>>n;mb=CreatePolyn(mb,n);}void main(){//int m,n;float x,result;char key;//Polyn pa,pb;cout<<endl<<endl<<endl<<endl<<" 欢迎您的使用!"<<endl;cout<<" 系统正在初始化数据,请稍后..."<<endl;_sleep(3*1000);system("cls");while(key){desktop();cin>>key;switch (key){case'1':input();cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);break;case'2':input();//pc=AddPolyn(pa,pb);cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);cout<<"多项式a+b:";PrintPolyn(AddPolyn(ma,mb));//DestroyPolyn(pc);break;case'3':input();//pd=SubtractPolyn(pa,pb);cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);cout<<"多项式a-b:";PrintPolyn(SubtractPolyn(ma,mb));//DestroyPolyn(pd);break;case'4':input();//pd=SubtractPolyn(pa,pb);cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);cout<<"多项式a*b:";PrintPolyn(MultiplyPolyn(ma,mb));//DestroyPolyn(pd);break;case'5':input();//pd=SubtractPolyn(pa,pb);cout<<"多项式a:";PrintPolyn(ma);cout<<"多项式b:";PrintPolyn(mb);cout<<"多项式a/b:";DevicePolyn(ma,mb);//DestroyPolyn(pd);break;case'6':input();cout<<"多项式a:";PrintPolyn(ma);cout<<"输入x的值:x=";cin>>x;result=ValuePolyn(ma,x);cout<<"多项式a的值:"<<result<<endl;break;case'7':DestroyPolyn(ma);DestroyPolyn(mb);exit(0);break;default:cout<<"Error!!!"<<endl;}cout<<endl<<endl;system("pause");}}。

一元多项式的计算的源程序

一元多项式的计算的源程序

#include<stdio.h>#include<stdlib.h>typedef struct{ float coef; //系数int expn; //指数}term;typedef struct LNode{ term data; //term多项式值struct LNode *next;}LNode,*LinkList;typedef LinkList polynomail;/*比较指数*/int cmp(term a,term b){ if(a.expn>b.expn) return 1;if(a.expn==b.expn) return 0;if(a.expn<b.expn) return -1;else exit(-2);}/*又小到大排列*/void arrange1(polynomail pa){ polynomail h=pa,p,q,r;if(pa==NULL) exit(-2);for(p=pa;p->next!=NULL;p=p->next); r=p;for(h=pa;h->next!=r;)//大的沉底{ for(p=h;p->next!=r&&p!=r;p=p->next)if(cmp(p->next->data,p->next->next->data)==1){ q=p->next->next;p->next->next=q->next;q->next=p->next;p->next=q;}r=p;//r指向参与比较的最后一个,不断向前移动} }/*由大到小排序*/void arrange2(polynomail pa){ polynomail h=pa,p,q,r;if(pa==NULL) exit(-2);for(p=pa;p->next!=NULL;p=p->next); r=p;for(h=pa;h->next!=r;)//小的沉底{ for(p=h;p->next!=r&&p!=r;p=p->next)if(cmp(p->next->next->data,p->next->data)==1){ q=p->next->next;p->next->next=q->next;q->next=p->next;p->next=q;}r=p;//r指向参与比较的最后一个,不断向前移动} }/*打印多项式,求项数*/int printpolyn(polynomail P){ int i;polynomail q;if(P==NULL) printf("无项!\n");else if(P->next==NULL) printf("Y=0\n");else{ printf("该多项式为Y=");q=P->next;i=1;if(q->data.coef!=0&&q->data.expn!=0){ printf("%.2fX^%d",q->data.coef,q->data.expn); i++; } if(q->data.expn==0&&q->data.coef!=0)printf("%.2f",q->data.coef);//打印第一项q=q->next;if(q==NULL){printf("\n");return 1;}while(1)//while中,打印剩下项中系数非零的项,{ if(q->data.coef!=0&&q->data.expn!=0){ if(q->data.coef>0) printf("+");printf("%.2fX^%d",q->data.coef,q->data.expn); i++;}if(q->data.expn==0&&q->data.coef!=0){ if(q->data.coef>0) printf("+");printf("%f",q->data.coef);}q=q->next;if(q==NULL){ printf("\n"); break; }}}return 1;}/*1、创建并初始化多项式链表*/polynomail creatpolyn(polynomail P,int m){ polynomail r,q,p,s,Q;int i;P=(LNode*)malloc(sizeof(LNode));r=P;for(i=0;i<m;i++){ s=(LNode*)malloc(sizeof(LNode));printf("请输入第%d项的系数和指数:",i+1);scanf("%f%d",&s->data.coef,&s->data.expn);r->next=s; r=s;}r->next=NULL;if(P->next->next!=NULL){ for(q=P->next;q!=NULL/*&&q->next!=NULL*/;q=q->next)//合并同类项for(p=q->next,r=q;p!=NULL;)if(q->data.expn==p->data.expn){ q->data.coef=q->data.coef+p->data.coef;r->next=p->next;Q=p;p=p->next;free(Q);}else{ r=r->next;p=p->next;}}return P;}/*2、两多项式相加*/polynomail addpolyn(polynomail pa,polynomail pb){ polynomail s,newp,q,p,r;int j;p=pa->next;q=pb->next;newp=(LNode*)malloc(sizeof(LNode));r=newp;while(p&&q){ s=(LNode*)malloc(sizeof(LNode));switch(cmp(p->data,q->data)){case -1: s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s; r=s;p=p->next;break;case 0: s->data.coef=p->data.coef+q->data.coef;if(s->data.coef!=0.0){ s->data.expn=p->data.expn;r->next=s;r=s;}p=p->next;q=q->next;break;case 1: s->data.coef=q->data.coef;s->data.expn=q->data.expn;r->next=s; r=s;q=q->next;break;}//switch}//whilewhile(p){ s=(LNode*)malloc(sizeof(LNode));s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s; r=s;p=p->next;}while(q){ s=(LNode*)malloc(sizeof(LNode));s->data.coef=q->data.coef;s->data.expn=q->data.expn;r->next=s; r=s;q=q->next;}r->next=NULL;for(q=newp->next;q->next!=NULL;q=q->next)//合并同类项for(p=q;p!=NULL&&p->next!=NULL;p=p->next)if(q->data.expn==p->next->data.expn){ q->data.coef=q->data.coef+p->next->data.coef;r=p->next;p->next=p->next->next;free(r);}printf("升序1 , 降序2\n");printf("选择:");scanf("%d",&j);if(j==1) arrange1(newp);else arrange2(newp);return newp;}/*3、两多项式相减*/polynomail subpolyn(polynomail pa,polynomail pb){ polynomail s,newp,q,p,r,Q; int j;p=pa->next;q=pb->next;newp=(LNode*)malloc(sizeof(LNode));r=newp;while(p&&q){ s=(LNode*)malloc(sizeof(LNode));switch(cmp(p->data,q->data)){case -1: s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s; r=s;p=p->next;break;case 0: s->data.coef=p->data.coef-q->data.coef;if(s->data.coef!=0.0){ s->data.expn=p->data.expn;r->next=s;r=s;}p=p->next;q=q->next;break;case 1: s->data.coef=-q->data.coef;s->data.expn=q->data.expn;r->next=s; r=s;q=q->next;break;}//switch}//whilewhile(p){ s=(LNode*)malloc(sizeof(LNode));s->data.coef=p->data.coef;s->data.expn=p->data.expn;r->next=s; r=s;p=p->next;}while(q){ s=(LNode*)malloc(sizeof(LNode));s->data.coef=-q->data.coef;s->data.expn=q->data.expn;r->next=s; r=s;q=q->next;}r->next=NULL;if(newp->next!=NULL&&newp->next->next!=NULL)//合并同类项{ for(q=newp->next;q!=NULL;q=q->next)for(p=q->next,r=q;p!=NULL;)if(q->data.expn==p->data.expn){ q->data.coef=q->data.coef+p->data.coef;r->next=p->next;Q=p;p=p->next;free(Q); }else{ r=r->next;p=p->next; }} printf("升序1 , 降序2\n");printf("选择:");scanf("%d",&j);if(j==1) arrange1(newp);else arrange2(newp);return newp;}/*4、销毁已建立的两个多项式*/void delpolyn(polynomail pa,polynomail pb) { polynomail p,q;p=pa;while(p!=NULL){ q=p;p=p->next;free(q);}p=pb;while(p!=NULL){ q=p;p=p->next;free(q);}printf("两个多项式已经销毁\n");}void main(){ polynomail pa=NULL,pb=NULL; polynomail p,q;polynomail addp=NULL,subp=NULL;int n,m;int sign='y';printf("1、创建两个一元多项式\n");printf("2、两多项式相加得一新多项式\n"); printf("3、两多项式相减得一新多项式\n"); printf("4、销毁已建立的两个多项式\n"); printf("5、退出\n");printf("\n");while(sign!='n'){ printf("请选择:");scanf("%d",&n);switch(n){case 1:{ printf("已建立两个一元多项式,请选择其他操作!"); break;}printf("请输入第一个多项式:\n");printf("要输入几项:");scanf("%d",&m);while(m==0){ printf("m不能为0,请重新输入m:");scanf("%d",&m);}pa=creatpolyn(pa,m);printpolyn(pa);printf("请输入第二个多项式:\n");printf("要输入几项:");scanf("%d",&m);pb=creatpolyn(pb,m);printpolyn(pb);break;case 2:if(pa==NULL){ printf("请先创建两个一元多项式!\n");break;}addp=addpolyn(pa,pb);printpolyn(addp);break;case 3:if(pa==NULL){ printf("请先创建两个一元多项式!\n");break;}subp=subpolyn(pa,pb);printpolyn(subp);break;case 4:if(pa==NULL){ printf("请先创建两个一元多项式!\n");break;}delpolyn(pa,pb);pa=pb=NULL;break;case 5:{ p=addp;while(p!=NULL){ q=p;p=p->next;free(q);}}if(subp!=NULL){ p=subp;while(p!=NULL){ q=p;p=p->next;free(q);}}exit(-2);}//switch}//while}。

C语言实现一元多项式相加(源代码)

C语言实现一元多项式相加(源代码)

C语言实现一元多项式相加(源代码)#include<stdio.h>#include<malloc.h>//动态申请空间的函数的头文件typedef struct node //定义节点类型{float coef; //多项式的系数int expn; //多项式的指数struct node * next; //结点指针域}PLOYList;void insert(PLOYList *head,PLOYList *input) //查找位置插入新链节的函数,且让输入的多项式呈降序排列{PLOYList *pre,*now;int signal=0;pre=head;if(pre->next==NULL) {pre->next=input;} //如果只有一个头结点,则把新结点直接连在后面else{now=pre->next;//如果不是只有一个头结点,则设置now指针while(signal==0){if(input->expn < now->expn){if(now->next==NULL){now->next=input;signal=1;}else{pre=now;now=pre->next;//始终让新输入的数的指数与最后一个结点中的数的指数比较,小于则插在其后面}}else if( input->expn > now->expn ){input->next=now;pre->next=input;signal=1;}//若新结点中指数比最后一个结点即now中的指数大,则插入now之前else//若指数相等则需合并为一个结点,若相加后指数为0则释放该结点{now->coef=now->coef+input->coef;signal=1;free(input);if(now->coef==0){pre->next=now->next;free(now);}}//else} //while}//else}//voidPLOYList *creat(char ch) //输入多项式{PLOYList *head,*input;float x;int y;head=(PLOYList *)malloc(sizeof(PLOYList)); //创建链表头head->next=NULL;scanf("%f %d",&x,&y);//实现用户输入的第一个项,包括其指数和系数while(x!=0)//当用户没有输入结束标志0时可一直输入多项式的项,且输入一个创建一个结点{input=(PLOYList *)malloc(sizeof(PLOYList)); //创建新链节input->coef=x;input->expn=y;input->next=NULL;insert(head,input); //每输入一项就将其排序,是的链表中多项式呈降序排列scanf("%f %d",&x,&y);}return head;}PLOYList *add(PLOYList *head,PLOYList *pre) //多项式相加,head为第一个多项式建立的链表表头,pre 为第二个多项式建立的链表表头{PLOYList *input;int flag=0;while(flag==0){if(pre->next==NULL)flag=1; //若该链表为空,则无需进行加法运算,跳出循环else{pre=pre->next;input=(PLOYList *)malloc(sizeof(PLOYList));//申请空间input->coef=pre->coef;input->expn=pre->expn;input->next=NULL;insert(head,input); // 把g(x)插入到f(x)中,相当于两者相加,结果保存于f(x) }}return head;}void print(PLOYList *fun) //输出多项式,fun指要输出的多项式链表的表头{PLOYList *printing;int flag=0;printing=fun->next;if(fun->next==NULL)//若为空表,则无需输出{printf("0\n");return;}while(flag==0){if(printing->coef>0&&fun->next!=printing)printf("+");if(printing->coef==1);else if(printing->coef==-1)printf("-");elseprintf("%f",printing->coef);if(printing->expn!=0) printf("x^%d",printing->expn);else if((printing->coef==1)||(printing->coef==-1))printf("1");if(printing->next==NULL)flag=1;elseprinting=printing->next;}printf("\n");}void start() //用户选择界面{printf(" * 1.两个一元多项式相加*\n");printf(" * 0.退出系统*\n");printf(" \n");printf(" 注:输入多项式格式为:系数1 指数1 系数2 指数2 ……,并以0 0 结束:\n");printf(" \n");printf(" 请选择操作: ");}void main(){PLOYList *f,*g,*pf,*hf,*p;int sign=-1;start();while(sign!=0){scanf("%d",&sign);switch(sign){case 0:break;case 1://多项式相加{printf(" 你选择的操作是多项式相加:\n");printf(" 请输入第一个多项式f(x):");f=creat('f');printf(" 第一个多项式为:f(x)=");print(f);printf(" 请输入第二个多项式g(x):");g=creat('g');printf(" 第二个多项式为:g(x)=");print(g);printf(" 结果为:F(x)=f(x)+g(x)=");f=add(f,g);print(f);printf("\n\n");printf(" 继续请选择相应操作,退出请按0. ");break;}}}}。

数据结构一元多项式的运算-无删减范文

数据结构一元多项式的运算-无删减范文

数据结构一元多项式的运算数据结构一元多项式的运算简介一元多项式是数学中常见的概念,用于表示一个变量的多项式表达式。

在计算机科学中,经常需要对一元多项式进行各种运算,如加法、减法、乘法等。

为了实现这些运算,可以使用数据结构来存储和操作一元多项式。

本文将介绍一元多项式的数据结构和常见的运算方法,并给出相应的代码示例。

数据结构一元多项式可以用链表来表示。

每个节点包含两个部分:系数(coefficient)和指数(exponent)。

系数表示该项的权重,指数表示该项的幂次。

链表的每个节点按照指数的升序排列。

以下是一个一元多项式的链表表示的示例:```markdown1.2x^2 + 3.7x^4 - 0.5x^3 -2.1x^1 + 4.0``````markdownNode 1: coefficient=1.2, exponent=2Node 2: coefficient=3.7, exponent=4Node 3: coefficient=-0.5, exponent=3Node 4: coefficient=-2.1, exponent=1Node 5: coefficient=4.0, exponent=0```运算方法加法运算两个一元多项式相加可以按照如下步骤进行:1. 遍历两个链表的节点,分别取出当前节点的系数和指数。

2. 如果两个节点的指数相等,将系数相加,并将其作为结果链表的节点。

3. 如果两个节点的指数不相等,将指数较小的节点插入结果链表,并继续遍历指数较大的节点。

4. 当其中一个链表遍历完后,直接将另一个链表的节点插入结果链表。

以下是加法运算的代码示例:```pythondef addPolynomials(p1, p2):result = Nonetl = Nonewhile p1 is not None and p2 is not None:if p1.exponent == p2.exponent:coef_sum = p1.coefficient + p2.coefficient if coef_sum != 0:node = Node(coef_sum, p1.exponent)if result is None:result = tl = nodeelse:tl.next = nodetl = nodep1 = p1.nextp2 = p2.nextelif p1.exponent > p2.exponent:node = Node(p1.coefficient, p1.exponent) if result is None:result = tl = nodeelse:tl.next = nodetl = nodep1 = p1.nextelse:node = Node(p2.coefficient, p2.exponent) if result is None:result = tl = nodeelse:tl.next = nodetl = nodep2 = p2.nextwhile p1 is not None:node = Node(p1.coefficient, p1.exponent)if result is None:result = tl = nodeelse:tl.next = nodetl = nodep1 = p1.nextwhile p2 is not None:node = Node(p2.coefficient, p2.exponent) if result is None:result = tl = nodeelse:tl.next = nodetl = nodep2 = p2.nextreturn result```减法运算减法运算可以看作加法运算的特殊情况,即将第二个多项式的系数取负数,再进行加法运算。

一元多项式相加C语言代码

一元多项式相加C语言代码

数据结构作业一元多项式相加C语言代码#include<stdio.h>#include<malloc.h>typedef struct node{int exp,coef;struct node *link;}PolyNode,*Polylinklist;Polylinklist Creat(int n){Polylinklist p,r=NULL,list=NULL;int coef,exp,i;for(i=1;i<=n;i++){scanf("%d %d",&coef,&exp);p=(Polylinklist)malloc(sizeof(PolyNode));p->coef=coef;p->exp=exp;p->link=NULL;if(list==NULL)list=p;elser->link=p;r=p;}return(list);}Polylinklist ATTACH(int coef,int exp,Polylinklist r){Polylinklist w;w=(Polylinklist)malloc(sizeof(PolyNode));w->exp=exp;w->coef=coef;r->link=w;return w;}Polylinklist PADD(Polylinklist a,Polylinklist b){Polylinklist c;Polylinklist r,p=a,q=b;int x;c=(Polylinklist)malloc(sizeof(PolyNode));r=c;while(p!=NULL&&q!=NULL)if(p->exp==q->exp){x=p->coef+q->coef;if(x!=0)r=ATTACH(x,q->exp,r);p=p->link;q=q->link;}else if(p->exp<q->exp){r=ATTACH(q->coef,q->exp,r);q=q->link;}else {r=ATTACH(p->coef,p->exp,r);p=p->link;}while(p!=NULL){r=ATTACH(p->coef,p->exp,r);p=p->link;}while(q!=NULL){r=ATTACH(q->coef,q->exp,r);q=q->link;}r->link=NULL;p=c;c=c->link;free(p);return c;}void Result(Polylinklist w){Polylinklist m;m=w;while(w==NULL){printf("0");break;}while(w!=NULL){if(w->exp==0)printf("%d",w->coef);elseprintf("%d*x^%d",w->coef,w->exp);w=w->link;while(w!=NULL){if(w->coef>0){if(w->exp==0)printf("+%d",w->coef);elseprintf("+%d*x^%d",w->coef,w->exp);}else{if(w->exp!=0)printf("%d*x^%d",w->coef,w->exp);elseprintf("%d",w->coef);}w=w->link;}}}void main(){Polylinklist c=NULL;PolyNode *Lengtha;PolyNode *Lengthb;int a1,b1;printf("Please input a's Length:");scanf("%d",&a1);Lengtha=Creat(a1);printf(" a=");Result(Lengtha);printf("\n");printf("Please input b's Length:");scanf("%d",&b1);Lengthb=Creat(b1);printf(" b=");Result(Lengthb);printf("\n");c=PADD(Lengtha,Lengthb);printf("\n");printf(" c=");Result(c);printf("\n");}。

一元多项式相加减 python

一元多项式相加减 python

在Python中,可以使用numpy库来进行一元多项式的相加减操作。

首先,需要导入numpy 库:
import numpy as np
然后,我们可以使用numpy的数组来表示一元多项式。

数组的每个元素表示一个系数,索引表示对应的指数。

例如,多项式2x^3 + 3x^2 - 4x + 1可以表示为一个数组[2, 3, -4, 1]。

现在,假设有两个多项式p1和p2,我们可以将它们表示为两个数组coeff1和coeff2。

然后,可以使用numpy的函数来进行相加减操作。

coeff1 = np.array([2, 3, -4, 1])
coeff2 = np.array([1, -2, 0, 5])
# 相加
sum_coeff = np.add(coeff1, coeff2)# 相减
diff_coeff = np.subtract(coeff1, coeff2)
print("相加结果:", sum_coeff)print("相减结果:", diff_coeff)
输出结果为:
相加结果: [3 1 -4 6]
相减结果: [ 1 5 -4 -4]
这样,我们就可以使用numpy库来进行一元多项式的相加减操作。

一元多项式运算c语言版

一元多项式运算c语言版

#include <stdio.h>#include <stdlib.h>#include <math.h>#define OK 1;#define ERROR 0;#define TURE 1;#define FALSE 0;#define MAXSIZE 100;typedef struct{float coef;int expn;}term,elemtype;typedef struct LNode{elemtype data;struct LNode *next;}List,*LinkList;typedef LinkList polynomail;int Initlist(LinkList *p){*p=(LinkList)malloc(sizeof(List));(*p)->next=NULL;return OK;}int cmp(term a,term b){if(a.expn<b.expn) return -1;else if(a.expn==b.expn) return 0;else return 1;}int LocateElem(LinkList *p,elemtype e,LinkList *q){ if(*p==NULL) return OVERFLOW;*q=(*p)->next;if((*q)==NULL){*q=*p;return ERROR;}while((*q)!=NULL){if(cmp(e,(*q)->data)==0) break;else *q=(*q)->next;}if((*q)!=NULL) return TURE;if((*q)==NULL) {return FALSE;}}int MakeNode(LinkList *s,elemtype e){if(!(*s)) return OVERFLOW;(*s)->data=e;(*s)->next=NULL;return OK;}void InsFirst(LinkList *q,LinkList *s){ (*s)->next=(*q)->next;(*q)->next=*s;}int DelFirst(LinkList *q,LinkList *s){LinkList p;p=*s;(*q)->next=p->next;return OK;}void FreeNode(LinkList *p){LinkList q;q=*p;free(q);}LinkList NextPos(LinkList *p,LinkList *q){ return (*q)->next;}void SetCurElem(polynomail *p,float m){ ((*p)->data).coef=m;}int ListEmpty(LinkList p){if(p->next==NULL) return TURE;if(p->next!=NULL) return FALSE;}void Append(LinkList *p,LinkList *s){ LinkList q;q=*p;while(q->next!=NULL) q=q->next;q->next=*s;}void SortPolyn(polynomail *p){polynomail q,s,t,m,n,x,y,x1,x2;q=*p;Initlist(&m);m->data.coef=0.0;m->data.expn=-1;n=m;s=q;y=s->next;t=y->next;x=q->next;x1=s;x2=t;while(x2!=NULL){while(t!=NULL){if(cmp(x->data,y->data)<=0){s=s->next;y=y->next;t=t->next;}else {x=y;x1=s;x2=t;}}if(cmp(x->data,y->data)>0){x=y;x1=s;x2=t;}x1->next=x2;n->next=x;x->next=NULL;n=n->next;x1=q;x=q->next;x2=x->next;s=x1;y=x;t=x2;}n->next=x;x->next=NULL;q->next=NULL;free(q);*p=m;}void CreatePolyn(polynomail *p,int m){ polynomail h,q[100],s[100],x;int i;elemtype e;elemtype t[100];h=*p;e.coef=0.0;e.expn=-1;h->data=e;h->next=NULL;q[0]=h;Initlist(&x);for(i=0;i<m;i++){Initlist(&s[i]);printf("please input the coef: the expn:\n");scanf("%f%d",&t[i].coef,&t[i].expn);if(!LocateElem(p,t[i],&x)){if(MakeNode(&s[i],t[i])) InsFirst(&q[i],&s[i]);q[i+1]=s[i];}else q[i+1]=q[i];}SortPolyn(p);}void PrintPolyn(polynomail q){q=q->next;if((q->data).coef>0.0&&(q->data).expn!=0)printf("%.4f*x^(%d)",(q->data).coef,(q->data).expn);else if((q->data).expn==0) printf("%.4f",(q->data).coef);else printf("%.4f*x^(%d)",(q->data).coef,(q->data).expn);q=q->next;while(q!=NULL){if((q->data).coef>0.0&&(q->data).expn!=0)printf("+%.4f*x^(%d)",(q->data).coef,(q->data).expn);else if((q->data).expn==0&&(q->data).coef>0.0) printf("+%.4f",(q->data).coef);else if((q->data).expn==0&&(q->data).coef<0.0) printf("%.4f",(q->data).coef);else printf("%.4f*x^(%d)",(q->data).coef,(q->data).expn);q=q->next;}printf("\n");}void AddPolyn(polynomail *pa,polynomail *pb){polynomail ha,hb,qa,qb;ha=*pa;hb=*pb;qa=NextPos(pa,&ha);qb=NextPos(pb,&hb);elemtype a,b;float sum;while(qa&&qb){a=qa->data;b=qb->data;switch(cmp(a,b)){case -1:ha=qa;qa=NextPos(pa,&qa);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=NextPos(pb,&hb);qa=NextPos(pa,&ha);break;case 1:DelFirst(&hb,&qb);InsFirst(&ha,&qb);qb=NextPos(pb,&hb);ha=NextPos(pa,&ha);break;}}if(!ListEmpty(*pb)) Append(pa,&qb);FreeNode(&hb);}void SubStractPolyn(polynomail *pa,polynomail *pb){ polynomail ha,hb,qa,qb;ha=*pa;hb=*pb;qa=NextPos(pa,&ha);qb=NextPos(pb,&hb);elemtype a,b;float sum;while(qb!=NULL){qb->data.coef=-qb->data.coef;qb=qb->next;}qb=NextPos(pb,&hb);while(qa&&qb){a=qa->data;b=qb->data;switch(cmp(a,b)){case -1:ha=qa;qa=NextPos(pa,&qa);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=NextPos(pb,&hb);qa=NextPos(pa,&ha);break;case 1:DelFirst(&hb,&qb);InsFirst(&ha,&qb);qb=NextPos(pb,&hb);ha=NextPos(pa,&ha);break;}}if(!ListEmpty(*pb)) Append(pa,&qb);FreeNode(&hb);}double EvaluatePolyn(polynomail p,float x){ polynomail q;double y=0.0;q=p->next;while(q!=NULL){y+=(q->data.coef)*pow(x,q->data.expn);q=q->next;}return y;}void DestroyPolyn(polynomail *p){free(*p);}void ClearPolyn(polynomail *p){polynomail q;q=*p;while(*p!=NULL){*p=(*p)->next;free(q);q=*p;}}int InsertPolyn(polynomail *q,polynomail *s){while((*q)->next!=NULL){switch(cmp((*q)->next->data,(*s)->data)){case -1:*q=(*q)->next;break;case 0:(*q)->data.coef+=(*s)->data.coef;return OK;case 1:(*s)->next=(*q)->next;(*q)->next=*s;return OK;}(*q)->next=*s;return OK;}}int DeletePolyn(polynomail *q,elemtype x){polynomail t;while((*q)->next!=NULL){if(((*q)->next->data.coef==x.coef)&&((*q)->next->data.expn==x.expn)){ t=(*q)->next;(*q)->next=t->next;free(t);return OK;}else *q=(*q)->next;}if((*q)->next==NULL) return FALSE;}int ChangePolyn(polynomail *p,elemtype x,elemtype y){ polynomail s;MakeNode(&s,y);DeletePolyn(p,x);InsertPolyn(p,&s);return OK;}void DifferentialPolyn(polynomail *p,int n){polynomail q,s;int i;q=*p;s=q->next;for(i=0;i<n;i++){while(s!=NULL){if(s->data.expn!=0){s->data.coef*=s->data.expn;s->data.expn--;q=q->next;s=q->next;}else {q->next=s->next;s=q->next;}}q=*p;s=q->next;}}void MultiplyPolyn(polynomail *pa,polynomail *pb){ int n=0,i;polynomail s,t,q[100];s=*pb;while(s->next!=NULL){n++;s=s->next;}s=(*pb)->next;for(i=0;i<n;i++){q[i]=*pa;t=q[i]->next;while(t!=NULL){t->data.coef*=s->data.coef;t->data.expn+=s->data.expn;t=t->next;}s=s->next;}for(i=1;i<n;i++){AddPolyn(&q[0],&q[i]);}*pa=q[0];}void IntegratePolyn(polynomail *p){polynomail q,s;q=*p;s=q->next;while(s!=NULL){s->data.coef/=s->data.expn+1;s->data.expn++;q=q->next;s=q->next;}}double DefiniteIntegralPolyn(polynomail *p,float x,float y){ IntegratePolyn(p);double f;f=EvaluatePolyn(*p,y)-EvaluatePolyn(*p,x);return f;}void InvolutionPolyn(polynomail *p,int n){polynomail q[100];int i;for(i=0;i<n;i++) q[i]=*p;for(i=1;i<n;i++) MultiplyPolyn(&q[0],&q[i]);*p=q[0];}void DivisionPolyn(polynomail *p,polynomail *q){ polynomail s,t,x,q1,q2,rest;int n,m;s=*p;t=*q;while(s->next!=NULL) s=s->next;while(t->next!=NULL) t=t->next;m=s->data.expn;n=t->data.expn;Initlist(&x);x->data.coef=0.0;x->data.expn=-1;Initlist(&rest);rest->data.coef=0.0;rest->data.expn=-1;while(m!=0&&m>=n){Initlist(&q1);q1->data.coef=0.0;q1->data.expn=-1;AddPolyn(&q1,q);x->data.coef=s->data.coef/(t->data.coef);x->data.expn=s->data.expn-t->data.expn;InsFirst(&rest,&x);q2=(*q)->next;while(q2!=NULL){q2->data.coef*=x->data.coef;q2->data.expn+=x->data.expn;q2=q2->next;}SubStractPolyn(p,&q1);DestroyPolyn(&q1);s=*p;t=*q;while(s->next!=NULL) s=s->next;while(t->next!=NULL) t=t->next;m=s->data.expn;n=t->data.expn;}q1=*p;q2=*q;*p=rest;*q=q1;DestroyPolyn(&q2);}void main(){polynomail p,q,p1,q1;int m,n,t;Initlist(&p);Initlist(&q);Initlist(&p1);Initlist(&q1);printf("please input the length of polynomail:\n");scanf("%d",&m);CreatePolyn(&p,m);CreatePolyn(&q,m);PrintPolyn(p);PrintPolyn(q);AddPolyn(&p,&q);PrintPolyn(p);printf("please input n= \n");scanf("%d",&n);DifferentialPolyn(&p,n);PrintPolyn(p);IntegratePolyn(&p);PrintPolyn(p);printf("please input the length of polynomail:\n");scanf("%d",&t);CreatePolyn(&p1,t);CreatePolyn(&q1,t);PrintPolyn(p1);PrintPolyn(q1);DivisionPolyn(&p1,&q1);PrintPolyn(p1);PrintPolyn(q1);printf("%lf\n",EvaluatePolyn(p,3.0));}。

一元多项式的乘法与加法运算c 语言

一元多项式的乘法与加法运算c 语言

一元多项式的乘法与加法运算c 语言一元多项式是高中数学中一个重要的概念,具有广泛的应用。

在c 语言中,我们可以通过数组和循环来实现一元多项式的乘法和加法运算。

一、一元多项式的表示方式在c语言中,一元多项式通常使用数组来表示。

数组的下标表示该项的指数,数组元素表示该项的系数。

例如,一个一元三次多项式5x^3-3x^2+7x-9在c语言中表示为:int poly[4] = { -9, 7, -3, 5 };其中poly[0]表示常数项系数-9,poly[1]表示x的系数7,以此类推。

二、一元多项式的乘法运算对于两个多项式P(x)和Q(x),它们的乘积为:(PQ)(x) = ∑(i+j=k) Pi × Qj × xk其中i、j、k分别表示P、Q、PQ中每一项的指数,Pi、Qj表示对应项的系数,xk表示PQ中对应指数的项。

通过这个公式,我们可以使用数组实现一元多项式的乘法运算。

具体实现如下:void poly_mul(int *p, int *q, int *s, int m, int n) {int i, j;memset(s, 0, sizeof(int) * (m + n)); // 将结果数组s初始化为0for (i = 0; i <= m; i++) {for (j = 0; j <= n; j++) {s[i + j] += p[i] * q[j]; // 对应项相乘并累加到结果数组}}}该函数参数分别为两个乘数的数组p、q,结果数组s,以及每个乘数的项数m、n。

在函数内部,我们首先将结果数组s初始化为0,然后使用两个循环遍历乘数数组,对应项相乘并累加到结果数组中。

最后得到的结果数组就是乘积多项式。

三、一元多项式的加法运算对于两个多项式P(x)和Q(x),它们的和为:(P+Q)(x) = ∑Pi × xi + ∑Qj × xj其中Pi、Qj分别表示对应项的系数,xi、xj表示对应项的指数。

一元多项式相加(C语言实现)

一元多项式相加(C语言实现)

//一元多项式相加//#define ERROR 0#include<stdio.h>#include<malloc.h>//#include<stdlib.h>typedef struct poly{ float coef;int expn;struct poly *next;}poly,*Linkpoly;void main(){Linkpoly creatpoly();Linkpoly addpoly(Linkpoly La,Linkpoly Lb);void printpoly(Linkpoly L);Linkpoly La,Lb;La=creatpoly();Lb=creatpoly();printf("多项式1:\n");printpoly(La);printf("\n");printf("多项式2:\n");printpoly(Lb);printf("\n");addpoly(La,Lb);printf("相加后的多项式:\n");printpoly(La);}Linkpoly creatpoly()//创建链表,输入多项式{int i,n;Linkpoly p,L;printf("请输入多项式项数:\n");scanf("%d",&n);printf("逆序输入多项式: <系数,指数>\n");L=(Linkpoly)malloc(sizeof(poly));//头结点L->next=NULL;for(i=n;i>0;--i){p=(Linkpoly)malloc(sizeof(poly));scanf("%f,%d",&p->coef,&p->expn);p->next=L->next; L->next=p;}return L;}Linkpoly addpoly(Linkpoly La,Linkpoly Lb)//多项式相加{Linkpoly p,r,q,s,s1,s2;r=La;//r指向p的前驱p=La->next;q=Lb->next;while(p&&q){if(p->expn<q->expn) { r=p;p=p->next;continue;}if(p->expn>q->expn) {s=q->next;q->next=r->next;r->next=q;q=s;r=r->next;continue;}if(p->expn==q->expn&&(p->coef+q->coef)!=0) { p->coef=p->coef+q->coef;r=p;p=p->next;q=q->next;continue;} if(p->expn==q->expn&&(p->coef+q->coef)==0) { s1=p;s2=q;p=r;p->next=p->next->next;p=p->next;q=q->next;continue;free(s1);free(s2);}}if(q){p=r;p->next=q;p=p->next;// free(p);}// return La;}void printpoly(Linkpoly L){Linkpoly p;p=L->next;while(p){printf("<%0.2f,%d> ",p->coef,p->expn);p=p->next;}}。

顺序链式一元多项式加法,减法,乘法运算的实现

顺序链式一元多项式加法,减法,乘法运算的实现

顺序链式一元多项式加法,减法,乘法运算的实现1.1设计内容及要求 1)设计内容(1)使用顺序存储结构实现多项式加、减、乘运算。

例如:10321058)(2456+-+-+=x x x x x x f ,x x x x x x g +--+=23451020107)(求和结果:102220128)()(2356++-+=+x x x x x g x f (2)使用链式存储结构实现多项式加、减、乘运算,10305100)(1050100+-+=x x x x f ,x x x x x x g 320405150)(10205090+++-=求和结果:1031040150100)()(102090100++-++=+x x x x x x g x f 2)设计要求(1)用C 语言编程实现上述实验内容中的结构定义和算法。

(2)要有main()函数,并且在main()函数中使用检测数据调用上述算法。

(3)用switch 语句设计如下选择式菜单。

***************数据结构综合性实验**************** *******一、多项式的加法、减法、乘法运算********** ******* 1.多项式创建********** ******* 2.多项式相加********** ******* 3.多项式相减***************** 4.多项式相乘 ********** ******* 5.清空多项式 ********** ******* 0.退出系统 ********** ******* 请选择(0—5) ********** **************************************************请选择(0-5):1.2数据结构设计根据下面给出的存储结构定义:#define MAXSIZE 20 //定义线性表最大容量//定义多项式项数据类型typedef struct{float coef; //系数int expn; //指数}term,elemType;typedef struct{term terms[MAXSIZE]; //线性表中数组元素int last; //指向线性表中最后一个元素位置}SeqList;typedef SeqList polynomial;1.3基本操作函数说明polynomial*Init_Polynomial();//初始化空的多项式int PloynStatus(polynomial*p)//判断多项式的状态int Location_Element(polynomial*p,term x)在多项式p中查找与x项指数相同的项是否存在int Insert_ElementByOrder(polynomial*p,term x)//在多项式p中插入一个指数项xint CreatePolyn(polynomial*P,int m)//输入m项系数和指数,建立表示一元多项式的有序表p char compare(term term1,term term2)//比较指数项term1和指数项term2polynomial*addPloyn(polynomial*p1,polynomial*p2)//将多项式p1和多项式p2相加,生成一个新的多项式polynomial*subStractPloyn(polynomial*p1,polynomial*p2) //多项式p1和多项式p2相减,生成一个新的多项式polynomial*mulitPloyn(polynomial*p1,polynomial*p2) //多项式p1和多项式p2相乘,生成一个新的多项式void printPloyn(polynomial*p)//输出在顺序存储结构的多项式p1.4程序源代码#include#include#include#define NULL 0#define MAXSIZE 20typedef struct{float coef;int expn;}term,elemType;typedef struct{term terms[MAXSIZE];int last;}SeqList;typedef SeqList polynomial; void printPloyn(polynomial*p); int PloynStatus(polynomial*p) {if(p==NULL){return -1;}else if(p->last==-1){return 0;}else{return 1;}}polynomial*Init_Polynomial() {polynomial*P;P=new polynomial;if(P!=NULL){P->last=-1;return P;}else{return NULL;}}void Reset_Polynomial(polynomial*p){if(PloynStatus(p)==1){p->last=-1;}}int Location_Element(polynomial*p,term x){int i=0;if(PloynStatus(p)==-1)return 0;while(i<=p->last && p->terms[i].expn!=x.expn) { i++;}if(i>p->last){return 0;}else{return 1;}}int Insert_ElementByOrder(polynomial*p,term x) { int j;if(PloynStatus(p)==-1)return 0;if(p->last==MAXSIZE-1){cout<<"The polym is full!"<<endl;< p=""> return 0;}j=p->last;while(p->terms[j].expn=0){p->terms[j+1]=p->terms[j];j--;}p->terms[j+1]=x;p->last++;return 1;}int CreatePolyn(polynomial*P,int m){float coef;int expn;term x;if(PloynStatus(P)==-1)return 0;if(m>MAXSIZE){printf("顺序表溢出\n");return 0;}else{printf("请依次输入%d对系数和指数...\n",m); for(int i=0;i<m;i++)< p="">scanf("%f%d",&coef,&expn);x.coef=coef;x.expn=expn;if(!Location_Element(P,x)){Insert_ElementByOrder(P,x);}}}return 1;}char compare(term term1,term term2) { if(term1.expn>term2.expn){return'>';}else if(term1.expn<term2.expn)< p=""> {return'<';}{return'=';}}polynomial*addPloyn(polynomial*p1,polynomial*p2) { int i,j,k;i=0;j=0;k=0;if((PloynStatus(p1)==-1)||(PloynStatus(p2)==-1)) { return NULL;}polynomial*p3=Init_Polynomial();while(i<=p1->last && j<=p2->last){switch(compare(p1->terms[i],p2->terms[j])){case'>':p3->terms[k++]=p1->terms[i++];p3->last++;break;case'<':p3->terms[k++]=p2->terms[j++];p3->last++;break;case'=':if(p1->terms[i].coef+p2->terms[j].coef!=0){p3->terms[k].coef=p1->terms[i].coef+p2->terms[j].coef;p3->terms[k].expn=p1->terms[i].expn;k++;p3->last++;}i++;j++;}}while(i<=p1->last){p3->terms[k++]=p1->terms[i++];p3->last++;}return p3;}polynomial*subStractPloyn(polynomial*p1,polynomial*p2) { int i;i=0;if((PloynStatus(p1)!=1)||(PloynStatus(p2)!=1)){return NULL;}polynomial*p3=Init_Polynomial();p3->last=p2->last;for(i=0;i<=p2->last;i++){p3->terms[i].coef=-p2->terms[i].coef;p3->terms[i].expn=p2->terms[i].expn;}p3=addPloyn(p1,p3);return p3;}polynomial*mulitPloyn(polynomial*p1,polynomial*p2){int i;int j;int k;i=0;if((PloynStatus(p1)!=1)||(PloynStatus(p2)!=1)){return NULL;}polynomial*p3=Init_Polynomial();polynomial**p=new polynomial*[p2->last+1];for(i=0;i<=p2->last;i++){for(k=0;k<=p2->last;k++){p[k]=Init_Polynomial();p[k]->last=p1->last;for(j=0;j<=p1->last;j++){p[k]->terms[j].coef=p1->terms[j].coef*p2->terms[k].coef; p[k]->terms[j].expn=p1->terms[j].expn+p2->terms[k].expn; }p3=addPloyn(p3,p[k]);}}return p3;}void printPloyn(polynomial*p){int i;for(i=0;i<=p->last;i++){if(p->terms[i].coef>0 && i>0)cout<<"+"<terms[i].coef;elsecout<terms[i].coef;cout<<"x^"<terms[i].expn;}cout<<endl;< p="">}void menu(){cout<<"\t\t*******数据结构综合性实验*********"<<endl;< p="">cout<<"\t\t***一、多项式的加、减、乘法运算***"<<endl;< p="">cout<<"\t\t******* 1.多项式创建*********"<<endl;< p="">cout<<"\t\t******* 2.多项式相加*********"<<endl;< p="">cout<<"\t\t******* 3.多项式相减*********"<<endl;< p="">cout<<"\t\t******* 4.多项式相乘*********"<<endl;< p="">cout<<"\t\t******* 5.清空多项式*********"<<endl;< p="">cout<<"\t\t******* 0.退出系统*********"<<endl;< p="">cout<<"\t\t****** 请选择(0-5) ********"<<endl;< p="">cout<<"\t\t***********************************"<<="">void main(){int sel;polynomial*p1=NULL;polynomial*p2=NULL;polynomial*p3=NULL;while(1){menu();cout<<"\t\t*请选择(0-5):";cin>>sel;switch(sel){case 1:p1=Init_Polynomial();p2=Init_Polynomial();int m;printf("请输入第一个多项式的项数:\n"); scanf("%d",&m); CreatePolyn(p1,m);printf("第一个多项式的表达式为p1="); printPloyn(p1); printf("请输入第二个多项式的项数:\n"); scanf("%d",&m); CreatePolyn(p2,m);printf("第二个多项式的表达式为p2="); printPloyn(p2); break;case 2:printf("p1+p2=");if((p3=subStractPloyn(p1,p2))!=NULL) printPloyn(p3); break;case 3:printf("\np1-p2=");if((p3=subStractPloyn(p1,p2))!=NULL)printPloyn(p3);break;case 4:printf("\np1*p2=");if((p3=mulitPloyn(p1,p2))!=NULL) printPloyn(p3);case 5:Reset_Polynomial(p1);Reset_Polynomial(p2);Reset_Polynomial(p3);break;case 0:return;}}return;}1.5程序执行结果</endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></term2.expn)<> </m;i++)<></endl;<>。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

N个一元多项式的加减乘除执行代码#include <iostream>using namespace std;struct Node{public:int coe;int exp;Node *next;};class List{public:Node *CreatList(Node *Head);Node *InsertNode(Node *Head,Node *Ptr);Node *AddList(Node *Head1,Node *Head2);Node *SubList(Node *Head1,Node *Head2);Node *MulList(Node *Head1,Node *Head2);};Node *List::CreatList(Node *Head){int num=0;Node *Ptr=new Node;Ptr->next=NULL;cout<<"请输入一元多项式的项数:";cin>>num;for (int i=1;i<=num;i++){cout<<"请输入多项式的第"<<i<<"项"<<endl;cin>>Ptr->coe;cin>>Ptr->exp;if (Ptr->coe==0){}else{InsertNode(Head,Ptr);}Ptr=new Node;Ptr->next=NULL;}return Head;}Node *List::InsertNode(Node *Head,Node *Ptr){Node *PrePos=new Node;Node *NowPos=Head->next;if (NowPos==NULL){Head->next=Ptr;Ptr->next=NULL;}else{PrePos=Head;while(NowPos!=NULL){if (Ptr->exp>NowPos->exp){PrePos->next=Ptr;Ptr->next=NowPos;break;}else if(Ptr->exp<NowPos->exp){PrePos=NowPos;NowPos=NowPos->next;}else{if ((Ptr->coe+NowPos->coe)==0){PrePos->next=NowPos->next;delete NowPos;break;}else{NowPos->coe=Ptr->coe+NowPos->coe;break;}}if (NowPos==NULL){Ptr->next=NULL;}}}return Head;}Node *List::AddList(Node *Head1,Node *Head2){Node *NewHead=new Node;Node *Ptr=new Node;Ptr->next=NULL;NewHead->next=NULL;Node *Ptr1=Head1->next;Node *Ptr2=Head2->next;if (Ptr1==NULL){if (Ptr2==NULL){}else{while(Ptr2!=NULL){Ptr->coe=Ptr2->coe;Ptr->exp=Ptr2->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr->next=NULL;Ptr2=Ptr2->next;}}}else{if (Ptr2==NULL){while(Ptr1!=NULL){Ptr->coe=Ptr1->coe;Ptr->exp=Ptr1->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr1=Ptr1->next;}}else{while(Ptr1!=NULL){Ptr->coe=Ptr1->coe;Ptr->exp=Ptr1->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr->next=NULL;Ptr1=Ptr1->next;}while(Ptr2!=NULL){Ptr->coe=Ptr2->coe;Ptr->exp=Ptr2->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr->next=NULL;Ptr2=Ptr2->next;}}}return NewHead;}Node *List::SubList(Node *Head1,Node *Head2){Node *NewHead=new Node;Node *Ptr=new Node;Ptr->next=NULL;NewHead->next=NULL;Node *Ptr1=Head1->next;Node *Ptr2=Head2->next;if (Ptr1==NULL){if (Ptr2==NULL){}else{while(Ptr2!=NULL){Ptr->coe=-Ptr2->coe;Ptr->exp=Ptr2->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr->next=NULL;Ptr2=Ptr2->next;}}}else{if (Ptr2==NULL){while(Ptr1!=NULL){Ptr->coe=Ptr1->coe;Ptr->exp=Ptr1->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr->next=NULL;Ptr1=Ptr1->next;}}else{while(Ptr1!=NULL){Ptr->coe=Ptr1->coe;Ptr->exp=Ptr1->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr->next=NULL;Ptr1=Ptr1->next;}while(Ptr2!=NULL){Ptr->coe=-Ptr2->coe;Ptr->exp=Ptr2->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr->next=NULL;Ptr2=Ptr2->next;}}}return NewHead;}Node *List::MulList(Node *Head1,Node *Head2){Node *NewHead=new Node;Node *Ptr=new Node;Ptr->next=NULL;NewHead->next=NULL;Node *Ptr1=Head1->next;Node *Ptr2=Head2->next;if (Ptr1==NULL){}else{if (Ptr2==NULL){}else{while(Ptr1!=NULL){while(Ptr2!=NULL){Ptr->coe=Ptr1->coe*Ptr2->coe;Ptr->exp=Ptr1->exp+Ptr2->exp;NewHead=InsertNode(NewHead,Ptr);Ptr=new Node;Ptr->next=NULL;Ptr2=Ptr2->next;}Ptr=new Node;Ptr->next=NULL;Ptr2=Head2->next;Ptr1=Ptr1->next;}}}return NewHead;}void ShowList(Node *Head){Node *Ptr=Head->next;if (Ptr==NULL){cout<<0;}else{if (Ptr->coe>0){if (Ptr->coe==1){if (Ptr->exp==0){cout<<Ptr->coe;}else if(Ptr->exp==1){cout<<"x";}else{cout<<"x^"<<Ptr->exp;}}else{if (Ptr->exp==0){cout<<Ptr->coe;}else if(Ptr->exp==1){cout<<Ptr->coe<<"x";}else{cout<<Ptr->coe<<"x^"<<Ptr->exp;}}}else{if (Ptr->coe==-1){if (Ptr->exp==0){cout<<Ptr->coe;}else if(Ptr->exp==1){cout<<"-"<<"x";}else{cout<<"-"<<"x^"<<Ptr->exp;}}else{if (Ptr->exp==0){cout<<Ptr->coe;}else if(Ptr->exp==1){cout<<Ptr->coe<<"x";}else{cout<<Ptr->coe<<"x^"<<Ptr->exp;}}}Ptr=Ptr->next;while (Ptr!=NULL){if (Ptr->coe>0){if (Ptr->coe==1){if (Ptr->exp==0){cout<<"+"<<Ptr->coe;}else if(Ptr->exp==1){cout<<"+"<<"x";}else{cout<<"+"<<"x^"<<Ptr->exp;}}else{if (Ptr->exp==0){cout<<"+"<<Ptr->coe;}else if(Ptr->exp==1){cout<<"+"<<Ptr->coe<<"x";}else{cout<<"+"<<Ptr->coe<<"x^"<<Ptr->exp;}}}else{if (Ptr->coe==-1){if (Ptr->exp==0){cout<<Ptr->coe;}else if(Ptr->exp==1){cout<<"-"<<"x";}else{cout<<"x^"<<Ptr->exp;}}else{if (Ptr->exp==0){cout<<Ptr->coe;}else if(Ptr->exp==1){cout<<Ptr->coe<<"x";}else{cout<<Ptr->coe<<"x^"<<Ptr->exp;}}}Ptr=Ptr->next;}}}void main(){Node *head1=new Node;Node *head2=new Node;head1->next=NULL;head2->next=NULL;List list1;List list2;List NewList;head1=list1.CreatList(head1);head2=list2.CreatList(head2);cout<<"f(x)=";ShowList(head1);cout<<endl;cout<<"g(x)=";ShowList(head2);cout<<endl;cout<<"两式相加的结果"<<endl;cout<<"F(x)=";ShowList(NewList.AddList(head1,head2));cout<<endl;cout<<"两式相减的结果"<<endl;cout<<"F(x)=";ShowList(NewList.SubList(head1,head2)); cout<<endl;cout<<"两式相乘的结果"<<endl;cout<<"F(x)=";ShowList(NewList.MulList(head1,head2)); cout<<endl;system("pause");。

相关文档
最新文档