02-线性结构1 一元多项式的乘法与加法运算

来源:互联网 发布:wps office for linux 编辑:程序博客网 时间:2024/06/06 05:12
02-线性结构1 一元多项式的乘法与加法运算   (20分)

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 03 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 15 20 -4 4 -5 2 9 1 -2 0
突然发现自己链表不会写了
#include <stdio.h>#include <stdlib.h> typedef struct node{int coefficient;int exponent;struct node * next;} PolyNode, *Polynomial;Polynomial ReadPoly();void Attach(int c, int e, Polynomial * Rear);Polynomial MultPoly(Polynomial P1, Polynomial P2);Polynomial AddPoly(Polynomial P1, Polynomial P2);void PrintPoly(Polynomial P);int main(int argc, char const *argv[]){Polynomial Poly1, Poly2, PolySum, PolyMul;Poly1 = ReadPoly();Poly2 = ReadPoly();PolyMul = MultPoly(Poly1, Poly2);PrintPoly(PolyMul);PolySum = AddPoly(Poly1, Poly2);PrintPoly(PolySum);return 0;}Polynomial ReadPoly(){Polynomial P, Rear, temp;P = (PolyNode*)malloc(sizeof(PolyNode));P->next = NULL;Rear = P;int N, c, e;scanf("%d", &N);while(N--){scanf("%d %d", &c, &e);Attach(c, e, &Rear);}temp = P;P = P->next; free(temp);return P;}void Attach(int c, int e, Polynomial * pRear){Polynomial P;P = (PolyNode*)malloc(sizeof(PolyNode));P->coefficient = c;P->exponent = e;P->next = NULL;(*pRear)->next = P;*pRear = P;}Polynomial MultPoly(Polynomial P1, Polynomial P2){Polynomial P, temp1, temp2, Rear, temp;int c, e;if(!P1 || !P2)return NULL;temp1 = P1;temp2 = P2;P = (PolyNode*)malloc(sizeof(PolyNode));P->next = NULL;Rear = P;while(temp2){c = temp1->coefficient * temp2->coefficient;e = temp1->exponent + temp2->exponent;if(c != 0){Attach(c, e, &Rear);temp2 = temp2->next;}}temp1 = temp1->next;while(temp1){temp2 = P2, Rear = P;while(temp2){c = temp1->coefficient * temp2->coefficient;e = temp1->exponent + temp2->exponent;if(c != 0){while(Rear->next && Rear->next->exponent > e)Rear = Rear->next;if(Rear->next && Rear->next->exponent == e){if(Rear->next->coefficient + c)Rear->next->coefficient += c;else{temp = Rear->next;Rear->next = temp->next;free(temp);}}else{temp = (PolyNode*)malloc(sizeof(PolyNode));temp->coefficient = c;temp->exponent = e;temp->next = Rear->next;Rear->next = temp;Rear = Rear->next;}temp2 = temp2->next;}}temp1 = temp1->next;}temp = P;P = P->next;free(temp);return P;}Polynomial AddPoly(Polynomial P1, Polynomial P2){Polynomial P, temp1, temp2, Rear, temp;if(!P1 && !P2){if(!P1)return P2;elsereturn P1;}P = (PolyNode*)malloc(sizeof(PolyNode));P->next = NULL;Rear = P;temp1 = P1;temp2 = P2;while(temp1 && temp2){if(temp1->exponent > temp2->exponent){if(temp1->coefficient){Attach(temp1->coefficient, temp1->exponent, &Rear);}temp1 = temp1->next;}else if(temp1->exponent == temp2->exponent){if(temp1->coefficient + temp2->coefficient){Attach(temp1->coefficient + temp2->coefficient, temp1->exponent, &Rear);}temp1 = temp1->next;temp2 = temp2->next;}else{if(temp2->coefficient){Attach(temp2->coefficient, temp2->exponent, &Rear);}temp2 = temp2->next;}}while(temp1){Attach(temp1->coefficient, temp1->exponent, &Rear);temp1 = temp1->next;}while(temp2){Attach(temp2->coefficient, temp2->exponent, &Rear);temp2 = temp2->next;}temp = P;P = P->next;free(temp);return P;}void PrintPoly(Polynomial P){int flag = 0;if(!P){printf("0 0");}while(P){if (!flag)flag = 1;elseprintf(" ");printf("%d %d", P->coefficient, P->exponent);P = P->next;}printf("\n");}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三洋电视应用商店打不开怎么办 中兴手机应用商店打不开怎么办 绝地求生与服务器失去连接怎么办 战地4载入不进去怎么办 电脑软件无法找到入口怎么办 ipad的按键坏了怎么办 更新ios12软件闪退怎么办 设置虚拟服务器输入不了数字怎么办 玩客云自动备份设备连接失败怎么办 3dmax文件打开失败怎么办 夏天吹空调嘴歪怎么办 燃气热水器温度调节拧不动怎么办 能率热水器震动声大怎么办 车底盘磕了漏油怎么办 法士特变速箱的随动阀漏气了怎么办 大灯随动afs失灵怎么办 2017款迈腾大灯随动故障怎么办 微信gps信号不好怎么办 苹果6s定位不准怎么办 电脑不读取u盘怎么办 注塑机上的料烤坨了怎么办 智能锁电机坏了怎么办 注塑机加热嘴内扣突了怎么办 tpu粘在螺杆上怎么办 注塑机锁模时会有射退动作怎么办 电动车刹车油泵不打油怎么办 cad转pdf颜色浅怎么办 松下多功能传真一体机卡纸怎么办 无刷电机坏了怎么办 6kv高压电机绝缘不合格怎么办? 400t油压机下降太慢怎么办 无法连线到服务器1~1怎么办? 数控车床车角度不亮怎么办 超市存包柜的票不见了怎么办 交货期来不及导致船期延误怎么办 跑1000米中途累怎么办 手指被机器压烂怎么办 机械手不能回归原点该怎么办 前缘送纸纸板翘怎么办 三菱AL 1R.2报警怎么办 工作好但领导不好伺候怎么办