两多项式求和(C语言版)
- 格式:doc
- 大小:18.00 KB
- 文档页数:3
C语⾔实现多项式的相加本⽂实例为⼤家分享了C语⾔多项式相加的具体代码,供⼤家参考,具体内容如下包含带头节点的链表的初始化,输出:#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>typedef struct Pol{int coe; // 系数int index; // 指数struct Pol *next;}Pol;int main(int argc, char *argv[]){Pol *head1 = NULL; // 第⼀个多项式Pol *head2 = NULL; // 第⼆个多项式Pol *Initiate(Pol *head1); // 声明初始化函数void Output(Pol *head); // 声明输出函数void PolAdd(Pol *head1, Pol *head2); // 声明相加函数int coe, index;char sign;Pol *p;int n = 0;// 初始化第⼀个多项式head1 = Initiate(head1);p = head1;while (1){scanf("%dx%d%c", &coe, &index, &sign);p->next = (Pol *)malloc(sizeof(Pol));p = p->next;p->coe = coe;p->index = index;p->next = NULL;if(sign == '\n')break;}printf("第⼀多项式输⼊完毕。
\n");// 初始化第⼆个多项式head2 = Initiate(head2);p = head2;while (1){scanf("%dx%d%c", &coe, &index, &sign);p->next = (Pol *)malloc(sizeof(Pol));p = p->next;p->coe = coe;p->index = index;p->next = NULL;if (sign == '\n')break;}printf("第⼆多项式输⼊完毕。
#include<stdio.h>#include<stdlib.h>#include<malloc.h>typedef int ElemType;/*单项链表的声明*/typedef struct PolynNode{int coef; // 系数int expn; // 指数struct PolynNode *next;}PolynNode,*PolynList;/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/ /*指数系数一对一对输入*/void CreatePolyn(PolynList &L,int n){int i;PolynList p,q;L=(PolynList)malloc(sizeof(PolynNode)); // 生成头结点L->next=NULL;q=L;printf("成对输入%d个数据\n",n);for(i=1;i<=n;i++){p=(PolynList)malloc(sizeof(PolynNode));scanf("%d%d",&p->coef,&p->expn); //指数和系数成对输入q->next=p;q=q->next;}p->next=NULL;}// 初始条件:单链表L已存在// 操作结果: 依次对L的每个数据元素调用函数vi()。
一旦vi()失败,则操作失败void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType)){PolynList p=L->next;while(p){vi(p->coef, p->expn);if(p->next){printf(" + "); //“+”号的输出,最后一项后面没有“+” }p=p->next;}printf("\n");}/*ListTraverse()调用的函数(类型要一致)*/void visit(ElemType c, ElemType e){if(c != 0){printf("%dX^%d",c,e); //格式化输出多项式每一项}}/* 多项式相加,原理:归并 *//* 参数:两个已经存在的多项式 *//* 返回值:归并后新的多项式的头结点 */PolynList MergeList(PolynList La, PolynList Lb){PolynList pa, pb, pc, Lc;pa = La->next;pb = Lb->next;Lc = pc = La; // 用La的头结点作为Lc的头结点while(pa&&pb){if(pa->expn < pb->expn){pc->next = pa; //如果指数不相等,pc指针连上指数小的结点,pc = pa;pa = pa->next; //指向该结点的指针后移}else if(pa ->expn > pb->expn ){pc->next = pb; //pc指针连上指数小的结点,pc = pb;pb = pb->next; //指向该结点的指针后移}else//(pa ->expn = pb->expn ){pa->coef = pa->coef + pb->coef; //指数相等时,系数相加 pc->next = pa;pc = pa;pa = pa->next; //两指针都往后移pb = pb->next;}}pc->next = pa ? pa:pb; // 插入剩余段return Lc;}void main(){PolynList ha,hb,hc;printf("非递减输入多项式ha, ");CreatePolyn(ha,5); // 正位序输入n个元素的值printf("非递减输入多项式hb, ");CreatePolyn(hb,5); // 正位序输入n个元素的值printf("多项式ha :");PolynTraverse(ha, visit); printf("\n");printf("多项式hb :"); PolynTraverse(hb, visit); printf("\n");hc = MergeList(ha,hb); PolynTraverse(hc, visit); }。
单链表应⽤之稀疏多项式求和(C语⾔)采⽤归并思想计算两个多项幂式之和,这⾥有两个化简好的关于x的多项幂式:A(x)=7+3x+9x^8+5x^17+2x^20;B(x)=8x+22x^7-9x^8-4x^17,⽤C语⾔实现两多项式数据的存储,并求两者的和Y(x)。
之所以称之为稀疏多项式,是因为多项式中各项x的指数部分不连续,且相差较⼤,故编程实现该类多项式存储时可考虑链式存储,提升空间利⽤率。
完整代码如下:#include <stdio.h>#include <stdlib.h>/*** 含头节点单链表应⽤之稀疏多项式相加:* A(x)=7+3x+9x^8+5x^17+2x^20* B(x)=8x+22x^7-9x^8-4x^17* 求:Y(x)=A(x)+B(x)*///基本操作函数⽤到的状态码#define TRUE 1;#define FALSE 0;#define OK 1;#define ERROR 0;//Status是新定义的⼀种函数返回值类型,其值为int型typedef int Status;//数据元素类型typedef struct {int coe; //系数int exp; //指数} ElemType;//单链表定义typedef struct Lnode {ElemType data; //数据域struct Lnode *next; //指针域} Lnode, *LinkList;//基本操作1:单链表初始化Status InitList(LinkList *list) {(*list)=(LinkList)malloc(sizeof(Lnode));(*list)->next=NULL;return OK;}//基本操作11:头插法建⽴链表,数据已保存在ElemType类型的数组中Status CreateList_H(LinkList *list,ElemType arrData[],int length) {int j;for(j=length-1;j>=0;j--){//新建结点Lnode *node;node=(Lnode*)malloc(sizeof(Lnode));node->data=arrData[j];node->next=NULL;//插⼊为1号结点node->next=(*list)->next;(*list)->next=node;}return OK;}//基本操作13:链表元素遍历输出Status ListTraverse(LinkList list) {Lnode *p;p=list;int j=0;printf("序号:系数:指数:\n");while(p->next){j++;p=p->next;printf("(%d) %d %d\n",j,(p->data).coe,(p->data).exp);}printf("\n");return OK;}//多项式求和, pa、pb、pc分别指向listA、listB、合并后新链表的当前结点Status GetPolynthicSum(LinkList *listA,LinkList *listB){Lnode *pa,*pb,*pc;pa=(*listA)->next;pb=(*listB)->next;pc=(*listA);while(pa&&pb){if(pa->data.exp==pb->data.exp) {Lnode *waitInsert=pa;pa->data.coe+=pb->data.coe; //系数相加if(pa->data.coe==0) { //系数和为零pa=pa->next;free(waitInsert); //释放系数和为零的结点} else {pa=pa->next;//表尾加⼊新结点,并更新为该新结点pc->next=waitInsert;pc=pc->next;}Lnode *needDelete=pb;pb=pb->next;free(needDelete); //释放listB中的结点} else if(pa->data.exp<pb->data.exp) {Lnode *waitInsert=pa;pa=pa->next;//表尾加⼊新结点,并更新为该新结点pc->next=waitInsert;pc=pc->next;} else {Lnode *waitInsert=pb;pb=pb->next;//表尾加⼊新结点,并更新为该新结点pc->next=waitInsert;pc=pc->next;}}//连接剩余结点if(pa) { //表listA长于listBpc->next=pa;} else {pc->next=pb;}//释放list_B表头free(*listB);return OK;}int main(void){//产⽣多项式相关数据,A(x)、B(x)的项按幂增排列ElemType waitInserted_A[] = {{ 7, 0 },{ 3, 1 },{ 9, 8 },{ 5,17 },{ 2, 20 }};ElemType waitInserted_B[] = {{ 8, 1 },{ 22, 7 },{ -9, 8 },{ -4, 17 }};//获得数组长度int arrLength_A=sizeof(waitInserted_A)/sizeof(waitInserted_A[0]); int arrLength_B=sizeof(waitInserted_B)/sizeof(waitInserted_B[0]);//头插法建⽴链表list_A和list_B分别保存A(x)、B(x)两个多项式的数据 LinkList list_A,list_B;InitList(&list_A);InitList(&list_B);CreateList_H(&list_A,waitInserted_A,arrLength_A);CreateList_H(&list_B,waitInserted_B,arrLength_B);printf("多项式A(x)的数据:\n");ListTraverse(list_A); //遍历测试printf("多项式B(x)的数据:\n");ListTraverse(list_B); //遍历测试//计算Y(x)=A(x)+B(x);结果数据放⼊list_A;GetPolynthicSum(&list_A,&list_B);printf("多项式Y(x)=A(x)+B(x)的数据:\n");ListTraverse(list_A); //遍历测试printf("\nEND!");return0; }。
#include<stdio.h>#include<stdlib.h>#include<malloc.h>typedef int ElemType;/*单项链表的声明*/typedef struct PolynNode{int coef; // 系数int expn; // 指数struct PolynNode *next;}PolynNode,*PolynList;/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/ /*指数系数一对一对输入*/void CreatePolyn(PolynList &L,int n){int i;PolynList p,q;L=(PolynList)malloc(sizeof(PolynNode)); // 生成头结点L->next=NULL;q=L;printf("成对输入%d个数据\n",n);for(i=1;i<=n;i++){p=(PolynList)malloc(sizeof(PolynNode));scanf("%d%d",&p->coef,&p->expn); //指数和系数成对输入q->next=p;q=q->next;}p->next=NULL;}// 初始条件:单链表L已存在// 操作结果: 依次对L的每个数据元素调用函数vi()。
一旦vi()失败,则操作失败void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType)){PolynList p=L->next;while(p){vi(p->coef, p->expn);if(p->next){printf(" + "); //“+”号的输出,最后一项后面没有“+”}p=p->next;}printf("\n");}/*ListTraverse()调用的函数(类型要一致)*/void visit(ElemType c, ElemType e){if(c != 0){printf("%dX^%d",c,e); //格式化输出多项式每一项 }}/* 多项式相加,原理:归并 *//* 参数:两个已经存在的多项式 *//* 返回值:归并后新的多项式的头结点 */PolynList MergeList(PolynList La, PolynList Lb){PolynList pa, pb, pc, Lc;pa = La->next;pb = Lb->next;Lc = pc = La; // 用La的头结点作为Lc的头结点while(pa&&pb){if(pa->expn < pb->expn){pc->next = pa; //如果指数不相等,pc指针连上指数小的结点,pc = pa;pa = pa->next; //指向该结点的指针后移}else if(pa ->expn > pb->expn ){pc->next = pb; //pc指针连上指数小的结点,pc = pb;pb = pb->next; //指向该结点的指针后移}else//(pa ->expn = pb->expn ){pa->coef = pa->coef + pb->coef; //指数相等时,系数相加pc->next = pa;pc = pa;pa = pa->next; //两指针都往后移pb = pb->next;}}pc->next = pa ? pa:pb; // 插入剩余段return Lc;}void main(){PolynList ha,hb,hc;printf("非递减输入多项式ha, ");CreatePolyn(ha,5); // 正位序输入n个元素的值 printf("非递减输入多项式hb, ");CreatePolyn(hb,5); // 正位序输入n个元素的值 printf("多项式ha :");PolynTraverse(ha, visit);printf("\n");printf("多项式hb :");PolynTraverse(hb, visit);printf("\n");hc = MergeList(ha,hb);PolynTraverse(hc, visit);}。
#include "stdafx.h"//备注:我用的编译平台所提供的头文件不是“stdio.h"#include"malloc.h"#include<cstdlib> //此头文件用来解决执行可以执行文件exe后直接退出的问题,希望可以帮到大家了解一个新函数和头文件typedef struct List{ //定义一个动态链表float coef;int expn;struct List *next;}*list;list initlist()//初始化,生成新结点{list l;l=(list)malloc(sizeof(List));if (!l) printf("error");l->next=NULL;return l;}void insertlist (list l,float *coe,int *exp)//每次scanf后插入结点,从链尾插入,main函数中定义一个链表尾指针{list s;s=(list)malloc(sizeof(List));if(!s)printf("error");s->coef=*coe;s->expn=*exp;l->next =s;s->next =NULL;}void putout(list l)//输出链表{list p;p=l->next;while(p->next){ if(p->expn==0)printf("1+");else if(p->coef==1&&p->expn==1){printf("x+");}elseprintf("%.2fx^%d+",p->coef,p->expn);p=p->next ;} if(p->expn==0)printf("1\n");else if(p->coef==1&&p->expn==1){printf("x\n");}elseprintf("%.2fx^%d\n",p->coef,p->expn);}int cmp(int f,int g) //比较函数,用来判定当前两多项式指数问题{if (f<g)return (-1);else if(f==g)return 0;elsereturn (1);}void addlist(list n,list m){//多项式相加list hn,hm,pn,pm;float sum;pn=n->next ;pm=m->next ;hn=n;hm=m;//定义一个当前节点的指针和一个头指针while(hn->next&&hm->next){switch (cmp(pn->expn,pm->expn))//比较两指数{case -1:hn=pn;pn=pn->next;break;case 0:sum=pn->coef +pm->coef ;if(sum!=0){pn->coef =sum;pm=pm->next ;free(hm->next);hm->next =pm;hn=hn->next ;pn=pn->next ;}//ifelse {hn->next =pn->next ;free(pn);pn=hn->next ;hm->next =pm->next ;free(pm);pm=hm->next ;}break;//elsecase 1:hm->next =pm->next ;hn->next=pm;pm->next =pn;hn=pm;pn=pn->next ;pm=hm->next ;break;}//switch}//whileif(hm->next ) //参考书本43页算法的思想,将剩余结点插入当前链表中{hn->next=pm;free(hm);}}//addlistvoid chongpaixu(list l)//将输入的多项式按升序排列,并将指数相同的合并(还不能执行){ list s;list q;list k;list w;float sum;k=initlist();q=l->next ;s=l;while(q->next){for(w=l ;q->next !=NULL;q=q->next ){for (s=s->next ;s->next!=NULL;s=s->next ){switch(cmp(s->expn,q->expn)){case -1:w=w->next ;break;case 1:k->coef=q->coef;q->coef=s->coef;s->coef=k->coef;k->expn=q->expn ;q->expn =s->expn ;s->expn =k->expn ;free(k);w=w->next ;break;case 0:sum=s->coef+q->coef;if(sum){s->coef=sum;s->next=q;free(q);q=s->next ;}//ifelse {w->next=q->next;free(s);free(q);s=w->next ;q=s->next ;}//elsebreak;}//switch}}}//while}void putmessage(void)//用来表明备注{printf("备注:该算法经过上课时老师给我们提出的问题进行了修改,不过关于排序的算法还不能完善\n");printf("因此,请输入时请将多项式按照升序输入,将相同指数合并!\n");printf("\n");printf("\n");printf("\n");printf("\n");printf("\n");printf("\n");}void main(){list l,s,hl,hs; int i,d,exp;float *q;int *w;float coe;putmessage();q=&coe;w=&exp;l=initlist();s=initlist();hl=l;hs=s;printf("请输入l的项数\n");scanf("%d",&d);if(d>0){for (i=0;i<d;i++){printf("请输入第%d项的系数\n",i+1);scanf("%f",&coe);printf("请输入第%d项的指数\n",i+1);scanf("%d",&exp);if(coe){insertlist(hl,q,w);hl=hl->next ;}}/*chongpaixu(l);*/if(l->next ){printf("你输入的多项式是f(x)=");putout(l);}else printf("你输入的多项式是f(x)=0\n"); }else printf("你输入的多项式l不存在\n");printf("请输入s的项数\n");scanf("%d",&d);if(d>0){for (i=0;i<d;i++){printf("请输入第%d项的系数\n",i+1);scanf("%f",&coe);printf("请输入第%d项的r指数\n",i+1);scanf("%d",&exp);if(coe){insertlist(hs,q,w);hs=hs->next ;}}if(s->next){printf("你输入的多项式是f(x)=");putout(s);}else printf("你输入的多项式是f(x)=0\n");}else printf("你输入的多项式不存在\n");hl=l;hs=s;if(hl->next &&hs->next ){addlist(l,s);printf("合并后的多项式f(x)=");if(l->next){putout(l);}else printf("0\n");}else printf("no add\n");system ( "pause" );//不加此语句,exe文件在执行完毕直接跳出}。
两多项式求和(C语言版)#include "stdlib.h"#include "stdio.h"# define OVERFLOW -2typedef struct term{ float coef; //多项式系数int expn; //多项式指数struct term *next;} node;node *Create(int n)//创建一个n个结点的链表,并给每//个节点数据域赋值{ node *head, *p, *q;//int i;head=(node *)malloc(sizeof(node));if(!head) { printf("分配内存失败!");exit(OVERFLOW);}for(i=0;i<n;i++)< p="">{ p=(node *)malloc(sizeof(node));if(!p){ printf("分配内存失败!");exit(OVERFLOW);}printf("请输入第%d项系数:\n",i+1);//从键盘读取数据scanf("%f",&p->coef);printf("请输入第%d项指数:\n",i+1);scanf("%d",&p->expn);//从键盘读取数据if(i==0)//如果是第一个节点,则指针q、head同时指向该第一个实节点head->next=q=p;else //否则一个节点一个节点往后接{ q->next=p;p->next=NULL;q=p;}}return(head);//返回所建链表头指针}int cmp (node *m,node *n )//比较两个指数大小,并返回-1或0或1 { if(m->expnexpn)return -1;else if(m->expn==n->expn)return 0;elsereturn 1;}float Sum(node *m,node *n)//求多项式两个系数之和{ return m->coef+n->coef;}node *Add (node *a,node *b )//两个多项式相加{ node *ha,*hb,*pa,*pb,*tmpb;int t;ha=a;hb=b;while(ha->next!=NULL) {pa=ha->next;pb=hb->next;tmpb=hb;while(tmpb->next!=NULL){t=cmp(pa,pb);if(t==0){ (pa->coef)+=(pb->coef);pb=pb->next;tmpb->next=pb;}if(t!=0){tmpb=pb;pb=pb->next;}}ha=ha->next;}if(hb->next!=NULL)//如果多项式b还有某些项未执行相加操作,//则将其接到结果多项式a后面ha->next=hb->next;return a;//返回结果多项式}int main(){ node *x,*y,*p,*hp;int m=0,n=0;char c1,c2;printf("请输入第一个多项式的项数!\n");scanf("%d",&m);printf("请输入第一个多项式\n\n");x=Create(m);printf("请输入第二个多项式的项数!\n");scanf("%d",&n);printf("请输入第二个多项式\n\n");y=Create(n);p=Add(x,y );printf("两个多项式相加成功,其结果如下:\n"); while(p->next!=NULL)//输出相加所得的多项式{ printf("%5.1f",p->next->coef);if(p->next->expn!=0)printf(" * X(%d)",p->next->expn);p=p->next;if(p->next!=NULL)printf(" + ");}c1=getchar();c2=getchar();if(c2=='\n')printf("\n");return 0;}</n;i++)<>。
#include"stdio.h"#include"stdlib.h"#include"conio.h"typedef struct Item{double coef;//系数int expn;//指数struct Item *next;}Item,*Polyn;#define CreateItem(p) p=(Item *)malloc(sizeof(Item));#define DeleteItem(p) free((void *)p);/************************************************************/ /* 判断选择函数 *//************************************************************/ int Select(char *str){ char ch;printf("%s\n",str);printf("Input Y or N:");do{ ch=getch();}while(ch!='Y'&&ch!='y'&&ch!='N'&&ch!='n');printf("\n");if(ch=='Y'||ch=='y') return(1);else return(0);}/************************************************************/ /* 插入位置定位函数 *//**************************************************************/ int InsertLocate(Polyn h,int expn,Item **p){ Item *pre,*q;pre=h;q=h->next;while(q&&q->expn<expn){ pre=q;q=q->next;}if(!q){ *p=pre;return(1);}else if(q->expn==expn){ *p=q;return(0);}else{ *p=pre;return(-1);}}/************************************************************/ /* 插入结点函数 *//************************************************************/ void insert(Item *pre,Item *p){p->next=pre->next;pre->next=p;}/************************************************************/ /* 输入多项式 */ /************************************************************/ Polyn Input(void){double coef;int expn,flag;Item *h,*p,*q,*pp;CreateItem(h);//产生头结点h->next=NULL;printf("input coef and expn(if end ,expn=-1)\n");while(1){scanf("%lf%d",&coef,&expn); //输入多项式的系数和指数if(expn==-1) break; //若指数为-,表示输入结束if(InsertLocate(h,expn,&pp))//返回值非表示插入新结点{ CreateItem(p);p->coef=coef;p->expn=expn;insert(pp,p);}else if(Select("has the same expn,Replace older value?")) pp->coef=coef; //指数相同,替换系数}return h;}/************************************************************/ /* 撤消多项式 */ /************************************************************/ void Destroy(Polyn h){Item *p=h,*q;while(p!=NULL){q=p;p=p->next;DeleteItem(q);}}/************************************************************/ /* 输出多项式 */ /************************************************************/ void Output(Polyn h,char *title){int flag=1;Item *p=h->next;printf("%s=",title);while(p){ if(flag) //表示是否是多项式的第一项{ flag=0;if(p->expn==0) printf("%.2lf",p->coef);else printf("%.2lfx^%d",p->coef,p->expn);}else{ if(p->coef>0) printf("+");if(p->expn==0) printf("%.2lf",p->coef);else printf("%.2lfx^%d",p->coef,p->expn);}p=p->next;}printf("\n");}/************************************************************//* 判断两个多项式项的关系 *//************************************************************/int ItemComp(Item x,Item y){ if(x.expn<y.expn)return(-1);else if(x.expn==y.expn)return(0);else return(1);}/************************************************************//* 两多项式多项式相加 *//************************************************************/Polyn AddPolyn(Polyn h1,Polyn h2){Item *head,*last,*pa=h1->next,*pb=h2->next,*s;CreateItem(head);head->next=NULL;last=head;while(pa&&pb){CreateItem(s);last->next=s;last=s;switch (ItemComp(*pa,*pb)){case -1:last->coef=pa->coef;last->expn=pa->expn;pa=pa->next;break;case 0:last->coef=pa->coef+pb->coef;last->expn=pa->expn;pa=pa->next;pb=pb->next;break;case 1:last->coef=pb->coef;last->expn=pb->expn;pb=pb->next;break;}}if(pa){while(pa){/*把剩余节点插入*/CreateItem(s);last->next=s;last=s;last->coef=pa->coef;last->expn=pa->expn;pa=pa->next;}} if(pb){while(pb){/*把剩余节点插入*/CreateItem(s);last->next=s;last=s;last->coef=pb->coef;last->expn=pb->expn;pb=pb->next;}} last->next=NULL;return head;}/************************************************************//* 两多项式多项式相减 *//************************************************************/Polyn SubtractPolyn(Polyn h1,Polyn h2){ Item *head,*last,*pa=h1->next,*pb=h2->next,*s;CreateItem(head);head->next=NULL;last=head;while(pa&&pb){CreateItem(s);last->next=s;last=s;switch (ItemComp(*pa,*pb)){case -1:last->coef=pa->coef;last->expn=pa->expn;pa=pa->next;break;case 0:last->coef=pa->coef-pb->coef;last->expn=pa->expn;pa=pa->next;pb=pb->next;break;case 1:last->coef=-(pb->coef);last->expn=pb->expn;pb=pb->next;break;}}if(pa){while(pa){/*把剩余节点插入*/CreateItem(s);last->next=s;last=s;last->coef=pa->coef;last->expn=pa->expn;pa=pa->next;}} if(pb){while(pb){/*把剩余节点插入*/CreateItem(s);last->next=s;last=s;last->coef=-(pb->coef);last->expn=pb->expn;pb=pb->next;}}last->next=NULL;return head;}/************************************************************//* 两多项式多项式相乘 *//************************************************************/Polyn MultPolyn(Polyn h1,Polyn h2) //两个多项式相乘{ int item,expn;Item *head,*pa,*pb=h2->next,*s,*pp;double coef;CreateItem(head);head->next=NULL;while(pb){pa=h1->next;while(pa){expn=pa->expn+pb->expn;coef=pa->coef*pb->coef;if(InsertLocate(head,expn,&pp)){CreateItem(s);s->coef=coef;s->expn=expn;insert(pp,s);}elsepp->coef=pp->coef+pa->coef;pa=pa->next;}pb=pb->next;}return head;}/************************************************************//* 菜单选择 *//************************************************************/int menu(void){ int num;system("cls");printf("%20c1--create P(x)\n",' ');printf("%20c2--create Q(x)\n",' ');printf("%20c3--p(x)+Q(x)\n",' ');printf("%20c4--P(x)-Q(x)\n",' ');printf("%20c5--p(x)*Q(x)\n",' ');printf("%20c6--print P(x)\n",' ');printf("%20c7--print Q(x)\n",' ');printf("%20c8--print P(x)+Q(x)\n",' ');printf("%20c9--print P(x)-Q(x)\n",' ');printf("%20c10--print P(x)*Q(x)\n",' ');printf("%20c11--Quit\n",' ');printf(" please select 1,2,3,4,5,6,7,8,9,10,11:");do{scanf("%d",&num);}while(num<1 || num>11);return(num);}/************************************************************//* 判断多项式是否存在 *//************************************************************/int PolynNotEmpty(Polyn h,char *p){ if(h==NULL){ printf("%s is not exist!\n",p);getchar();return(0);}else return(1);}/************************************************************//* 主函数 *//************************************************************/ void main(){ int num;Polyn h1=NULL; //指向p(x) 指针Polyn h2=NULL; //指向Q(x) 指针Polyn h3=NULL; //指向P(x)+Q(x) 指针Polyn h4=NULL; //指向P(x)-Q(x) 指针Polyn h5=NULL; //指向P(x)*Q(x) 指针while(1){ num=menu();getchar();switch(num){case 1: //输入第一个多项式,若多项式存在,首先撤消然后再输入if(h1!=NULL){ if(Select("P(x) is not Empty,Create P(x) again?")) { Destroy(h1);h1=Input();}}else h1=Input();break;case 2: //输入第二个多项式,若多项式存在,首先撤消然后再输入if(h2!=NULL){ if(Select("Q(x) is not Empty,Create Q(x) again?")) { Destroy(h2);h2=Input();}}else h2=Input();break;case 3: //两多项式相加if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)"))//判断俩多项式都非空 {h3=AddPolyn(h1,h2);Output(h3,"P(x)+Q(X)");printf("P(x)+Q(x) has finished!\n");getchar();}break;case 4: //两多项式相减if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)")){ h4=SubtractPolyn(h1,h2);Output(h4,"P(x)-Q(x)");printf("P(x)-Q(x) has finished!\n");getchar();}break;case 5: //两多项式相乘if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)")) { h5=MultPolyn(h1,h2);Output(h5,"P(x)*Q(x)");printf("P(x)*Q(x) has finished!\n");getchar();}break;case 6: //显示第一个多项式if(PolynNotEmpty(h1,"p(x)")){ Output(h1,"P(x)");getchar();}break;case 7: //显示第二个多项式if(PolynNotEmpty(h2,"Q(x)")){ Output(h2,"Q(x)");getchar();}break;case 8: //显示相加结果多项式if(PolynNotEmpty(h3,"P(x)+Q(x)")){ Output(h3,"P(x)+Q(x)");getchar();}break;case 9: //显示相减结果多项式if(PolynNotEmpty(h4,"P(x)-Q(x)")){ Output(h4,"P(x)-Q(x)");getchar();}break;case 10: //显示相乘结果多项式if(PolynNotEmpty(h5,"P(x)*Q(x)")){ Output(h5,"P(x)*Q(x)");getchar();}break;case 11: //结束程序运行。
c语言多项式的相加
C语言中,多项式的相加是一种常见的运算。
多项式是指由若干项组成的代数式,每一项都有系数和指数。
例如,3x^2 + 4x + 5 就是一个多项式,其中3、4、5分别是系数,x^2、x分别是指数。
要实现多项式的相加,需要按照指数从高到低的顺序依次将相同指数的项相加,最终得到一个简化后的多项式。
具体实现可以使用数组来存储多项式的系数,数组下标表示指数。
以下是一个简单的C语言函数,用于实现两个多项式的相加: ```
void add_poly(int a[], int b[], int n) {
int i;
for (i = 0; i < n; i++) {
a[i] += b[i];
}
}
```
其中,a和b分别表示两个多项式的系数数组,n表示数组的长度(即最高指数加一)。
函数的实现很简单,就是依次将a和b对应位置的系数相加。
以上就是C语言中多项式相加的简单介绍。
如果需要实现更复杂的多项式运算,可以考虑使用链表等数据结构。
- 1 -。
C语言题目-多项式加法多项式加法(10分)题目内容:一个多项式可以表达为x的各次幂与系数乘积的和,比如:2x6+3x5+12x3+6x+20现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出。
程序要处理的幂最大为100。
输入格式:总共要输入两个多项式,每个多项式的输入格式如下:每行输入两个数字,第一个表示幂次,第二个表示该幂次的系数,所有的系数都是整数。
第一行一定是最高幂,最后一行一定是0次幂。
注意第一行和最后一行之间不一定按照幂次降低顺序排列;如果某个幂次的系数为0,就不出现在输入数据中了;0次幂的系数为0时还是会出现在输入数据中。
输出格式:从最高幂开始依次降到0幂,如:2x6+3x5+12x3-6x+20注意其中的x是小写字母x,而且所有的符号之间都没有空格,如果某个幂的系数为0则不需要有那项。
输入样例:6 25 33 121 60 206 25 32 121 60 20输出样例:4x6+6x5+12x3+12x2+12x+40时间限制:500ms内存限制:32000kb代码#include <stdio.h> //此程序需要分3种情况,一、幂为0;二、幂为1;三、幂大于1int a[105],b[105];int main(){intx,y;while (~scanf("%d%d",&x,&y)){//存储第一个多项式的数据,当幂为0时停止输入a[x]=y;if (x==0) break;}while (~scanf("%d%d",&x,&y)){//存储第二个多项式的数据,当幂为0时停止输入b[x]=y;if (x==0) break;}for (int i=0;i<=100;i++)a[i]=a[i]+b[i]; //将两个多项式的系数相加保存到数组a[i]中int flag=0; //用此数来区分输出的式子是否是第一个数字,是第一个式子flag就为0,不是第一个式子flag就为1for (int i=20;i>1;i--){if (a[i]){ //系数不为0if (a[i]>0){ //系数大于0的情况if (a[i]==1){ //系数等于1的情况if (flag==0){//输出的式子是第一个式子printf("x%d",i);flag=1;}else printf("+x%d",i); //输出的式子不是第一个式子}else{ //系数大于1的情况if (flag==0){printf("%dx%d",a[i],i);flag=1;}elseprintf("+%dx%d",a[i],i);}}else{ //系数小于0的情况if (a[i]==-1)printf("-x%d",i);elseprintf("%dx%d",a[i],i);}}}if (a[1]){if (a[1]>0){if (a[1]==1){if (flag==0){printf("x");flag=1;} elseprintf("+x");}else{if (flag==0){printf("%dx",a[1]); flag=1;} elseprintf("+%dx",a[1]);}}else{if (a[1]==-1)printf("-x");elseprintf("%dx",a[1]);}}if (a[0]>0){if (flag==0){printf("%d",a[0]);flag=1;}elseprintf("+%d",a[0]);}if (a[0]<=0){printf("%d",a[0]);}printf("\n");return 0;}【下载本文档,可以自由复制内容或自由编辑修改内容,更多精彩文章,期待你的好评和关注,我将一如既往为您服务】。
程序设计实践(大作业)两个多项式求和问题描述:通常在数学中对一元n次多项式可表示成如下形式:给定一个多项式,在C语言中可以用可读的形式将该多项式规范地输出。
例如,给出系数1 5 22 4 33 3 1 1 -1 0,输出为x^5+22x^4+33x^3+x-1。
所使用的规则如下:(1)多项式各项按照指数的递减顺序输出(2)次数出现在^之后,如3x^2(3)常数项仅输出常数。
(4)如果所有的项都是以0作为系数,则仅输出常数0,否则输出非零系数的项。
(5)如果第1项的系数是正数,在该项前没有符号;如果第1项的系数是负数,在该项前是减号,如2x^4+3x^2-10,-2x^4+3x^2-10。
(6)非常数项系数为1和-1不用显示,如x^7-10x^2,-x^2-x+2。
对每一项输入系数和指数。
按指数从高到低的顺序输入。
系数和指数的大小都在【-100,+100】之间。
输入-9999时一个多项式结束。
输出:先输出2个多项式,再输出求和后的多项式。
每个式子占据一行。
每一组输入对应的输出包括三行,包括2个多项式以及求和后的多项式。
每个式子占据一行。
例:Input:1 32 2 -8 0 -99991 1023 6 0 -99991 172 10 -9 5 0 2 23 -1 -1 -3 -9999-1 17 -3 9 2 6 7 5 3 -1 -9999Output:A:x^3+2x^2-8B:x^10+2x^3+6A+B:x^10+3x^3+2x^2-2A:x^17+2x^10-9x^5+23x^-1-x^-3B:-x^17-3x^9+2x^6+7x^5+3x^-1A+B:2x^10-3x^9+2x^6-2x^5+26x^-1-x^-3代码:#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;int main(){int x,y; //cishu +100;int i=0;int couna=0,counb=0;int na[300];int nb[300];int nc[300];int flag;while (scanf("%d",&x)!=EOF){flag=0;memset(na,0,sizeof(na));memset(nb,0,sizeof(nb));memset(nc,0,sizeof(nc));while (x!=-9999){scanf("%d",&y);na[y+100]=x;couna++;scanf("%d",&x);}scanf("%d",&x);while (x!=-9999){scanf("%d",&y);nb[y+100]=x;counb++;scanf("%d",&x);}printf("A:");for(i=200;i>=0;i--){if(flag==0&&na[i]!=0){if(i==100){if(na[i]>0)p rintf("%d",na[i]);else printf("%d",na[i]);}else if(i==101){if(na[i]==1)printf("x");else if(na[i]==-1)printf("-x");else if(na[i]>0)printf("%dx",na[i]);elseprintf("%dx",na[i]);}else if(na[i]==1)printf("x^%d",i-100);else if(na[i]==-1)printf("-x^%d",i-100);else if(na[i]>0)printf("%dx^%d",na[i],i-100);elseprintf("%dx^%d",na[i],i-100);flag++;}else if(na[i]!=0){if(i==100){if(na[i]>0)p rintf("+%d",na[i]);else printf("%d",na[i]);}else if(i==101){if(na[i]==1)printf("+x");else if(na[i]==-1)printf("-x");else if(na[i]>0)printf("+%dx",na[i]);elseprintf("%dx",na[i]);}else if(na[i]==1)printf("+x^%d",i-100);else if(na[i]==-1)printf("-x^%d",i-100);else if(na[i]>=0)printf("+%dx^%d",na[i],i-100);elseprintf("%dx^%d",na[i],i-100);}}if(flag==0)printf("\nB:");flag=0;for(i=200;i>=0;i--){if(flag==0&&nb[i]!=0){if(i==100){if(nb[i]>0)printf("%d",nb[i]);else printf("%d",nb[i]);}else if(i==101){if(nb[i]==1)printf("x");else if(nb[i]==-1)printf("-x");else if(nb[i]>0)printf("%dx",nb[i]);elseprintf("%dx",nb[i]);}else if(nb[i]==1)printf("x^%d",i-100);else if(nb[i]==-1)printf("-x^%d",i-100);else if(nb[i]>=0)printf("%dx^%d",nb[i],i-100);elseprintf("%dx^%d",nb[i],i-100);flag++;}else if(nb[i]!=0){if(i==100){if(nb[i]>0)printf("+%d",nb[i]);else printf("%d",nb[i]);}else if(i==101){if(nb[i]==1)else if(nb[i]==-1)printf("-x");else if(nb[i]>0)printf("+%dx",nb[i]);elseprintf("%dx",nb[i]);}else if(nb[i]==1)printf("+x^%d",i-100);else if(nb[i]==-1)printf("-x^%d",i-100);else if(nb[i]>=0)printf("+%dx^%d",nb[i],i-100);elseprintf("%dx^%d",nb[i],i-100);}}if(flag==0)printf("0");for(i=0;i<=200;i++){nb[i]=na[i]+nb[i];}flag=0;printf("\nA+B:");for(i=200;i>=0;i--){if(flag==0&&nb[i]!=0){if(i==100){if(nb[i]>0)printf("%d",nb[i]);else printf("%d",nb[i]);}else if(i==101){if(nb[i]==1)printf("x");else if(nb[i]==-1)printf("-x");else if(nb[i]>0)printf("%dx",nb[i]);elseprintf("%dx",nb[i]);}else if(nb[i]==1)printf("x^%d",i-100);else if(nb[i]==-1)printf("-x^%d",i-100);else if(nb[i]>=0)printf("%dx^%d",nb[i],i-100);elseprintf("%dx^%d",nb[i],i-100);flag++;}else if(nb[i]!=0){if(i==100){if(nb[i]>0)printf("+%d",nb[i]);else printf("%d",nb[i]);}else if(i==101){//printf("nb[i]:%d\n",nb[i]);if(nb[i]==1)printf("+x");else if(nb[i]==-1)printf("-x");else if(nb[i]>0)printf("+%dx",nb[i]);elseprintf("%dx",nb[i]);}else if(nb[i]==1)printf("+x^%d",i-100);else if(nb[i]==-1)printf("-x^%d",i-100);else if(nb[i]>=0)printf("+%dx^%d",nb[i],i-100);elseprintf("%dx^%d",nb[i],i-100);}}if(flag==0)printf("0");printf("\n");}return 0;}运行结果:。