1 //简单的一维数组,适合稠密多项式 2 #include <stdio.h> 3 #include <stdlib.h> 4 #define MaxDegree 1000 5 typedef struct { 6 int CoeffArray[MaxDegree+1]; 7 int HighPower; 8 } *Polynomial; 9 10 void ZeroPolynomial(Polynomial poly) 11 { 12 for (int i = 0; i <= MaxDegree; ++i) { 13 poly->CoeffArray[i] = 0; 14 } 15 poly->HighPower = 0; 16 } 17 18 int Max(int a, int b) 19 { 20 return a > b ? a : b; 21 } 22 //多项式相加 23 void AddPolynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PolySum) 24 { 25 ZeroPolynomial(PolySum); 26 PolySum->HighPower = Max(Poly1->HighPower, Poly2->HighPower); 27 28 for (int i = PolySum->HighPower; i >= 0; --i) { 29 PolySum->CoeffArray[i] = Poly1->CoeffArray[i] + Poly2->CoeffArray[i]; 30 } 31 } 32 //多项式相乘 33 void MultPolynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PolyProd) 34 { 35 ZeroPolynomial(PolyProd); 36 PolyProd->HighPower = Poly1->HighPower + Poly2->HighPower; 37 38 if (PolyProd->HighPower > MaxDegree) 39 exit(1); 40 else 41 for (int i = 0; i <= Poly1->HighPower; ++i) 42 for (int j = 0; j <= Poly2->HighPower; ++j) 43 PolyProd->CoeffArray[i+j] += Poly1->CoeffArray[i] * Poly2->CoeffArray[j]; 44 }