以下是我的 实验报告,那位仁兄可以帮我指点指点啊? 在下感激不尽!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 一、实验名称:一元多项式的表示及相加运算 二、实验目的: 1、创建一元多项式的函数定义。 2、两个一元多项式相加函数的定义。 3、输出一元多项式函数。 三、实验源代码: #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <conio.h>
typedef struct node /*创建单链表*/ { float coef;/*系数*/ int expn; /*指数*/ struct node * next; } PolyNode;
PolyNode * Create_Poly(char ch) /*输入多项式*/ { PolyNode * p, *s,*r; float x; int y; p=(PolyNode *)malloc(sizeof(PolyNode)); p->next=NULL; printf("Please input polynomal %c(end of 0 0):\n",ch);/*系数不为0,指数从小到大递增输入*/ scanf("%f %d",&x,&y); while(x!=0) { s=(PolyNode *)malloc(sizeof(PolyNode)); s->coef=x; /*x作为系数*/ s->expn=y; /*y作为指数*/ s->next=NULL; if(p->next==NULL) { p->next=s; r=s; } else { r->next=s; r=s; } scanf("%f %d",&x,&y); } return p; }
PolyNode * Add_Poly(PolyNode * f,PolyNode * g) /*多项式相加*/ { PolyNode * fg; PolyNode *t,*q,*s,*r; float m; t=f->next; q=g->next; fg=r=(PolyNode*)malloc(sizeof(PolyNode)); fg->next=NULL; while(t&&q) { if(t->expn==q->expn) /*指数相等时系数相加*/ { m=(t->coef)+(q->coef); if(m!=0) /*系数不为0时加到结果中去*/ { s=(PolyNode *)malloc(sizeof(PolyNode)); s->coef=m; s->expn=t->expn; s->next=NULL; } t=t->next; q=q->next; } else /*指数小的加到结果中去再后移*/ if(t->expn<q->expn) { s=(PolyNode *)malloc(sizeof(PolyNode)); s->coef=t->coef; s->expn=t->expn; s->next=NULL; t=t->next; } else { s=(PolyNode *)malloc(sizeof(PolyNode)); s->coef=q->coef; s->expn=q->expn; s->next=NULL; q=q->next; }
if(fg->next==NULL) { fg->next=s; r=s; } else { r->next=s; r=s; } }/*while*/ r->next=t?t:q; /*把没加完的接上*/ return fg; }
void Out_Poly(PolyNode * f) /*输出多项式*/ { PolyNode *t; t=f->next; if(!f->next){ printf("0\n"); return; } while(t) { if(t->coef>0&&f->next!=t) printf("+"); if(t->expn==0) printf("%f",t->coef); else printf("%f*X^%d",t->coef,t->expn); t=t->next; } printf("\n"); }
void main() { PolyNode *s1,*s2,*P; char A,B; s1=Create_Poly('A'); s2=Create_Poly('B'); P=Add_Poly(s1,s2); printf("Now,after A and B,the new polynomal is:\n "); Out_Poly(P); }
四、实验结果: 1、Please input polynomal A(end of 0 0): 2.5 1 1.5 2 6.3 3 9.4 4 0 0 PolyNode A is: 2.500000*x^1+1.500000*x^2+6.300000*x^3+9.400000*x^4 Please input polynomal B(end of 0 0): 0.8 1 3.6 2 4.5 3 7.5 4 0 0 PolyNode B is: 0.800000*x^1+3.600000*x^2+4.500000*x^3+7.5*x^4 Now,after A and B,the new polynomal is: 3.300000*x^1+5.100000*x^2+10.000000*x^3+16.900000*x^4 Press any key to continue
2、Please input polynomal A(end of 0 0): 3 2 4 5 8 6 7 9 0 0 PolyNode A is: 3.000000*x^2+4.000000*x^5+8.000000*x^6+7.000000*x^9 Please input polynomal B(end of 0 0): 2 1 5 5 6 7 8 8 6 9 0 0 PolyNode B is: 2.000000*x^1+5.000000*x^5+6.000000*x^7+8.000000*x^8+6.000000*x^9 Now,after A and B,the new polynomal is: 2.000000*x^1+3.000000*x^2+9.000000*x^5+8.000000*x^6+6.000000*x^7+8.000000*x^8+13.000000*x^9 Press any key to continue
3、Please input polynomal A(end of 0 0): 2 1 3 2 5 5 0 0 PolyNode A is: 2.000000*x^1+3.000000*x^2+5.000000*x^5 Please input polynomal B(end of 0 0): 0 0 PolyNode B is: 0 Now,after A and B,the new polynomal is: 2.000000*x^1+3.000000*x^2+5.000000*x^5 Press any key to continue