您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 一元稀疏多项式计算器(数据结构)
【问题描述】设计一个一元稀疏多项式简单计算器【基本要求】一元多项式简单计算器的基本功能是:1,输入并建立多项式;2,输出多项式,输出形式为整数序列:n,c1,e1,c2,c2,...,cn,en,其中n是多项式的项数,ci和ei分别是第i项的系数和指数,序列按指数降序排列;3,多项式a和b相加,建立多项式a+b;4,多项式a和b相减,建立多项式a-b.【测试数据】1,(2x+5x^8-3.1x^11)+(7-5x^8+11x^9)=(-3.1x^11+11x^9+2x+7)【实现提示】用带表头结点的单链表存储多项式。#includestdio.h#includemalloc.htypedefstructnode{floatcoef;intexpn;structnode*next;}Lnode,*polynmial;voidcreate(polynmial&L);//输入并建立多项式Lvoiddisplay(polynmialL);//显示,输出多项式Lvoidsort(polynmial&L);//多项式L按指数排序voidreverse(polynmial&L);//逆置voidselect();//用户选择加减操作voidadd(polynmialLa,polynmialLb,polynmial&Lc);//多项式La,Lb相加voidsubtract(polynmialLa,polynmialLb,polynmial&Ld);//多项式La减去Lb,结果给Ldvoidcreate(polynmial&L)//输入并建立多项式L{inti,n;staticstructnode*p;scanf(%d,&n);L=(structnode*)malloc(sizeof(structnode));L-next=NULL;for(i=0;in;i++){p=(structnode*)malloc(sizeof(structnode));scanf(%f%d,&p-coef,&p-expn);p-next=L-next;L-next=p;}}voiddisplay(polynmialL)//显示,输出多项式L{structnode*p,*q;intflag=0;intk=0;q=L-next;while(q){if(q-coef!=0)k++;q=q-next;}printf(%d,,k);p=L-next;if(p-coef!=0){printf(%.1f,%d,,p-coef,p-expn);flag++;}for(p=p-next;p;p=p-next){if(p-coef!=0){printf(%.1f,%d,,p-coef,p-expn);flag++;}}if(flag==0)printf(%d\n,flag);elseprintf(\n);}voidsort(polynmial&L)//多项式L按指数排序{polynmialp,q,r,u;p=L-next;L-next=NULL;while(p!=NULL){r=L;q=L-next;while((q!=NULL)&&(q-expn=p-expn)){r=q;q=q-next;}u=p-next;r-next=p;p-next=q;p=u;}}voidreverse(polynmial&L)//逆置{polynmialH;staticstructnode*p,*q,*s;H=(structnode*)malloc(sizeof(structnode));H-next=NULL;p=(structnode*)malloc(sizeof(structnode));s=L-next;p-coef=s-coef;p-expn=s-expn;p-next=s-next;while(s){p-coef=s-coef;p-expn=s-expn;p-next=s-next;q=H-next;H-next=p;p-next=q;p=(structnode*)malloc(sizeof(structnode));s=s-next;}p=H-next;q=L-next;while(p){q-coef=p-coef;q-expn=p-expn;q=q-next;p=p-next;}}voidselect()//用户选择加减操作{printf(请选择加减操作\n);printf(1.两个一元多项式相加\n);printf(2.两个一元多项式相减\n);}voidadd(polynmialLa,polynmialLb,polynmial&Lc)//多项式La,Lb相加{structnode*pa,*pb;staticstructnode*pc;Lc=(structnode*)malloc(sizeof(structnode));pa=La-next;pb=Lb-next;Lc-next=NULL;while(pa&&pb){pc=(structnode*)malloc(sizeof(structnode));if(pa-expnpb-expn){pc-next=Lc-next;Lc-next=pc;pc-coef=pa-coef;pc-expn=pa-expn;pa=pa-next;}elseif(pa-expn==pb-expn){pc-next=Lc-next;Lc-next=pc;pc-expn=pa-expn;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-expn=pb-expn;pb=pb-next;}}while(pa){pc=(structnode*)malloc(sizeof(structnode));pc-next=Lc-next;Lc-next=pc;pc-coef=pa-coef;pc-expn=pa-expn;pa=pa-next;}while(pb){pc=(structnode*)malloc(sizeof(structnode));pc-next=Lc-next;Lc-next=pc;pc-coef=pb-coef;pc-expn=pb-expn;pb=pb-next;}}voidsubtract(polynmialLa,polynmialLb,polynmial&Ld)//多项式La减去Lb,结果给Ld{structnode*pa,*pb;staticstructnode*pd;Ld=(structnode*)malloc(sizeof(structnode));pa=La-next;pb=Lb-next;Ld-next=NULL;while(pa&&pb){pd=(structnode*)malloc(sizeof(structnode));if(pa-expnpb-expn){pd-next=Ld-next;Ld-next=pd;pd-coef=pa-coef;pd-expn=pa-expn;pa=pa-next;}elseif(pa-expn==pb-expn){pd-next=Ld-next;Ld-next=pd;pd-expn=pa-expn;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-expn=pb-expn;pb=pb-next;}}while(pa){pd=(structnode*)malloc(sizeof(structnode));pd-next=Ld-next;Ld-next=pd;pd-coef=pa-coef;pd-expn=pa-expn;pa=pa-next;}while(pb){pd=(structnode*)malloc(sizeof(structnode));pd-next=Ld-next;Ld-next=pd;pd-coef=-pb-coef;pd-expn=pb-expn;pb=pb-next;}}intmain(){intsign;polynmialLa,Lb,Lc,Ld;printf(请输入第一个多项式:\n);create(La);sort(La);printf(请输入第二个多项式:\n);create(Lb);sort(Lb);select();scanf(%d,&sign);switch(sign){case1:printf(多项式之和为:\n);add(La,Lb,Lc);sort(Lc);reverse(Lc);display(Lc);break;default:printf(多项式之差为:\n);subtract(La,Lb,Ld);sort(Ld);reverse(Ld);display(Ld);break;}return0;}
本文标题:一元稀疏多项式计算器(数据结构)
链接地址:https://www.777doc.com/doc-5743570 .html