02-线性结构2 一元多项式的乘法与加法运算 (20分)
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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 <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct PolynomialNode *Poly; struct PolynomialNode{ int Exponent; int Coeff; Poly Next; }; void Attach(int coef, int exp, Poly *rear){ Poly poly = (Poly)malloc(sizeof(struct PolynomialNode)); poly->Exponent = exp; poly->Coeff = coef; poly->Next = NULL; (*rear)->Next = poly; (*rear) = poly; } Poly ReadPoly(){ Poly pHead = (Poly)malloc(sizeof(struct PolynomialNode)); Poly pPre = pHead; int exp; int coef; int N; scanf("%d", &N); for(int i = 0; i < N; ++i){ scanf("%d %d", &coef, &exp); Attach(coef, exp, &pPre); } Poly pTemp = pHead; pHead = pHead->Next; free(pTemp); return pHead; } Poly Add(Poly poly1, Poly poly2){ Poly pHead = (Poly)malloc(sizeof(struct PolynomialNode)); pHead->Next = NULL; Poly pPre = pHead; Poly p1 = poly1; Poly p2 = poly2; int coef = 0; while(p1 && p2){ if(p1->Exponent == p2->Exponent){ coef = p1->Coeff + p2->Coeff; if(coef != 0){ Attach(coef, p1->Exponent, &pPre); } p1 = p1->Next; p2 = p2->Next; } else if(p1->Exponent > p2->Exponent){ Attach(p1->Coeff, p1->Exponent, &pPre); p1 = p1->Next; } else{ Attach(p2->Coeff, p2->Exponent, &pPre); p2 = p2->Next; } } while(p1){ Attach(p1->Coeff, p1->Exponent, &pPre); p1 = p1->Next; } while(p2){ Attach(p2->Coeff, p2->Exponent, &pPre); p2 = p2->Next; } Poly pTemp = pHead; pHead = pHead->Next; free(pTemp); return pHead; } Poly Multiple(Poly poly1, Poly poly2){ if(!poly1||!poly2){ return NULL; } Poly pHead = (Poly)malloc(sizeof(struct PolynomialNode)); pHead->Next = NULL; Poly pPre = pHead; Poly p1 = poly1; Poly p2 = poly2; int e,c; while(p2){ Attach(p1->Coeff*p2->Coeff, p1->Exponent+p2->Exponent, &pPre); p2 = p2->Next; } p1 = p1->Next; while(p1){ p2 = poly2; pPre = pHead; while(p2){ e = p1->Exponent + p2->Exponent; c = p1->Coeff*p2->Coeff; while(pPre->Next&&pPre->Next->Exponent > e) pPre = pPre->Next; if(pPre->Next && pPre->Next->Exponent == e){ if(pPre->Next->Coeff + c){ pPre->Next->Coeff +=c; } else{ Poly temp = pPre->Next; pPre->Next = temp->Next; free(temp); } } else{ Poly temp = (Poly)malloc(sizeof(struct PolynomialNode)); temp->Coeff = c; temp->Exponent = e; temp->Next = pPre->Next; pPre->Next = temp; pPre = pPre->Next; } p2 = p2->Next; } p1 = p1->Next; } p2 = pHead; pHead = pHead->Next; free(p2); return pHead; } void PrintPoly(Poly p){ if(!p){ printf("0 0\n"); return; } printf("%d %d", p->Coeff, p->Exponent); p = p->Next; while(p){ printf(" %d %d", p->Coeff, p->Exponent); p = p->Next; } printf("\n"); } int main(){ //读取第一个多项式; Poly poly1 = ReadPoly(); //读取第二个多项式; Poly poly2 = ReadPoly(); Poly polyProduct = Multiple(poly1, poly2); PrintPoly(polyProduct); Poly polySum = Add(poly1, poly2); PrintPoly(polySum); return 0; }
提交结果: