一元多项式加减乘除源代码

合集下载

一元多项式的运算C程序设计

一元多项式的运算C程序设计
Байду номын сангаас
69 printf("\n"); 70 } 71 void PolynAdd(pPOLYN pa, pPOLYN pb) //多项式相加 pa=pa+pb 72 { 73 pPOLYN pa1, pb1, pc1, p; 74 pa1=pa‐>next; //指向被加链表的第1个有效项 75 pb1=pa‐>next; //指向加链表的第1个有效项 76 pc1=pa; //指向被加链表 77 pc1‐>next=NULL; 78 pc1‐>expn=0; // 清空多项式项目数量 79 while(pa1 && pb1) //两个多项式都未结束 80 { 81 if(pa1‐>expn > pb1‐>expn) // pa1指数大于pb1指数 82 { 83 pc1‐>next=pa1; // 将pa1加入结果链表中 84 pc1=pa1; 85 pa1=pa1‐>next; //处理pa1中的下一项 86 pc1‐>next=NULL; 87 } 88 else 89 { //pa1指数等于pb1指数,进行系数项加 90 pa1‐>coef+=pb1‐>coef; //累加素数 91 if(pa1‐>coef!=0) //若系数不为0 92 { 93 pc1‐>next=pa1; //将相加结果添加到结果链表中 94 pc1=pa1; 95 pa1=pa1‐>next; //处理pa1中的下一项 96 pc1‐>next=NULL; 97 p=pb1; 98 pb1=pb1‐>next; //处理pb1中的下一项 99 free(p); 100 } 101 else // 系数为0, 则不记录该项 102 { 103 p=pa1; // 用临时指针指向pa1中的该项 104 pa1=pa1‐>next; //从链表中删除该项 105 free(p); //释放该项占用内存 106 p=pb1; //用临时指针指向pb1中的该项 107 pb1=pb1‐>next; //从链表中删除该项 108 free(p); // 释放该项占用内存

一元多项式的各种运算实现(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;

一元多项式运算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));}。

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

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

⼀元多项式加法、减法、乘法实现源代码////链接程序:#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();}。

一元多项式加减乘除运算

一元多项式加减乘除运算

#include <iostream>#include <stdio.h>using namespace std;class Node{public:int Coe;int Exp;Node *next;};class List{public://初始化构造函数,别忘了初始化Node *InsertNode(Node *Head,Node *Ipt); //声明一个输入节点函数Node *InsertTerm(); //声明一个输入项函数void ShowPloy(Node *Head); //显示多项式的函数Node *AddPloy(Node *Head1,Node *Head2); //声明一个加法函数Node *SubPloy(Node *Head1,Node *Head2); //声明一个减法函数Node *MulPloy(Node *Head1,Node *Head2); //声明一个乘法函数void MainShow(); //程序界面设计};Node *List::InsertNode(Node *Head,Node *Ipt) //插入节点函数,返回值为空{int Flag=0; //初始化一个信号,便于我后面的判断Node *pre,*now; //定义两个指针,分别代表我们之前的位置以及现在的位置pre=Head; //将头指针赋给pre,即我们之前的位置在头结点if (pre->next==NULL){pre->next=Ipt; //此时的Ipt就是我们传入的ptr指针,他是我们要插入的那个指针,这行语句是在我们已经判断了头指针后面为空的情况下,我们将Ipt赋给头指针的后一项}else{now=pre->next; //如果头指针后面的值不为空,那么代表着这个链表已经开始存储数据,所以我们需要遍历链表,同时我们将现在的位置定在头指针的后面,然后开始依次比较,看插入的节点的幂数与现在指针的幂数哪个大,若比现在大,那么插到现在位置的前面,若小,那么继续比较,若相同,那么合并这两项///接下来会是一系列if的判断语句,OK,如果你现在有点头晕那么先休息片刻,再来看这段程序,当然这还不算什么while(Flag==0){if ((Ipt->Exp)<(now->Exp)) //判断结果为插入指针的幂数小于现在指针的幂数,执行花括号里面的语句{if (now->next==NULL) //如果现在指针后面的指针值为空,执行花括号里面的语句{now->next=Ipt;Flag=1; //标记Flag变为1,跳出while 循环}else //看清楚,这个else是表示如果现在指针后面指针的值不为空的情况,那么执行花括号的语句{pre=now; //如果接下来还有值得话,那么我就将现在的指针定义为我之前的指针now=pre->next; //而我的位置将向后移动一位,我现在的位置变成了now之后的那个指针,然后我继续做判断}}else if(Ipt->Exp>now->Exp) //这个else表示的是插入节点的幂数大于现在节点的幂数时,执行花括号内的语句{Ipt->next=now; //当插入的幂数大于现在的幂数时,我们就只需将插入的节点放到现在节点的前面即可,那么我们就将插入节点的后面的指针定义为现在的指针pre->next=Ipt; //之前的指针指向我们现在的插入指针Flag=1; //标记Flag变为1,,跳出while循环}else //这个else表示我们的插入节点幂数既不大于也不小于现在的节点幂数,那么很显然,这个表示的是幂数相同的情况。

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

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

顺序链式一元多项式加法,减法,乘法运算的实现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;<>。

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 项。

一元多项式的计算代码

一元多项式的计算代码

一元多项式的计算代码:#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 #include typedef struct node {float coef; int exp;struct node *next; }Lnode, *polynmial;void create(polynmial &L); //输入并建立多项式L void display(polynmial L); //显示,输出多项式L void sort(polynmial &L); //多项式L按指数排序 voidreverse(polynmial &L); //逆置 void select(); //用户选择加减操作void add(polynmial La, polynmial Lb, polynmial &Lc); //多项式voidsubtract(polynmial La, polynmial Lb, polynmial &Ld); //多项式void create(polynmial &L) //输入并建立多项式L {int i, n;static struct node *p; scanf(\L = (struct node *)malloc (sizeof(struct node)); L->next = NULL; for(i = 0;i < n; i++) {p = (struct node *)malloc(sizeof(struct node)); scanf(\ p->next = L->next; L->next = p; } }void display(polynmial L)//显示,输出多项式L {struct node *p, *q; int flag = 0; int k = 0; q = L->next; while(q) {if(q->coef != 0) k++;q = q->next;La,Lb相加La减去LbLd ,结果给}printf(\p = L->next; if(p->coef != 0) {printf(\ flag++; }for(p = p->next; p; p = p->next) {if(p->coef != 0) {printf(\ flag++; } }if(flag == 0)printf(\elseprintf(\}void sort(polynmial &L)//多项式L按指数排序 {polynmial p, q, r, u; p = L->next;L->next = NULL; while(p != NULL) {r = L;q = L->next;while((q != NULL) && (q->exp <= p->exp)) {r = q;q = q->next; }u = p->next; r->next = p; p->next = q; p = u; } }void reverse(polynmial &L)//逆置{polynmial H;static struct node *p, *q, *s;H = (struct node*)malloc(sizeof(struct node)); H->next = NULL;p = (struct node*)malloc(sizeof(struct node)); s = L->next;p->coef = s->coef; p->exp = s->exp; p->next = s->next; while(s) {p->coef = s->coef; p->exp = s->exp; p->next = s->next; q = H->next; H->next = p; p->next = q;p = (struct node*)malloc(sizeof(struct node)); s = s->next; }p = H->next; q = L->next; while(p) {q->coef = p->coef; q->exp = p->exp; q = q->next; p = p->next; } }void select() //用户选择加减操作 {printf(\请选择加减操作\\n\printf(\两个一元多项式相加\\n\printf(\两个一元多项式相减\\n\}void add(polynmial La, polynmial Lb, polynmial &Lc)//{struct node *pa, *pb; static struct node *pc;Lc = (struct node*)malloc(sizeof(struct node));La,Lb多项式相加pa = La->next; pb = Lb->next; Lc->next = NULL; while(pa && pb) {pc = (struct node*)malloc(sizeof(struct node)); if(pa->exp < pb->exp) {pc->next = Lc->next; Lc->next = pc;pc->coef = pa->coef; pc->exp = pa->exp; pa = pa->next; }elseif(pa->exp == pb->exp) {pc->next = Lc->next; Lc->next = pc; pc->exp = pa->exp;pc->coef = pa->coef + pb->coef; pa = pa->next; pb = pb->next; } else {pc->next = Lc->next; Lc->next = pc;pc->coef = pb->coef; pc->exp = pb->exp; pb = pb->next; } }while(pa) {pc = (struct node*)malloc(sizeof(struct node)); pc->next = Lc->next; Lc->next = pc;pc->coef = pa->coef; pc->exp = pa->exp; pa = pa->next; }while(pb) {pc = (struct node*)malloc(sizeof(struct node)); pc->next = Lc->next; Lc->next = pc;pc->coef = pb->coef; pc->exp = pb->exp; pb = pb->next; } }void subtract(polynmial La, polynmial Lb, polynmial &Ld)//多项式{struct node *pa, *pb; static struct node *pd;Ld = (struct node*)malloc(sizeof(struct node)); pa = La->next; pb = Lb->next; Ld->next = NULL; while(pa && pb) {pd = (struct node*)malloc(sizeof(struct node)); if(pa->exp < pb->exp) {pd->next = Ld->next; Ld->next = pd;pd->coef = pa->coef; pd->exp = pa->exp; pa = pa->next; }elseif(pa->exp == pb->exp) {pd->next = Ld->next; Ld->next = pd; pd->exp = pa->exp;pd->coef = pa->coef - pb->coef; pa = pa->next; pb = pb->next; } else {pd->next = Ld->next; Ld->next = pd;pd->coef = pb->coef; pd->exp = pb->exp;La减去Lb,结果给Ld感谢您的阅读,祝您生活愉快。

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

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

#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}。

一元多项式求解(cc++)源程序

一元多项式求解(cc++)源程序
tag=1;
break;
}
}while(tag==1);//考虑多项式名重复情况
q=(name *)malloc(sizeof(name));
strcpy(q->s,s2);
q->next=pname->next;
pname->next=q;//头插法
PrintPoly(pname);//输出所有的多项式
}
void Plus(name *pname){//多项式相加
char s[2][N],s2[N];
name *p[2],*q;
node *pp,*qq,*rr,*ss;
int tag,i;
PrintPoly(pname);//输出所有多项式
if(strcmp(q->s,s)==0){
printf("多项式名有冲突,请重新输入:");
scanf("%s",s);
tag=1;
break;
}
}while(tag==1);//考虑多项式名重复情况
strcpy(p->s,s);
printf("请输入多项式的项数(输入数字):");
scanf("%s",s2);
tag=1;
break;
}
}while(tag==1);//考虑多项式名重复情况
q=(name *)malloc(sizeof(name));
strcpy(q->s,s2);
q->next=pname->next;
pname->next=q;//头插法

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

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

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

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

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

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

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

每个节点包含两个部分:系数(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");}。

一元多项式相加(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;}}。

一元多项式的加减

一元多项式的加减

一元多项式的加减(代码+截图)程序原代码:#include<iostream>using namespace std;struct Node{floatcoef;intexp;};typedef Node polynomial;structLNode{polynomial data;LNode *next;};typedefLNode* Link;voidCreateLink(Link &L,int n);voidPrintList(Link L);voidPolyAdd(Link &pc,Linkpa,Linkpb);voidPolySubstract(Link &pc,Linkpa,Linkpb);voidCopyLink(Link &pc,Link pa);intJudgeIfExpSame(Link pa,Link e);voidDestroyLink(Link &L);intCompareIfNum(int i);voidDestroyLink(Link &L){Link p;p=L->next;while(p){L->next=p->next;delete p;p=L->next;}delete L;L=NULL;}voidCreateLink(Link &L,int n){if(L!=NULL){DestroyLink(L);}Link p,newp;L=new LNode;L->next=NULL;(L->data).exp=-1;p=L;for(int i=1;i<=n;i++){newp=new LNode;printf("请输入第%d项的系数和指数:\n",i);printf("系数:");scanf("%f",&(newp->data).coef);printf("指数:");scanf("%d",&(newp->data).exp);if(newp->data.exp<0){printf("您输入有误,指数不允许为负值!\n");deletenewp;i--;continue;}newp->next=NULL;p=L;if(newp->data.coef==0){printf("系数为零,重新输入!\n");deletenewp;i--;continue;}while((p->next!=NULL)&&((p->next->data).exp<(newp->data).exp)) {p=p->next;}if(!JudgeIfExpSame( L, newp)){newp->next=p->next;p->next=newp;}else{printf("输入的该项指数与多项式中已存在的某项相同,请重新创建一个正确的多项式\n");deletenewp;DestroyLink(L);CreateLink(L,n);break;}}}intJudgeIfExpSame(Link L,Link e){Link p;p=L->next;while(p!=NULL&&(e->data.exp!=p->data.exp))p=p->next;if(p==NULL)return 0;elsereturn 1;}voidPrintList(Link L){Link p;if(L==NULL||L->next==NULL)printf("该一元多项式为空!\n");else{p=L->next;if((p->data).coef>0){if((p->data).exp==0)printf("%1.f",(p->data).coef);else if((p->data).coef==1&&(p->data).exp==1)printf("X");else if((p->data).coef==1&&(p->data).exp!=1)printf("X^%d",(p->data).exp);else if((p->data).exp==1&&(p->data).coef!=1)printf("%1.fX",(p->data).coef);elseprintf("%1.fX^%d",(p->data).coef,(p->data).exp);}if((p->data).coef<0){if((p->data).exp==0)printf("%1.f",(p->data).coef);else if(p->data.coef==-1&&p->data.exp==1)printf("-X");else if(p->data.coef==-1&&p->data.exp!=1)printf("-X^%d",(p->data).exp);else if(p->data.exp==1)printf("%1.fX",(p->data).coef);elseprintf("%1.fX^%d",(p->data).coef,(p->data).exp);}p=p->next;while(p!=NULL){if((p->data).coef>0){if((p->data).exp==0)printf("+%1.f",(p->data).coef);else if(p->data.coef==1&&p->data.exp==1)printf("+X");else if(p->data.coef==1&&p->data.exp!=1)printf("+X^%d",(p->data).exp);else if(p->data.exp==1)printf("+%1.fX",(p->data).coef);elseprintf("+%1.fX^%d",(p->data).coef,(p->data).exp);}if((p->data).coef<0){if((p->data).exp==0)printf("%1.f",(p->data).coef);else if(p->data.coef==-1&&p->data.exp==1)printf("-X");else if(p->data.coef==-1&&p->data.exp!=1)printf("-X^%d",(p->data).exp);else if(p->data.exp==1)printf("%1.fX",(p->data).coef);elseprintf("%1.fX^%d",(p->data).coef,(p->data).exp);}p=p->next;}}}voidCopyLink(Link &pc,Link pa){Link p,q,r;pc=new LNode;pc->next=NULL;r=pc;p=pa;while(p->next!=NULL){q=new LNode;q->data.coef=p->next->data.coef;q->data.exp=p->next->data.exp;r->next=q;q->next=NULL;r=q;p=p->next;}}voidPolyAdd(Link &pc,Linkpa,Linkpb){Link p1,p2,p,pd;CopyLink(p1,pa);CopyLink(p2,pb);pc=new LNode;pc->next=NULL;p=pc;p1=p1->next;p2=p2->next;while(p1!=NULL&&p2!=NULL){if(p1->data.exp<p2->data.exp){p->next=p1;p=p->next;p1=p1->next;}else if(p1->data.exp>p2->data.exp){p->next=p2;p=p->next;p2=p2->next;}{p1->data.coef=p1->data.coef+p2->data.coef;if(p1->data.coef!=0){p->next=p1;p=p->next;p1=p1->next;p2=p2->next;}else{pd=p1;p1=p1->next;p2=p2->next;deletepd;}}}if(p1!=NULL){p->next=p1;}if(p2!=NULL){p->next=p2;}}voidPolySubstract(Link &pc,Linkpa,Linkpb){Link p,pt;CopyLink(pt,pb);p=pt;while(p!=NULL){(p->data).coef=(-(p->data).coef);p=p->next;}PolyAdd(pc,pa,pt);DestroyLink(pt);}void Clear(){system("pause");system("cls");}void Menu(){printf("\t*\t\t\t *\n");printf("\n");printf("\t*******************一元多项式的简单运算*********************\n"); printf("\t*\t\t\t\t\t\t\t *\n");printf("\t*\t\t 1创建要运算的两个一元多项式\t\t *\n");printf("\t*\t\t 2将两个一元多项式相加\t\t\t *\n");printf("\t*\t\t 3将两个一元多项式相减\t\t\t *\n");printf("\t*\t\t 4显示两个一元多项式\t\t\t *\n");printf("\t*\t\t 5退出\t\t\t\t\t *\n");printf("\t*\t\t\t\t\t\t\t *\n");printf("\t*******************一元多项式的简单运算*********************\n"); printf("\n");printf("\t*\t\t 请输入你要进行的操作(1-5)\t\t *\n");}intCompareIfNum(int i){if(i>0&&i<5)return 0;elsereturn 1;}void main(){int n;Link L,La=NULL,Lb=NULL;int choose;while(1){Menu();scanf("%d",&choose);switch(choose){case 1:printf("请输入你要运算的第一个一元多项式的项数:\n");scanf("%d",&n);if(CompareIfNum(n)==1){printf("您的输入有误,请重新输入……\n");Clear();break;}CreateLink(La,n);printf("请输入你要运算的第二个一元多项式的项数:\n");scanf("%d",&n);if(CompareIfNum(n)==1){printf("您的输入有误,请重新输入……\n");Clear();break;}CreateLink(Lb,n);Clear();break;case 2:if(La==NULL||Lb==NULL){printf("您的多项式创建有误,请重新选择……\n");Clear();break;}PolyAdd(L,La,Lb);printf("相加的两个一元多项式为:\n");PrintList(La);PrintList(Lb);printf("相加后的结果为:\n");PrintList(L);Clear();DestroyLink(L);break;case 3:if(La==NULL||Lb==NULL){printf("您的多项式创建有误,请重新选择……\n");Clear();break;}PolySubstract(L,La,Lb);printf("相减的两个一元多项式为:\n");PrintList(La);PrintList(Lb);printf("相减后的结果为:\n");PrintList(L);Clear();DestroyLink(L);break;case 4:if(La==NULL||Lb==NULL){printf("您的多项式创建有误,请重新选择……\n");Clear();break;}printf("第一个一元多项式为:\n");PrintList(La);printf("第二个一元多项式为:\n");PrintList(Lb);Clear();break;case 5:exit(0); //exit(0)强制终止程序,返回状态码0表示正常结束default:printf("您的输入有误,请重新选择操作……\n");Clear();break;}}}运行结果截图:。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
p->coef+=q->coef;
if(fabs(p->coef)>1e-6)
{
pre->next = q->next;
temp = q;
q = temp->next;
free(temp);
}
else //两项系数和为0,释放结点p和q
//函数实现
//创建多项式链表,返回头指针
PolyList CreatePolyList()
{
PolyNode *s,*rear,*head;
int e; //指数
float c; //系数
int n=1; //计数器
head =(PolyNode *) malloc(sizeof(PolyNode));
s->coef = sum;
s->exp = pa->exp;
rear->next = s;
rear = s;
}
//pa,pb指针后移
pa = pa->next;
pb = pb->next;
}
else if(pa->exp>pb->exp) //若pa指数大于pb指数,将pa结点副本插入到PolyC尾部
//Polynomial_list
//功能:输入两个多项式(不要求是升幂排列,程序将自动合并同类项和排序),进行加减乘除运算
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//结点定义
typedef struct PolyNode
{
int exp; //指数
float coef; //系数
PolyNode* next;
}PolyNode , * PolyList;
//函数声明
PolyList CreatePolyList(); //创建多项式链表,返回头指针
void DisplayPolyList(PolyList Poly);//显示多项式
s->exp = pa->exp;
rear->next = s ;
rear = s ;
pa = pa->next;
}
else //若pb指数大于pa指数,将pb结点副本插入到PolyC尾部
{
s = (PolyNode *)malloc(sizeof(PolyNode));
float sum=0;//存储两项系数和
PolyNode *pa , *pb , *rear , *s ;
PolyC = (PolyNode *)malloc(sizeof(PolyNode));
pa = PolyA->next;
pb = PolyB->next;
rear = PolyC;
{
rear->next = p->next;
temp = p;
p = temp->next;
free(temp);
pre->next = q->next;
temp = q;
q = temp->next;
free(temp);
while(rear->next!=NULL)
{
exp = rear->next->exp;
p = rear->next ;
prior = rear;
temp = prior->next ;
while(p!=NULL)
{
if(p->exp > exp)
void SortPoly(PolyList Poly)
{
PolyList rear , p ,temp , prior;
if(!Poly->next) return; //若多项式为空,返回
MergePoly(Poly);
rear = Poly;
int exp;//记录当前啊搜索项中的最小指数
PolyNode *pa , *pb , *rear , *s ;
PolyC = (PolyNode *)malloc(sizeof(PolyNode));
pa = PolyA->next;
pb = PolyB->next;
rear = PolyC;
rear->next = NULL;
{
PolyNode * p , *temp;
p = L;
while(p!=NULL)
{temp = p ; p = p->next; free(temp); }
}
//将多项式和并同类项
void MergePoly(PolyList Poly)
{
PolyNode * p , *q , * rear ,*pre ,* temp;
rear = Poly;
p = Poly->next ;
while(rear->next!=NULL)
{
q = p->next;
pre = p;
temp = p;
while(q)
{
if(p->exp == q->exp) //两项指数相等,合并两项,并释放结点q
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = sum;
s->exp = pa->exp;
rear->next = s;
rear = s;
}
//pa,pb指针后移
pa = pa->next;
}
}
//多项式相减,返回差多项式链表头指针
PolyList PolySub(PolyList PolyA , PolyList PolyB)
{
PolyList PolyC ;
SortPoly(PolyA);
SortPoly(PolyB);
float sum =0 ;//存储两项系数差
{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pa->coef;
void DestroyPolyList(PolyList L);//释放链表所用存储空间
void MergePoly(PolyList Poly);//将多项式和并同类项
void SortPoly(PolyList Poly);//将多项式按升序排列
PolyList PolyAdd(PolyList PolyA , PolyList PolyB);//多项式相加,返回和多项式链表头指针
while(pa && pb)
{
if(pa->exp == pb->exp)
{
sum = pa->coef-pb->coef;
if(fabs(sum)>1e-6) //如果两两系数不为0,则将两项和存入s中,并插入PolyC尾部
{
s = (PolyNode *)malloc(sizeof(PolyNode));
{
exp = p->exp ;
temp = p ;
p = temp->next ;
}
else
{
p = p->next ;
if(rear->next->next ==NULL) return; //p为最后一个元素且指数最小,提前返回
s->coef = pa->coef;
s->exp = pa->exp;
rear->next = s;
pa = pa->next;
rear = s ;
}
while(pb)
{
s = (PolyNode *)malloc(sizeof(PolyNode));
rear->next = NULL;
while(pa && pb)
{
if(pa->exp == pb->exp)
{
sum = pa->coef+pb->coef;
if(fabs(sum)>1e-6) //如果两两系数不为0,则将两项和存入s中,并插入PolyC尾部
rear = head;
//输入多项式的系数和指数,若输入系数为0退出
printf("请输入多项式的第%d项的系数和指数(用逗号分开):" , n++);
scanf("%f,%d" , &c , &e);
while(fabs(c) > 1e-6)
{ s = (PolyNode *)malloc(sizeof(PolyNode));
return head;
}
//计算两个多项式(可不按顺序排列) , 结果存到链表PolyC中,并返回
PolyList PolyAdd(PolyList PolyA , PolyList PolyB)
{
PolyList PolyC ;
SortPoly(PolyA);
相关文档
最新文档