PtPoly PolySubtraction (PtPoly ppoly1, PtPoly ppoly2) { PtPoly Sub; unsigned int Degree, I, Invert = 0; if (!ValidPolys (ppoly1, ppoly2)) return NULL; if(ppoly1->Degree < ppoly2->Degree) { Degree = ppoly1->Degree ; Invert = 1; }else{ Degree = ppoly2-> Degree; } if ((Sub = PolyCopy ((ppoly1->Degree >= ppoly2->Degree) ? ppoly1 : ppoly2)) == NULL) { Error = NO_MEM; return NULL; } Error = OK; if (Invert) Sub = PolySymmetrical(Sub); for (I = 0; I <= Degree; I++) Sub->Poly[I] = ppoly1->Poly[I] - ppoly2->Poly[I]; ReducePoly(Sub); return Sub; }
int main (void) { PtPoly poly1 = NULL, poly2 = NULL, poly3 = NULL; char filename[21]; int st, degree, i; double coef; system ("clear"); printf ("\nLer polinomio do ficheiro - Read polynomial from a text file\n"); do { printf ("Nome do ficheiro (Filename) -> "); st = scanf ("%20[^\n]", filename); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); poly1 = PolyCreateFile (filename); printf ("\nPolinomio lido do ficheiro - Polynomial acquired from text file %s\n", filename); WritePoly (poly1); poly2 = PolySymmetrical (poly1); printf ("\nPolinomio simetrico - Symmetrical polynomial\n"); WritePoly (poly2); PolyDestroy (&poly2); printf ("\nLer polinomio do teclado - Read polynomial from keyboard\n"); do { printf ("grau do polinomio (polynomial's degree)? "); scanf ("%d", °ree); scanf ("%*[^\n]"); scanf ("%*c"); } while (degree < 0); poly2 = PolyCreate (degree); for (i = 0; i <= degree; i++) { do { printf ("Coeficiente do polinomio (polynomial's coefficient)? "); st = scanf ("%lf", &coef); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); PolyModifyCoefficient (poly2, i, coef); } printf ("\nPolinomio lido do teclado - Polynomial acquired from keyboard\n"); WritePoly (poly2); printf ("\nEscrever polinomio no ficheiro - Storing the polynomial in a text file\n"); do { printf ("Nome do ficheiro (Filename) -> "); st = scanf ("%20[^\n]", filename); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); PolyStoreFile (poly2, filename); poly3 = PolyAddition (poly1, poly2); printf ("\nPolinomio soma - Sum polynomial\n"); WritePoly (poly3); PolyDestroy (&poly3); poly3 = PolySubtraction (poly1, poly2); printf ("\nPolinomio diferenca - Difference polynomial\n"); WritePoly (poly3); PolyDestroy (&poly3); poly3 = PolyMultiplication (poly1, poly2); printf ("\nPolinomio produto - Product polynomial\n"); WritePoly (poly3); printf ("\nDestruir os polinomios - Releasing the polynomials\n"); PolyDestroy (&poly1); PolyDestroy (&poly2); PolyDestroy (&poly3); WritePoly (poly3); return 0; }