C语言一元多项式加法.doc

上传人:公**** 文档编号:561515366 上传时间:2023-11-29 格式:DOC 页数:4 大小:21.01KB
返回 下载 相关 举报
C语言一元多项式加法.doc_第1页
第1页 / 共4页
C语言一元多项式加法.doc_第2页
第2页 / 共4页
C语言一元多项式加法.doc_第3页
第3页 / 共4页
C语言一元多项式加法.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《C语言一元多项式加法.doc》由会员分享,可在线阅读,更多相关《C语言一元多项式加法.doc(4页珍藏版)》请在金锄头文库上搜索。

1、/C语言数据结构_一元多项式加法#include #include #include #define OK 1#define ERROR 0typedef int Status;typedef int ElemType;typedef struct LNode /定义结构体ElemType coef;ElemType exp;struct LNode *next;LNode,*Linklist;Status CreateList_L(Linklist &L) /初始化链表L=(Linklist)malloc(sizeof(LNode);L-next=NULL;return OK;Status

2、InsertList_L(Linklist &L,int i,ElemType c,ElemType e) /在链表中i位置插入元素eLinklist p=L,s;int j=0;while(p-next!=NULL&jnext;j+;s=(Linklist)malloc(sizeof(LNode);s-coef=c;s-exp=e;s-next=p-next;p-next=s;return OK;Status AddList_L(Linklist &La,Linklist Lb,int len) /将b中的所有项加到a中Linklist p=Lb;int j=len; while(p-nex

3、t!=NULL)p=p-next; InsertList_L(La,j,p-coef,p-exp);j+;return OK;Status GetLength(Linklist L,int &len) /检查链表长度Linklist p=L;while(p-next!=NULL)p=p-next;len+;return OK;Status PrintList_L(Linklist L) /输出链表 Linklist p=L;if(p=NULL) return ERROR;printf(两多项式的和为n);while(p-next!=NULL)p=p-next;if(p-coef!=0) pri

4、ntf(%d,%d ,p-coef,p-exp);putchar(n);return OK;Status ReorderList_L(Linklist &L) /将链表按指数从小到大排序Linklist p=L,q=L;ElemType temp; for(p=L;p-next!=NULL;p=p-next)for(q=p;q!=NULL;q=q-next)if(p-expq-exp)temp=p-exp; p-exp=q-exp; q-exp=temp; temp=p-coef; p-coef=q-coef; q-coef=temp;return OK;Status CheckList_L(

5、Linklist &L) /按指数由小到大排序后,合并同类项Linklist p=L,s;ReorderList_L(p);p=p-next;while(p-next!=NULL)if(p-next-exp=p-exp)p-coef+=p-next-coef;s=(Linklist)malloc(sizeof(LNode);s=p-next;p-next=s-next;free(s);elsep=p-next;return OK;void main()Linklist La,Lb;CreateList_L(La);CreateList_L(Lb);int lena=0,i=0;ElemType

6、 tempcoef,tempexp;printf(请逐项输入系数与指数,格式为a,b(a为系数,b为指数)。输入0,0结束n);printf(请输入第一个一元多项式n);while(1) scanf(%d,%d,&tempcoef,&tempexp);if(tempcoef=0&tempexp=0)break;if(tempcoef!=0)i+;InsertList_L(La,i,tempcoef,tempexp);i=0;printf(请输入第二个一元多项式n);while(1) scanf(%d,%d,&tempcoef,&tempexp);if(tempcoef=0&tempexp=0)break;if(tempcoef!=0)i+;InsertList_L(Lb,i,tempcoef,tempexp);GetLength(La,lena);AddList_L(La,Lb,lena);CheckList_L(La); PrintList_L(La);free(La);free(Lb);

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

当前位置:首页 > 生活休闲 > 社会民生

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