赞
踩
设计函数分别求两个一元多项式的乘积与和。
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0
。
- 4 3 4 -5 2 6 1 -2 0
- 3 5 20 -7 4 3 1
- 15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
- 5 20 -4 4 -5 2 9 1 -2 0
个人分析:emmmmmm一元多项式加法好像挺容易的,也就是判断俩个多项式中的项,指数小者排在前面,指数相同者进行合并。还好何老师给我准备了小白专场。这道题的方方面面在小白专场里已经讲的相当清楚了。提一下我觉得特殊的点:
专场选择用链表记录多项式,在读入多项式系数时,为了保证代码的一致性(不用为了第一个数据结点写特殊代码),采用了头结点为空结点的处理。
对于菜鸡来说,这次的题目很过瘾(码量很足),虽然是亦步亦趋,跟着小白专场,写了3个小时才写好的程序。但是很激动,自己也能写近200行的代码了。嗯!要越来越厉害!加油加油!!
代码如下:
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct itemNode *List;
- struct itemNode{
- int xishu; //原谅我很low的用了拼音orz
- int zhishu;
- List next;
- };
-
- List Attach(int c,int e,List *pRear)
- {
- List L;
- L=(List)malloc(sizeof(struct itemNode));
- L->next=NULL;
- L->xishu=c;
- L->zhishu=e;
- (*pRear)->next=L;
- *pRear=L;
- }
-
- List Creat()
- {
- int number,c,e;
- List Rear,T,t;
- T=(List)malloc(sizeof(struct itemNode));
- Rear=T;
- scanf("%d",&number);
- for(int i=0;i<number;i++)
- {
- scanf("%d %d",&c,&e);
- Attach(c,e,&Rear);
- }
- t=T;T=T->next;free(t);
- return T;
- }
- List Add(List T1,List T2)
- {
- List t1,t2,P,Rear,t;
- int sum;
- t1=T1;t2=T2;
- P=(List)malloc(sizeof(struct itemNode));
- Rear=P;
- while(t1&&t2)
- {
- if(t1->zhishu==t2->zhishu)
- {
- sum=t1->xishu+t2->xishu;
- if(sum==0)
- {
- t1=t1->next;
- t2=t2->next;
- }
- else
- {
- Attach(sum,t1->zhishu,&Rear);
- t1=t1->next;
- t2=t2->next;
- }
- }
- else if(t1->zhishu>t2->zhishu)
- {
- Attach(t1->xishu,t1->zhishu,&Rear);
- t1=t1->next;
- }
- else
- {
- Attach(t2->xishu,t2->zhishu,&Rear);
- t2=t2->next;
- }
- }
- while(t1)
- {
- Attach(t1->xishu,t1->zhishu,&Rear);
- t1=t1->next;
- }
- while(t2)
- {
- Attach(t2->xishu,t2->zhishu,&Rear);
- t2=t2->next;
- }
- Rear->next=NULL;
- t=P;
- P=P->next;
- free(t);
- return P;
- }
-
- List Mult(List T1,List T2)
- {
- //采取先用一多项式第一项乘以二多项式,得到初始结果多项式
- List P,t1,t2,Rear,t;
- int c,e;
- t1=T1;t2=T2;
- P=(List)malloc(sizeof(struct itemNode));
- Rear=P;
- if(!t1||!t2)
- return NULL;
- while(t2)
- {
- Attach(t1->xishu*t2->xishu,t1->zhishu+t2->zhishu,&Rear);
- t2=t2->next;
- }
- t1=t1->next;
-
- //再将一多项式各项乘以二多项式,得到结果结点,插入结果多项式
- while(t1)
- {
- t2=T2;
- Rear=P;
- while(t2)
- { e=t1->zhishu+t2->zhishu;
- c=t1->xishu*t2->xishu;
- while(Rear->next&&e<Rear->next->zhishu)
- {
- Rear=Rear->next;
- }
- if(Rear->next&&(Rear->next->zhishu==e))
- {
- if(c+Rear->next->xishu==0)
- {
- t=Rear->next;
- Rear->next=t->next;
- free(t);
- }
- else
- {
- Rear->next->xishu+=c;
- }
- }
- else
- {
- t=(List)malloc(sizeof(struct itemNode));
- t->xishu=c;
- t->zhishu=e;
- t->next=Rear->next;
- Rear->next=t;
- Rear=Rear->next;
- }
-
- t2=t2->next;
- }
- t1=t1->next;
- }
- t1=P;P=P->next;free(t1);
- return P;
- }
-
- void PrintPoly(List T)
- {
- int flag=0;
- if(!T){
- printf("0 0\n");
- return;
- }
- while(T){
- if(!flag)
- flag=1;
- else
- printf(" ");
- printf("%d %d",T->xishu,T->zhishu);
- T=T->next;
- }
- printf("\n");
- }
- int main()
- {
- List T1,T2,TT,TS;
- T1=Creat();
- T2=Creat();
- TT=Mult(T1,T2);
-
- PrintPoly(TT);
- TS=Add(T1,T2);
- PrintPoly(TS);
- return 0;
-
- }

测试结果:
总结:欸,最近毕设的事还在折磨着菜鸡,因为身在曹营心在汉,每每动笔的时候都不太写的下去。哪有写代码的时候那种探索->失败->失败->成功的喜悦。不过,这不是不好好写毕设论文的借口,搞定这些事就能一心向码!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。