当前位置:文档之家› 数据结构实验,多项式计算器

数据结构实验,多项式计算器

实验题目:多项式运算器实验内容:1、熟悉编程环境2、用链式存储结构实现稀疏一元多项式运算器实验目的和要求:1、通过本实验,掌握VC++6.0的基本使用,包括源程序的输入,编译运行及调试。

调试的目的是找程序的运行错误,通过DEBUG菜单设置断点实现。

2、用链式存储结构实现一元多项式的运算。

熟练掌握指针和链表的基本操作,利用菜单进行功能选择。

功能基本要求:创建、显示、求和、求差、求值、销毁、清空、修改实验算法:1、数据结构描述:输入的稀疏每一个多项式用一个链表存储,链表的每一个节点存储多项式的一个非零项。

定义为LNode型结构体,其中保存该项的系数和指数。

主函数中用一个数组存储每一个多项式的第一项的头指针以调用多项式。

2、函数和算法描述:主函数main定义LNode*数组a[]存储每一个多项式头节点的地址,并构建菜单以选择调用函数进行多项式的操作,对多项式进行操作的函数返回新改动的多项式头结点地址。

Createpolyn函数用以创建一个多项式,在用户输入结束指令之前,不断的申请空间,构建LNode型结点并加至构建的链表的表尾,输入结束指令之后,表尾设NULL并返回表头指针。

Printpolyn函数用以在屏幕上打印多项式,接收需要打印的多项式链表的头结点,构造循环体在表尾之前,不断以mx^n格式打印对应结点中的数据。

Copypolyn函数用以复制多项式,接收需要复制的多项式a和复制位置b 的指针,构造循环体,在a到表尾之前循环,在b对应的链表中构建新的结点,结点上的数据赋值为a中对应的值,返回b的头结点地址。

Addpolyn函数用以求两个已知多项式a、b的和存入c,先构建循环体,在a、b链表都未进行到表尾时,比较两个结点中的次数值,如果相同,将系数相加赋于c的当前结点,如果不同,将次数较小多项式y(a或b)的结点赋值给c当前结点,在将y链表向后推。

Subtract函数用以求两多项式的差,类似求和算法。

Value函数用以求一个多项式的值,接收x的值,构建循环体,在表尾之前循环以遍历多项式链表。

内置循环体,以次数n为限循环,求出x^n的值,乘以系数并将每一项的值叠加得值。

Destroypolyn函数用以销毁已有的多项式,将此多项式链表的空间FREE 掉,返回空指针。

Clearpolyn函数用以清空多项式,构建循环体,将多项式的各项的系数、指数置零。

3、时空分析:L= sizeof(struct LNode)一个含有N项的多项式占用的储存空间为NL+1主要函数的时间复杂度:(N项多项式)Createpolyn:NPrintpolyn:NCopypolyn:NAddpolyn:N1+N2实验结果:控制菜单:显示多项式:多项式求值:求和、求差、复制、销毁、清空也均能得到正常结果源代码:#include<stdio.h>#include <stdlib.h>#define N 20#define L sizeof(struct LNode)struct LNode{int n;double m;struct LNode*next;};struct LNode* createpolyn (struct LNode*p){double i=1;int j,a=0;struct LNode*u;p=(struct LNode*)malloc(L);p->m=0;p->n=0;p->next=NULL;u=p;while(i!=0){printf("从低次到高次输入X的非零项的次数及系数:'0 0'表示结束:\n");scanf("%d %lf",&j,&i);if(i!=0){p->next=(struct LNode*)malloc(L);p=p->next;p->m=i;p->n=j;}};p->next=NULL;printf("completed!\n");return(u);}void printpolyn (struct LNode*p){p=p->next;while (p!=NULL){printf("+%lfX^%d\n",p->m,p->n);p=p->next;}//printf("NULL");}struct LNode* copypolyn (struct LNode*p,struct LNode*q){struct LNode* u;q=(struct LNode*)malloc(L);u=q;while (p->next!=NULL){q->m=p->m;q->n=p->n;p=p->next;q->next=(struct LNode*)malloc(L);q=q->next;}q->m=p->m;q->n=p->n;q->next=NULL;printf("completed!\n");return(u);}/*struct LNode* addpolyn (struct LNode*p,struct LNode*q,struct LNode*r) {struct LNode*u;r=(struct LNode*)malloc(L);u=r;while((q->next!=NULL)&&(p->next!=NULL)){if((q->n)>(p->n)){r->n=p->n;r->m=p->m;p=p->next;}else{if((q->n)==(p->n)){r->n=p->n;r->m=(p->m)+(q->m);p=p->next;q=q->next;}else{r->n=q->n;r->m=q->m;q=q->next;}}r->next=(struct LNode*)malloc(L);r=r->next;}if((q->next)==NULL){while((q->n>p->n)&&(p->next!=NULL)){ if((q->n)>(p->n)){r->n=p->n;r->m=p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}}if((q->n)<=(p->n)){if((q->n)==(p->n)){r->n=p->n;r->m=(p->m)+(q->m);}else{r->n=q->n;r->m=q->m;}}if(p->next!=NULL){r->next=(struct LNode*)malloc(L);r=r->next;p=p->next;while(p->next!=NULL){r->n=p->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r=r->next;p=p->next;}r->n=p->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r->next=NULL;}else{r->next=(struct LNode*)malloc(L);r->next=NULL;}}else{ if((q->n)>(p->n)){r->n=p->n;r->m=p->m;}else{if((q->n)==(p->n)){r->n=p->n;r->m=(p->m)+(q->m);}while((q->n<p->n)&&q->next!=NULL){r->n=q->n;r->m=q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}}if(q->next!=NULL){r->next=(struct LNode*)malloc(L);r=r->next;q=q->next;while(q->next!=NULL){r->n=q->n;r->m=q->m;r->next=(struct LNode*)malloc(L);r=r->next;q=q->next;}r->n=q->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r->next=NULL;}else{r->next=(struct LNode*)malloc(L);r->next=NULL;}}printf("completed!\n");return(u);}整理之前的ADDPOLYN函数*/struct LNode*addpolyn(struct LNode*p,struct LNode*q,struct LNode*r){struct LNode* u=NULL;if((q==NULL)||(p==NULL)){return(u);}r=(struct LNode*)malloc(L);u=r;while(((q->next)!=NULL)&&((p->next)!=NULL)){ if(q->n>p->n){r->n=p->n;r->m=p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}if(q->n==p->n){r->n=p->n;r->m=p->m+q->m;q=q->next;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}if(q->n<p->n){r->n=q->n;r->m=q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}}if((q->next==NULL)&&(p->next==NULL)){ if(q->n>p->n){r->n=p->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r=r->next;r->n=q->n;r->m=q->m;}if(q->n==p->n){r->n=p->n;r->m=p->m+q->m;}if(q->n<p->n){r->n=q->n;r->m=q->m;r->next=(struct LNode*)malloc(L);r=r->next;r->m=p->m;r->n=p->n;}}if((q->next==NULL)&&(p->next!=NULL)){ if(q->n<p->n){r->n=q->n;r->m=q->m;r->next=(struct LNode*)malloc(L);r=r->next;while(p->next!=NULL){r->n=p->n;r->m=p->m;p=p->next;r->next=(structLNode*)malloc(L);r=r->next;}r->n=p->n;r->m=p->m;}if(q->n==p->n){r->n=q->n;r->m=q->m+p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;while(p->next!=NULL){r->n=p->n;r->m=p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=p->n;r->m=p->m;}if(q->n>p->n){while(p->n<q->n){r->n=p->n;r->m=p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}if(q->n==p->n){r->n=q->n;r->m=q->m+p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;while(p->next!=NULL){r->n=p->n;r->m=p->m; p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=p->n;r->m=p->m;}if(q->n<p->n){r->n=q->n;r->m=q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next; while(p->next!=NULL){r->n=p->n;r->m=p->m; p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=p->n;r->m=p->m;}}}if((p->next==NULL)&&((q->next)!=NULL)){ if(p->n<q->n){r->n=p->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r=r->next;while(q->next!=NULL){r->n=q->n;r->m=q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=q->n;r->m=q->m;}if((p->n)==(q->n)){r->n=p->n;r->m=p->m+q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;while((q->next)!=NULL){r->n=q->n;r->m=q->m;r->next=(struct LNode*)malloc(L);r=r->next;q=q->next;}r->n=q->n;r->m=q->m;}if(p->n>q->n){while(q->n<p->n){r->n=q->n;r->m=q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}if(p->n==q->n){r->n=p->n;r->m=p->m+q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;while(q->next!=NULL){r->n=q->n;r->m=q->m; q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=q->n;r->m=q->m;}if(q->n>p->n){r->n=p->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r=r->next; while(q->next!=NULL){r->n=q->n;r->m=q->m; q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=q->n;r->m=q->m;}}}r->next=NULL;printf("completed!\n");return(u);}struct LNode*subtract(struct LNode*p,struct LNode*q,struct LNode*r){struct LNode* u=NULL;if((q==NULL)||(p==NULL)){return(u);}r=(struct LNode*)malloc(L);u=r;while(q->next!=NULL&&p->next!=NULL){ if(q->n>p->n){r->n=p->n;r->m=p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}if(q->n==p->n){r->n=p->n;r->m=p->m-q->m;q=q->next;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}if(q->n<p->n){r->n=q->n;r->m=-q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}}if((q->next==NULL)&&(p->next==NULL)){ if(q->n>p->n){r->n=p->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r=r->next;r->n=q->n;r->m=-q->m;}if(q->n==p->n){r->n=p->n;r->m=p->m-q->m;}if(q->n<p->n){r->n=q->n;r->m=-q->m;r->next=(struct LNode*)malloc(L);r=r->next;r->m=p->m;r->n=p->n;}}if((q->next==NULL)&&(p->next!=NULL)){ if(q->n<p->n){r->n=q->n;r->m=-q->m;r->next=(struct LNode*)malloc(L);r=r->next;while(p->next!=NULL){r->n=p->n;r->m=p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=p->n;r->m=p->m;}if(q->n==p->n){r->n=q->n;r->m=-q->m+p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;while(p->next!=NULL){r->n=p->n;r->m=p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=p->n;r->m=p->m;}if(q->n>p->n){while(p->n<q->n){r->n=p->n;r->m=p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}if(q->n==p->n){r->n=q->n;r->m=-q->m+p->m;p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;while(p->next!=NULL){r->n=p->n;r->m=p->m; p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=p->n;r->m=p->m;}if(q->n<p->n){r->n=q->n;r->m=-q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next; while(p->next!=NULL){r->n=p->n;r->m=p->m; p=p->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=p->n;r->m=p->m;}}}if((p->next==NULL)&&(q->next!=NULL)){ if(p->n<q->n){r->n=p->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r=r->next;while(q->next!=NULL){r->n=q->n;r->m=-q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=q->n;r->m=-q->m;}if(p->n==q->n){r->n=p->n;r->m=p->m-q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;while(q->next!=NULL){r->n=q->n;r->m=-q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=q->n;r->m=-q->m;}if(p->n>q->n){while(q->n<p->n){r->n=q->n;r->m=-q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}if(p->n==q->n){r->n=p->n;r->m=p->m-q->m;q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;while(q->next!=NULL){r->n=q->n;r->m=-q->m; q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=q->n;r->m=-q->m;}if(q->n>p->n){r->n=p->n;r->m=p->m;r->next=(struct LNode*)malloc(L);r=r->next; while(q->next!=NULL){r->n=q->n;r->m=-q->m; q=q->next;r->next=(struct LNode*)malloc(L);r=r->next;}r->n=q->n;r->m=-q->m;}}}r->next=(struct LNode*)malloc(L); r->next=NULL;printf("completed!\n");return(u);}double value (struct LNode*p,double x){double a,b; int i;p=p->next;a=0;while(p->next!=NULL){b=1;i=0;while(i<p->n){b=b*x;i++;}b=p->m*b;a=a+b;p=p->next;}b=1;i=0;while(i<p->n){b=b*x;i++;}b=p->m*b;a=a+b;return(a);}struct LNode* destroypolyn(struct LNode*p){struct LNode* u;p=NULL;u=p;printf("completed!\n");return(u);}struct LNode* clearpolyn(struct LNode*p){struct LNode* u=p;while(p!=NULL){p->m=0;p=p->next;}printf("completed!\n");return(u);}void main(){struct LNode *a[N]={NULL};double x,z;int n,n1,n2,n31,n32,n41,n42,n43,n51,n52,n53,n6,n7,n8,n9;top: printf("********************多项式运算器********************\n");printf(" 1----------------创建多项式\n");printf(" 2----------------显示多项式\n");printf(" 3----------------复制多项式\n");printf(" 4----------------多项式求和\n");printf(" 5----------------多项式求差\n");printf(" 6----------------多项式求值\n");printf(" 7----------------销毁多项式\n");printf(" 8----------------清空多项式\n");printf(" 9----------------修改多项式\n");printf(" 0----------------退出\n");printf(" 输入您要的运算:");scanf("%d",&n);switch(n){case 1:printf("输入您要创建的多项式存储位置\n");scanf("%d",&n1);a[n1-1]=createpolyn(a[n1-1]);break;case 2:printf("输入您要显示的多项式序号\n");scanf("%d",&n2);if(a[n2-1]!=NULL){printpolyn(a[n2-1]);}else printf("NULL\n");break;case 3:printf("输入您要复制的多项式序号和复制后的多项式的存储位置\n");scanf("%d",&n31);scanf("%d",&n32);a[n32-1]=copypolyn(a[n31-1],a[n32-1]);break;case 4:printf("输入您要求和的多项式以及和式存储位置\n");scanf("%d",&n41);scanf("%d",&n42);scanf("%d",&n43);a[n43-1]=addpolyn(a[n41-1],a[n42-1],a[n43-1]);break;case 5:printf("输入您要求差的被减式、减式以及差式存储位置\n");scanf("%d",&n51);scanf("%d",&n52);scanf("%d",&n53);a[n53-1]=subtract(a[n51-1],a[n52-1],a[n53-1]);break;case 6:printf("输入您要求值的多项式以及X的值\n");scanf("%d",&n6);scanf("%lf",&x);z=value(a[n6-1],x);printf("the value is:%lf\n",z);break;case 7:printf("输入您要销毁的多项式存储位置\n");scanf("%d",&n7);a[n7-1]=destroypolyn(a[n7-1]);break;case 8:printf("输入您要清空的多项式存储位置\n");scanf("%d",&n8);a[n8-1]=clearpolyn(a[n8-1]);break;case 9:printf("输入您要修改的多项式存储位置\n");scanf("%d",&n9);a[n9-1]=destroypolyn(a[n9-1]);a[n9-1]=createpolyn(a[n9-1]);break;case 0:printf("bye bye\n");break;}if(n!=0) goto top;}。

相关主题