/**************************************************** * This program calculates R(x) = a*P(x) + b*Q(x), * * P(x), Q(x) and R(x) being polynomials. * * ------------------------------------------------- * * Ref.: "Mathématiques en Turbo-Pascal By M. Ducamp * * and A. Reverchon (vol 2), Eyrolles, Paris, 1988" * * [BIBLI 05]. * * ------------------------------------------------- * * SAMPLE RUN: * * * * LINEAR COMBINATION OF TWO POLYNOMIALS: * * * * P(X) = x3 +5/4x2 -8 * * Q(x) = 2x2 -1/7 * * * * a = 1/2 * * b = -1/3 * * * * +1/2 X3 -1/24 X2 -83/21 * * * * ------------------------------------------------- * * Functions used (of module Polynoms): * * * * AddNumber(), EnterPolynom(), DisplayPolynom(), * * MultNumber() and SetNumber(). * * * * C++ version by J-P Moreau. * * (To be linked with polynoms.cpp).* * (www.jpmoreau.fr) * ****************************************************/ #include #include #include #include "polynoms.h" ar_polynom *P, *Q, *R; ar_number a, b; // a P(X) + b Q(X) = R(X) bool CombiPolynom(ar_polynom *P, ar_polynom *Q, ar_number a, ar_number b, ar_polynom *R) { int i; ar_number u,v; if (Q->degree > P->degree) R->degree=Q->degree; else R->degree=P->degree; if (R->degree > AR_MAXPOL) return FALSE; // degree of R is too big for (i=0; i<=R->degree; i++) { if (!SetNumber(&u,"0")) return FALSE; if (!SetNumber(&v,"0")) return FALSE; if (i<=P->degree) if (!MultNumber(a,P->coeff[i], &u)) return FALSE; if (i<=Q->degree) if (!MultNumber(b,Q->coeff[i], &v)) return FALSE; if (!AddNumber(u,v,&R->coeff[i])) return FALSE; } while (R->degree>0 && fabs(R->coeff[R->degree].value)degree--; return TRUE; } void main() { //dynamic memory allocation of polynomials P = (ar_polynom *) calloc(1,sizeof(ar_polynom)); Q = (ar_polynom *) calloc(1,sizeof(ar_polynom)); R = (ar_polynom *) calloc(1,sizeof(ar_polynom)); printf("\n LINEAR COMBINATION OF TWO POLYNOMIALS:\n\n"); if (!EnterPolynom(" P(X) = ", P)) return; if (!EnterPolynom(" Q(X) = ", Q)) return; printf("\n"); ReadNumber(" a = ", &a); ReadNumber(" b = ", &b); if (CombiPolynom(P,Q,a,b,R)) DisplayPolynom(R); else printf(" Error in linear combination."); printf("\n\n"); free(P); free(Q); free(R); } //end of file combipol.cpp