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

C语言一元多项式加法

//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 InsertList_L(Linklist &L,int i,ElemType c,ElemType e) //在链表中i位置插入元素e{Linklist 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->next!=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) printf("%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->exp>q->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(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);}else{p=p->next;}}return OK;}void main(){Linklist La,Lb;CreateList_L(La);CreateList_L(Lb);int lena=0,i=0;ElemType tempcoef,tempexp;printf("请逐项输入系数与指数,格式为a,b(a为系数,b为指数)。输入0,0结束\n");printf("请输入第一个一元多项式\n");while(1){ scanf("%d,%d",&tempcoef,&tempexp);if(tempcoef==0&&tempex

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值