设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
1 2 |
4 3 4 -5 2 6 1 -2 0 3 5 20 -7 4 3 1 |
一元多项式的乘法与加法运算代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
#include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct Node *PtrToNode; struct Node { ElementType Term; ElementType Expon; PtrToNode Next; Node() { Term = 0; Expon = 0; Next = NULL; } }; typedef PtrToNode List; List Read(); void Print(List L); List Add(List L1, List L2); List Multi(List L1, List L2); List MultiByNode(List L1, struct Node L2); int main() { List L1, L2, LAdd, LMulti; L1 = Read(); L2 = Read(); LMulti = Multi(L1, L2); LAdd = Add(L1, L2); Print(LMulti); Print(LAdd); return 0; } List Read(){ List L, head; L = new Node(); L->Next = NULL; head = L; int n; scanf("%d", &n); if (n > 0){ for (int i = 0; i < n; i++){ List temp = new Node();; scanf("%d%d", &(temp->Term), &(temp->Expon)); L->Next = temp; L = temp; } } return head; } void Print(List L){ List p = L->Next; if (!p){ printf("0 0\n"); } else{ while (p){ if (p->Next != NULL){ printf("%d %d ", p->Term, p->Expon); } else { printf("%d %d\n", p->Term, p->Expon); } p = p->Next; } } } List Add(List L1, List L2){ List L, head, t1, t2; L = new Node();; head = L; t1 = L1->Next; t2 = L2->Next; while (t1 != NULL&&t2 != NULL){ List temp = new Node();; if (t1->Expon > t2->Expon){ temp->Term = t1->Term; temp->Expon = t1->Expon; L->Next = temp; L = temp; t1 = t1->Next; } else if (t1->Expon < t2->Expon) { temp->Term = t2->Term; temp->Expon = t2->Expon; L->Next = temp; L = temp; t2 = t2->Next; } else if (t1->Expon == t2->Expon){ //抵消 if ((t1->Term + t2->Term) == 0){ t1 = t1->Next; t2 = t2->Next; } else{ temp->Term = t1->Term + t2->Term; temp->Expon = t1->Expon; L->Next = temp; L = temp; t1 = t1->Next; t2 = t2->Next; } } } //剩下的项直接挂上去 if (t1 == NULL){ L->Next = t2; } else if (t2 == NULL){ L->Next = t1; } return head; } List Multi(List L1, List L2){ List L, head, t1, t2, temp; L = new Node();; head = L; t1 = L1->Next; t2 = L2->Next; if (t1 != NULL&& t2 != NULL){ temp = MultiByNode(L1, *t2); t2 = t2->Next; while (t2){ List temp2 = MultiByNode(L1, *t2); temp = Add(temp, temp2); t2 = t2->Next; } L->Next = temp->Next; } return head; } List MultiByNode(List L1, struct Node L2){ List L, head, t1; L = new Node();; head = L; t1 = L1->Next; while (t1){ List temp = new Node();; temp->Term = t1->Term*L2.Term; temp->Expon = t1->Expon + L2.Expon; L->Next = temp; L = temp; t1 = t1->Next; } return head; } |
测试结果: