C语言实现一元多项式相加(源代码).doc_第1页
C语言实现一元多项式相加(源代码).doc_第2页
C语言实现一元多项式相加(源代码).doc_第3页
C语言实现一元多项式相加(源代码).doc_第4页
C语言实现一元多项式相加(源代码).doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C语言实现一元多项式相加(源代码)#include#include/动态申请空间的函数的头文件typedef struct node /定义节点类型float coef; /多项式的系数 int expn; /多项式的指数 struct node * next; /结点指针域PLOYList;void insert(PLOYList *head,PLOYList *input) /查找位置插入新链节的函数,且让输入的多项式呈降序排列 PLOYList *pre,*now; int signal=0; pre=head; if(pre-next=NULL) pre-next=input; /如果只有一个头结点,则把新结点直接连在后面 elsenow=pre-next;/如果不是只有一个头结点,则设置now指针 while(signal=0) if(input-expn expn) if(now-next=NULL) now-next=input; signal=1; else pre=now; now=pre-next;/始终让新输入的数的指数与最后一个结点中的数的指数比较,小于则插在其后面 else if( input-expn now-expn ) input-next=now; pre-next=input; signal=1; /若新结点中指数比最后一个结点即now中的指数大,则插入now之前 else/若指数相等则需合并为一个结点,若相加后指数为0则释放该结点 now-coef=now-coef+input-coef; signal=1; free(input); if(now-coef=0) pre-next=now-next; free(now); /else /while/else/voidPLOYList *creat(char ch) /输入多项式 PLOYList *head,*input; float x; int y; head=(PLOYList *)malloc(sizeof(PLOYList); /创建链表头 head-next=NULL; scanf(%f %d,&x,&y);/实现用户输入的第一个项,包括其指数和系数 while(x!=0)/当用户没有输入结束标志0时可一直输入多项式的项,且输入一个创建一个结点 input=(PLOYList *)malloc(sizeof(PLOYList); /创建新链节 input-coef=x; input-expn=y; input-next=NULL; insert(head,input); /每输入一项就将其排序,是的链表中多项式呈降序排列 scanf(%f %d,&x,&y); return head;PLOYList *add(PLOYList *head,PLOYList *pre) /多项式相加,head为第一个多项式建立的链表表头,pre为第二个多项式建立的链表表头 PLOYList *input; int flag=0; while(flag=0) if(pre-next=NULL) flag=1; /若该链表为空,则无需进行加法运算,跳出循环 else pre=pre-next; input=(PLOYList *)malloc(sizeof(PLOYList);/申请空间 input-coef=pre-coef; input-expn=pre-expn; input-next=NULL; insert(head,input); / 把g(x)插入到f(x)中,相当于两者相加,结果保存于f(x) return head;void print(PLOYList *fun) /输出多项式,fun指要输出的多项式链表的表头 PLOYList *printing; int flag=0; printing=fun-next; if(fun-next=NULL)/若为空表,则无需输出 printf(0n); return; while(flag=0) if(printing-coef0&fun-next!=printing) printf(+); if(printing-coef=1); else if(printing-coef=-1) printf(-); else printf(%f,printing-coef); if(printing-expn!=0) printf(x%d,printing-expn); else if(printing-coef=1)|(printing-coef=-1) printf(1); if(printing-next=NULL) flag=1; else printing=printing-next; printf(n);void start() /用户选择界面 printf( * 1.两个一元多项式相加 *n); printf( * 0.退出系统 *n); printf( n); printf( 注:输入多项式格式为:系数1 指数1 系数2 指数2 ,并以0 0 结束:n); printf( n); printf( 请选择操作: );void main()PLOYList *f,*g,*pf,*hf,*p; int sign=-1; start(); while(sign!=0) scanf(%d,&sign); switch(sign) case 0: break; case 1:/多项式相加 printf( 你选择的操作是多项式相加:n); printf( 请输入第一个多项式f(x):); f=creat(f); printf( 第一个多项式为:f(x)=); print(f); printf( 请输入第二个多项式g(x):); g=creat(g); print

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论