The array implements addition and subtraction of polynomials, And the array polynomial multiplication.
"Fatal. h" // header file
1 #include<stdio.h>2 #include<stdlib.h>3 #define Error(Str) FatalError(Str)4 #define FatalError(Str) fprintf(stderr,"%s\n",Str),exit(1);
1/* This code doesn't really do much */2/* Thus I havene' t bothered testing it */3 4 # include "fatal. h "// header file 5 6 # define MaxDegree 100 // defines the maximum exponent of A polynomial as 100 7 8 static int 9 Max (int A, int B) 10 {11 return A> B? A: B; // return the maximum value 12} 13 in AB // define A struct array 15 typedef struct 16 {17 int CoeffArray [MaxDegree + 1]; // you need to store one more constant item 18 int HighPower; // The index 19} * Polynomial; 20/* END */21 22/* START: fig3_19.txt */23 void 24 ZeroPolynomial (Polynomial Poly) // initialize Polynomial 25 {26 int I; 27 28 for (I = 0; I <= MaxDegree; I ++) 29 Poly-> CoeffArray [I] = 0; 30 Poly-> HighPower = 0; 31} 32/* END */33 34/* START: fig3_20.txt */35 void // Add two polynomials 36 AddPolynomial (const Polynomial Poly1, const Polynomial Poly2, 37 Polynomial PolySum) 38 {39 int I; 40 41 ZeroPolynomial (PolySum ); 42 PolySum-> HighPower = Max (Poly1-> HighPower, 43 Poly2-> HighPower); 44 45 for (I = PolySum-> HighPower; I> = 0; I --) 46 PolySum-> CoeffArray [I] = Poly1-> CoeffArray [I] 47 + Poly2-> CoeffArray [I]; 48} 49/* END */50 51/* START: fig3_21.txt */52 void // two polynomials multiply by 53 degrees (const Polynomial Poly1, 54 const Polynomial Poly2, Polynomial PolyProd) 55 {56 int I, j; 57 58 ZeroPolynomial (PolyProd ); 59 PolyProd-> HighPower = Poly1-> HighPower + Poly2-> HighPower; 60 61 if (PolyProd-> HighPower> MaxDegree) 62 Error ("Exceeded array size "); 63 else 64 for (I = 0; I <= Poly1-> HighPower; I ++) 65 for (j = 0; j <= Poly2-> HighPower; j ++) 66 PolyProd-> CoeffArray [I + j] + = 67 Poly1-> CoeffArray [I] * 68 Poly2-> CoeffArray [j]; 69} 70/* END */