PtPoly PolyMultiplication (PtPoly ppoly1, PtPoly ppoly2) { if(!ValidPolys(ppoly1,ppoly2)) return NULL; if(ppoly1==NULL || ppoly2==NULL){ Error = NO_POLY; return NULL; } PtPoly mul; int degree = ppoly1->Degree + ppoly2->Degree; if((mul = PolyCreate(degree)) == NULL){ return NULL; } for (int i = 0; i <= ppoly1->Degree ; ++i) { for (int j = 0; j <= ppoly2->Degree ; ++j) { mul->Poly[i + j] += ppoly1->Poly[i] * ppoly2->Poly[j]; } } Error = OK; ReducePoly(mul); return mul; }
PtPoly PolyCopy (PtPoly ppoly) { PtPoly Copy; unsigned int I; if (ppoly == NULL) { Error = NO_POLY; return NULL; } if ((Copy = PolyCreate (ppoly->Degree)) == NULL) { Error = NO_MEM; return NULL; }; Error = OK; for (I = 0; I <= ppoly->Degree; I++) Copy->Poly[I] = ppoly->Poly[I]; return Copy; }
PtPoly PolySymmetrical (PtPoly ppoly) { PtPoly Symetric; unsigned int I; if (ppoly == NULL) { Error = NO_POLY; return 0; } if ((Symetric = PolyCreate (ppoly->Degree)) == NULL) { Error = NO_MEM; return NULL; } Error = OK; for (I = 0; I <= ppoly->Degree ; I++) Symetric->Poly[I] = ppoly->Poly[I] * (-1); return Symetric; }
PtPoly PolyAddition (PtPoly ppoly1, PtPoly ppoly2) { if(!ValidPolys(ppoly1,ppoly2)) return NULL; if(ppoly1==NULL || ppoly2==NULL){ Error = NO_POLY; } int smallestDegree; int biggestDegree; int biggest = 0; /* considera-se o ppoly1 */ if(ppoly1->Degree > ppoly2->Degree){ biggestDegree = ppoly1->Degree; smallestDegree = ppoly2->Degree; }else{ biggestDegree = ppoly2->Degree; smallestDegree = ppoly1->Degree; biggest = 1; } PtPoly sum; if((sum = PolyCreate(biggestDegree)) == NULL){ return NULL; } for (int i = 0; i <= smallestDegree ; ++i) { sum->Poly[i] = ppoly1->Poly[i] + ppoly2->Poly[i]; } for (int j = smallestDegree + 1; j <= biggestDegree; ++j) { if(biggest) sum->Poly[j] = ppoly2->Poly[j]; else sum->Poly[j] = ppoly1->Poly[j]; } Error = OK; ReducePoly(sum); return sum; }
PtPoly PolyCopy (PtPoly ppoly) { PtPoly Copy; int I; /* verifica se o vector existe - verifies if vector exists */ if (ppoly == NULL) { Error = NO_POLY; return NULL; } /* criação do vector copia nulo - creating an empty vector */ if ((Copy = PolyCreate (ppoly->Degree+1)) == NULL) return NULL; /* fazer a copia do vector - copying the components */ for (I = 0; I < ppoly->Degree+1; I++) Copy->Poly[I] = ppoly->Poly[I]; ReducePoly(Copy); return Copy; /* devolve o vector copia - returning the new vector */ }
PtPoly PolyCreateFile (char *pnomef) { PtPoly Poly; FILE *PtF; unsigned int Degree, I; if ( (PtF = fopen (pnomef, "r")) == NULL) { Error = NO_FILE; return NULL; } fscanf (PtF, "%u", &Degree); if ((Poly = PolyCreate (Degree)) == NULL) { Error = NO_MEM; fclose (PtF); return NULL; } for (I = 0; I <= Degree; I++) fscanf (PtF, "%lf", Poly->Poly+I); Error = OK; fclose (PtF); return Poly; }
PtPoly PolyCreateFile (char *pnomef) { PtPoly Poly; FILE *PtF; unsigned int Degree, I; if ( (PtF = fopen (pnomef, "r")) == NULL) { Error = NO_FILE; return NULL; } /* leitura da dimensão do vector do ficheiro - reading the vector's size from the text file */ fscanf (PtF, "%u", &Degree); /* criação do vector nulo - creating an empty vector */ if ((Poly = PolyCreate (Degree)) == NULL) { fclose (PtF); return NULL; } /* leitura das componentes do vector do ficheiro - reading the vector's components from the text file */ for (I = 0; I <= Degree; I++) fscanf (PtF, "%lf", Poly->Poly+I); fclose (PtF); /* fecho do ficheiro - closing the text file */ Error = OK; return Poly; /* devolve o vector criado - returning the new vector */ }
PtPoly PolySubtraction (PtPoly ppoly1, PtPoly ppoly2) { PtPoly sub; /* Check validity of polynomials */ if (!ValidPolys(ppoly1, ppoly2)) return NULL; int smallestDegree; int biggestDegree; int biggest = 0; /* considera-se o ppoly1 */ if(ppoly1->Degree > ppoly2->Degree){ biggestDegree = ppoly1->Degree; smallestDegree = ppoly2->Degree; }else{ biggestDegree = ppoly2->Degree; smallestDegree = ppoly1->Degree; biggest = 1; } if((sub = PolyCreate(biggestDegree)) == NULL){ return NULL; } for (int i = 0; i <= smallestDegree ; ++i) { sub->Poly[i] = ppoly1->Poly[i] - ppoly2->Poly[i]; } for (int j = smallestDegree + 1; j <= biggestDegree; ++j) { if (biggest && sub->Poly[j] != 0) sub->Poly[j] = ppoly2->Poly[j] * (-1); else sub->Poly[j] = ppoly1->Poly[j]; } ReducePoly(sub); Error = OK; return sub; }
PtPoly PolySymmetrical (PtPoly ppoly) { if(ppoly == NULL){ Error = NO_POLY; return NULL; } PtPoly tmp; if((tmp = PolyCreate(ppoly->Degree)) == NULL){ return NULL; } for (int i = 0; i <= ppoly->Degree ; ++i) { if(ppoly->Poly[i]!=0){ tmp->Poly[i] = -ppoly->Poly[i]; } } return tmp; }
PtPoly PolyMultiplication (PtPoly ppoly1, PtPoly ppoly2) { PtPoly Mult; unsigned int I,J,TotalDegree; if (!ValidPolys (ppoly1, ppoly2)) return NULL; TotalDegree = ppoly1->Degree + ppoly2->Degree; if ((Mult = PolyCreate(TotalDegree)) == NULL) { Error = NO_MEM; return NULL; } Error = OK; for (I = 0; I <= TotalDegree; I++) Mult->Poly[I] = 0; for (I = 0; I <= ppoly1->Degree; I++ ){ for(J = 0; J <= ppoly2->Degree; J++) Mult->Poly[I+J] += ppoly1->Poly[I] * ppoly2->Poly[J]; } ReducePoly(Mult); return Mult; }
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; }