多项式加减法_第1页
多项式加减法_第2页
多项式加减法_第3页
多项式加减法_第4页
多项式加减法_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

一实验目的设计一个一元稀疏多项式简单计算器。二、实验要求能创建和实现多项式的加减等各种功能。三、实验内容一元稀疏多项式的基本功能:输入并建立多项式;输出多项式,输出的形式为整数序列:n,c1,e1,c2,e2,……,cn,en,其中n是多项式的项数,ci和ei分别是第i项的系数和指数,序列按指数降序排列。多项式a和b相加,建立多项式a+b;多项式a和b相加,建立多项式a-b;四、程序源代码#include<stdio.h>#include<malloc.h>#defineNULL0typedefstruct LNode{ floatcoef; intexpn; structLNode*next;}LNode,*Linklist;/////////////////////////////////////////////////LNode*creat(intcount)///////////////////////////创建链表,存放多项式{ Linklistp1,p2,head; intn=0; p1=p2=(Linklist)malloc(sizeof(LNode)); scanf("%f%d",&p1->coef,&p1->expn); head=NULL; while(n<count) { n=n+1; if(n==1)head=p1; elsep2->next=p1; p2=p1; p1=(Linklist)malloc(sizeof(LNode)); if(n!=count) { scanf("%f%d",&p1->coef,&p1->expn); } } p2->next=NULL; return(head);}Linklistsort(Linklisthead)////////////////////////对多项式按指数降序排序{ Linklistp0,p1,p2; inti=0; ints; floatf; p0=p1=head; while(p0) { i++; p0=p0->next; } for(intj=0;j<i-1;j++) { p1=head; while(p1->next) { p2=p1; p1=p1->next; if(p1->expn>p2->expn) { f=p1->coef; s=p1->expn; p1->coef=p2->coef; p1->expn=p2->expn; p2->coef=f; p2->expn=s; } } } returnhead;}/////////////////////////////////////////////////////合并系数相同的项Linklistmerge(Linklisthead){ Linklistp1,p2; p1=p2=head; while(p1) { p2=p1; p1=p1->next; while(p1&&p1->expn==p2->expn) { p2->coef+=p1->coef; p2->next=p1->next; free(p1); p1=p2->next; } } returnhead;}/////////////////////////////////////////////////////输入多项式各项系数及指数voidprint(Linklisthead){ Linklistp,q; intcount=0; q=head; while(q) { q=q->next; count++; } printf("多项式中的各项系数指数依次为:\n"); printf("%d",count); p=head; if(head==NULL) printf("所得结果为0!\n"); if(head!=NULL) do {printf("\t%4.2fX%d",p->coef,p->expn); p=p->next; }while(p!=NULL); printf("\n");}///////////////////////////////////////////////////////出去系数为零的项Linklistdel_Zero(Linklisthead){ Linklistp1,p2; if(head==NULL) { printf("\nlistnull!\n");returnhead; } p1=p2=head; while(p1) { p2=p1; p1=p1->next; while(p1&&p1->coef==0) { if(p1==head)head=p1->next; else { p2->next=p1->next; p1=p2->next; } } } if(head->coef==0) head=head->next; return(head);}///////////////////////////////////////////////////////连接函数LinklistList_Link(LinklistL1,LinklistL2){ Linklistp; p=L1; while(p&&p->next) p=p->next; if(p==NULL) L1=L2; else p->next=L2; L2=NULL; returnL1;}voidmain(){ intcount1,count2; LinklistL1,L2,p; printf("输入第一个多项式的项数:\n"); scanf("%d",&count1); while(count1<0) { printf("输入不合法,重新输入:\n"); scanf("%d",&count1); } printf("依次输入各项的系数和指数:\n"); L1=creat(count1); sort(L1); //print(L1); printf("输入第二个多项式的项数:\n"); scanf("%d",&count2); while(count2<0) { printf("输入不合法,重新输入:\n"); scanf("%d",&count2); } printf("依次输入各项的系数和指数:\n"); L2=creat(count2); sort(L2); //print(L2); intchioce; printf("//////////////////////////////////////////////\n"); printf("多项式相加输入1,多项式做差输入2:\n"); scanf("%d",&chioce); switch(chioce) { case1: L1=List_Link(L1,L2);break; case2: p=L2; while(p) { p->coef=-p->coef; p=p->next; } L1=List_Link(L1,L2);break; default:

温馨提示

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

评论

0/150

提交评论