简单多项式运算器

上传人:子 文档编号:42843438 上传时间:2018-06-03 格式:DOC 页数:17 大小:19.17KB
返回 下载 相关 举报
简单多项式运算器_第1页
第1页 / 共17页
简单多项式运算器_第2页
第2页 / 共17页
简单多项式运算器_第3页
第3页 / 共17页
简单多项式运算器_第4页
第4页 / 共17页
简单多项式运算器_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《简单多项式运算器》由会员分享,可在线阅读,更多相关《简单多项式运算器(17页珍藏版)》请在金锄头文库上搜索。

1、简单多项式运算器简单多项式运算器/*说明:本程序是利用结构体数组申请 SIZE 个头结点,用来保存最多 SIZE 个多项式,每个多项式给一个地址(0SIZE-1) ,在操作时需要给多项式一个保存地址。*/#include#include#define SIZE 10/定义结点 Linklist 指链表头指针类型,Link 指结点指针类型typedef struct LNodefloat coef;int expn;struct LNode *next;LNode,*Linklist,*Link;/构造结点LNode *MakeNode(float coef, int expn)Link p;p

2、=(LNode*)malloc(sizeof(LNode);if(!p)return false;/分配失败p-coef=coef;p-expn=expn;p-next=NULL;return p;/初始化一个空链表,也就是一个空的多项式,头结点为 0x-1void InitPolyn(Linklist /顺序插入结点,同类项合并void OrderInsert(Linklist while(p-nextif(p-nextif(p-next-coef=0)t=p-next;p-next=p-next-next;free(t);free(s);elses-next=p-next;p-next=s

3、; /按升幂排列创建多项式,按降幂排列从键盘输入指数和系数void CreatePolyn(Linklist float c; int e;InitPolyn(L); coutce;if(enext)coutnext;while(p)coutcoefexpn;if(p-nextcoutnext;while(p)OrderInsert(La,MakeNode(p-coef,p-expn);/复制接点并且插入 Lap=p-next;return La;/求多项式的项数,即表长,并返回表长int LengthPolyn(Linklist La)Link p;int e=0;if(!La)return

4、(0);/La 不存在if(!La-next)return e;p=La;while(p-next)e=e+1;p=p-next;return e;/多项式求和int AddPolyn(Linklist La, Linklist Lb, Linklist InitPolyn(Lc);if(Laq=Lb-next;elsereturn(0);while(pp=p-next;elset=MakeNode(q-coef,q-expn);q=q-next;OrderInsert(Lc,t);elset=MakeNode(p-coef+q-coef,p-expn);if(t-coef=0)p=p-nex

5、t;q=q-next;elseOrderInsert(Lc,t);p=p-next;q=q-next;/将 t 结点插入 Lcif(!p)r=q;else if(!q)r=p;while(r)t=MakeNode(r-coef,r-expn);OrderInsert(Lc,t);r=r-next;return(1);/多项式求差 La-Lbint SubPolyn(Linklist La, Linklist Lb, Linklist InitPolyn(Lc);if(Laelsereturn 0;while(p)p-coef=p-coef*-1;p=p-next; /相减之后 Lb 不可还原A

6、ddPolyn(La,Lb,Lc);p=Lb-next;while(p)p-coef*=-1;p=p-next;return 1;/多项式赋值值double ValuePolyn(Linklist La,double int i;float m,a=1;e=0;coutm;if(!La)return 0;if(!La-next)return e;p=La-next;while(p)for(i=0;iexpn;i+)a=a*m;e+=p-coef*a;a=1;p=p-next;/逐项求值,累项相加return e;/销毁多项式int DestroyPolyn(Linklist if(!La)re

7、turn false;p=q=La;while(q)q=q-next;free(p);p=q;La=q;return(1);/清空多项式int ClearPolyn(Linklist if(!La)return false;p=q=La-next;while(q)q=q-next;free(p);p=q;La-next=q;return 1;/删除 La 中的一个结点,指数为 mint DeleteNode(Linklist La, int m)Link p,q;if(!La|!La-next)return -1;p=La;q=p-next;while(qq=q-next;if(!q)retu

8、rn -1;elsep-next=q-next;free(q);return 1;/用 m 返回 La 的最高项次数int TopPolyn(Linklist La)Link p;int m;if(!La)return false;elsep=La;if(!p-next)m=0;elsewhile(p-next)p=p-next;m=p-expn;return m;/求 La 和 Lb 的乘积并用 Lc 返回其值Linklist MultiplyPolyn(Linklist La, Linklist Lb, Linklist if(!Lc)InitPolyn(Lc);if(!La|!Lb)re

9、turn false;if(!La-next|!Lb-next)OrderInsert(Lc,MakeNode(0,0);return Lc;p=La-next;q=Lb-next;while(p)while(q)OrderInsert(Lc,MakeNode(p-coef*q-coef,p-expn+q-expn);q=q-next;q=Lb-next;p=p-next;return Lc;/用 p 返回 L 的最高次项的指针Link ToppointerPolyn(Linklist L)Link p;if(!L|!L-next)return NULL;elsep=L-next;while(

10、p-next)p=p-next;return p;/本地作减法,用 La 减去 Lb,返回 La 的值int SubPolynloc(Linklist if(!La|!Lb)return 0;SubPolyn(La,Lb,L);ClearPolyn(La);CopyPolyn(La,L);return 1;void main(void)Linklist PSIZE=NULL;int menu,i,a1,a2,a3,a4,a5,a7,a10;double e;while(1)coutmenu;switch(menu)case 0:couta1;if(Pa1)couta3;if(Pa3)couta1a2;AddPolyn(Pa1,Pa2,Pa3);couta4;if(Pa4)couta1a2;SubPolyn(Pa1,Pa2,Pa4);couta1;if(Pa1)couta2a3;MultiplyPolyn(Pa2,Pa3,Pa1);couta5;if(!Pa5)couta10;couta7;DestroyPolyn(Pa7);cout“销毁完毕!“endl;system(“pause“);break;default:cout“对不起,您输入的功能编号有错!“endl;break;system(“cls“);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号