设计函数分别求两个一元多项式的乘积与和。
输入分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
#include <iostream> using namespace std; typedef struct Node { int coef; int exp; struct Node *next; } * List; List Read(); void Print(List L); List GetMutiply(List L1, List L2); List GetSum(List L1, List L2); int Count(List L); List Sort(List L); List Sort(List L) { List head = L; List temp; int cou = Count(L); int coef; int exp; int tcoef; while (cou--) { temp = head->next; while (temp->next) { if (temp->exp < temp->next->exp) { coef = temp->coef; exp = temp->exp; temp->exp = temp->next->exp; temp->coef = temp->next->coef; temp->next->coef = coef; temp->next->exp = exp; temp = temp->next; } else if (temp->exp == temp->next->exp) { tcoef = temp->coef + temp->next->coef; temp->coef = tcoef; temp->next = temp->next->next; temp = temp->next; } else { temp = temp->next; } } } return head; } List Read() { List L = (struct Node *)malloc(sizeof(Node)); List head; List add = NULL; L->next = NULL; head = L; int n, coef, exp; cin >> n; if (n != 0) { while (n--) { cin >> coef >> exp; if (coef != 0) { add = (struct Node *)malloc(sizeof(Node)); add->coef = coef; add->exp = exp; L->next = add; L = add; } } L->next = NULL; } return head; } void Print(List L) { int count = Count(L); if (L->next) { L = L->next; while (L->next) { if (L->coef != 0) { cout << L->coef << " " << L->exp << " "; L = L->next; } else { L = L->next; } } cout << L->coef << " " << L->exp << endl; } else if (count == 0) { cout << 0 << " " << 0 << endl; } else { cout << 0 << " " << 0; } } List GetSum(List L1, List L2) { List head = (Node *)malloc(sizeof(Node)), temp1, temp2, newlist, add = NULL; head->next = NULL; newlist = head; temp1 = L1->next; temp2 = L2->next; int tcoef; while (temp1 && temp2) { if (temp1->exp > temp2->exp) { add = (struct Node *)malloc(sizeof(Node)); add->coef = temp1->coef; add->exp = temp1->exp; newlist->next = add; newlist = add; temp1 = temp1->next; } else if (temp1->exp < temp2->exp) { add = (struct Node *)malloc(sizeof(Node)); add->coef = temp2->coef; add->exp = temp2->exp; newlist->next = add; newlist = add; temp2 = temp2->next; } else { if (temp1->coef + temp2->coef != 0) { tcoef = temp1->coef + temp2->coef; add = (struct Node *)malloc(sizeof(Node)); add->coef = tcoef; add->exp = temp1->exp; newlist->next = add; newlist = add; } temp1 = temp1->next; temp2 = temp2->next; } } if (temp1) newlist->next = temp1; else newlist->next = temp2; return head; } List GetMutiply(List L1, List L2) { List head = (Node *)malloc(sizeof(Node)), temp1 = L1->next, temp2 = L2->next, newlist, add = NULL; head->next = NULL; newlist = head; int tcoef, texp; int count1, count2; if (temp1 && temp2) { count1 = Count(L1); count2 = Count(L2); for (int i = count1; i > 0; i--) { for (int j = count2; j > 0; j--) { tcoef = temp1->coef * temp2->coef; texp = temp1->exp + temp2->exp; add = (struct Node *)malloc(sizeof(Node)); add->coef = tcoef; add->exp = texp; newlist->next = add; newlist = add; temp2 = temp2->next; } temp1 = temp1->next; temp2 = L2->next; } } head = Sort(head); return head; } int Count(List L) { int count = 0; List t = L->next; while (t) { count++; t = t->next; } return count; } int main() { List L1 = Read(); List L2 = Read(); List L = GetMutiply(L1, L2); Print(L); L = GetSum(L1, L2); Print(L); }
结果:
原文:https://www.cnblogs.com/Transirizo/p/15086117.html