設計函數分別求兩個一元多項式的乘積與和。
輸入格式:
輸入分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
解:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct PolyNode* Polynomial; 5 6 struct PolyNode{ 7 int coef; 8 int expon; 9 Polynomial next; 10 }; 11 12 Polynomial ReadPoly(); 13 Polynomial Mult(Polynomial P1,Polynomial P2); 14 void PrintPoly(Polynomial PP); 15 Polynomial Add(Polynomial P1,Polynomial P2); 16 17 int main() //程序框架搭建 18 { 19 Polynomial P1, P2, PP, PS; 20 P1 = ReadPoly(); 21 P2 = ReadPoly(); 22 23 PP = Mult(P1, P2); 24 PrintPoly(PP); 25 26 PS = Add(P1, P2); 27 PrintPoly(PS); 28 29 return 0; 30 } 31 32 //對Rear指針的處理:1.初值為NULL,根據是否為空做不同處理;2.指向一個空節點 33 //*pRear當前結果表達式尾項指針的指針 34 //函數實現在pRear后面插入節點 35 void Attach(int c, int e, Polynomial *pRear) 36 { 37 Polynomial P; 38 P = (Polynomial)malloc(sizeof(struct PolyNode)); 39 P->coef = c; 40 P->expon = e; 41 P->next = NULL; 42 (*pRear)->next = P; 43 *pRear = P; 44 } 45 46 Polynomial ReadPoly() 47 { 48 Polynomial P, Rear, t; 49 int c, e, N; 50 scanf("%d", &N); 51 P = (Polynomial)malloc(sizeof(struct PolyNode)); //鏈表頭空節點 52 P->next = NULL; 53 Rear = P; 54 while (N--) 55 { 56 scanf("%d %d", &c, &e); 57 if (c != 0) 58 Attach(c, e, &Rear); //將當前項插入多項式尾部 59 } 60 t = P; 61 P = P->next; 62 free(t); //刪除臨時生成的頭結點 63 return P; 64 } 65 66 Polynomial Add(Polynomial P1, Polynomial P2) 67 { 68 Polynomial t1, t2; 69 t1 = P1; 70 t2 = P2; 71 //生成新的頭結點 72 Polynomial P,t; 73 P = (Polynomial)malloc(sizeof(struct PolyNode)); 74 P->next = NULL; 75 Polynomial Rear; 76 Rear = P; 77 while (t1&&t2) 78 { 79 if (t1->expon==t2->expon) 80 { 81 if (t1->coef+t2->coef) //系數和為0時不添加到尾節點上 /*考慮周全*/ 82 { 83 Attach(t1->coef + t2->coef, t1->expon, &Rear); 84 } 85 t1 = t1->next; 86 t2 = t2->next; 87 } 88 else if (t1->expon>t2->expon) 89 { 90 Attach(t1->coef, t1->expon, &Rear); 91 t1=t1->next; 92 }else 93 { 94 Attach(t2->coef, t2->expon, &Rear); 95 t2 = t2->next; 96 } 97 } 98 while (t1) 99 { 100 Attach(t1->coef, t1->expon, &Rear); 101 t1 = t1->next; 102 } 103 while (t2) 104 { 105 Attach(t2->coef, t2->expon, &Rear); 106 t2 = t2->next; 107 } 108 t = P; 109 P = P->next; 110 free(t); 111 return P; 112 } 113 114 //多項式乘法方法: 115 //1. 將乘法運算轉換為加法運算,讓P1的每一項和P2相乘,在加到結果多項式中 116 //2. 逐項插入,將P1當前項乘P2當前項,在插入到結果表達式中,關鍵是要找到插入的位置 117 118 Polynomial Mult(Polynomial P1, Polynomial P2) 119 { 120 Polynomial P, Rear; 121 Polynomial t1, t2, t; 122 if (!P1||!P2) 123 { 124 return NULL; 125 } 126 t1 = P1; 127 t2 = P2; 128 P = (Polynomial)malloc(sizeof(struct PolyNode)); 129 Rear = P; 130 while (t2) 131 { 132 //先用P1的第一項乘以P2,得到初始結果多項式 133 Attach(t1->coef*t2->coef, t1->expon + t2->expon, &Rear); 134 t2 = t2->next; 135 } 136 t1 = t1->next; 137 while (t1) 138 { 139 t2 = P2; 140 Rear = P; //將尾節點置到頭結點來 141 while (t2) 142 { 143 int e = t1->expon + t2->expon; 144 int c = t1->coef * t2->coef; //以后的每一項相乘的結果 145 while (Rear->next&&Rear->next->expon>e) //找插入位置 146 { 147 Rear = Rear->next; 148 } 149 if (Rear->next&&Rear->next->expon==e) 150 { 151 if (Rear->next->coef+c) //判系數是否為0 152 { 153 Rear->next->coef += c; 154 } 155 else //為0刪除節點 156 { 157 t = Rear->next; 158 Rear->next = t->next; 159 free(t); 160 } 161 } 162 else //插入位置 163 { 164 t = (Polynomial)malloc(sizeof(struct PolyNode)); 165 t->coef = c; 166 t->expon = e; 167 t->next = Rear->next; 168 Rear->next = t; 169 170 Rear = Rear->next; 171 } 172 t2 = t2->next; 173 } 174 t1 = t1->next; 175 } 176 t2 = P; 177 P = P->next; 178 free(t2); 179 180 return P; 181 } 182 183 void PrintPoly(Polynomial P) 184 { 185 int flag = 0;//輔助調整輸出格式 186 if (!P) 187 { 188 printf("0 0\n"); /*格式*/ 189 return; 190 } 191 while (P) 192 { 193 if (!flag) //第一次 194 { 195 flag = 1; 196 } 197 else 198 { 199 printf(" "); 200 } 201 printf("%d %d", P->coef, P->expon); 202 P = P->next; 203 } 204 printf("\n"); 205 }