/* * * fonctions avancees sur les polynomes * */ #include "fonctions.h" #include "pile.h" #include "hash.h" #include "main.h" #include "scalaires.h" #include "terminal.h" #include "exceptions.h" #include "polynom.h" #include "interface.h" #ifdef HAVE_CONFIG_H #include "config.h" #else #define _(x) x #endif /* * Nous allons utiliser des pointeurs sur des fonctions. Voici donc les typedefs pour notre * structure... */ typedef void (*func_name) (polynome arg1, polynome arg2, polynome arg3); typedef struct func_t { func_name func; char *nom; int arite; } func_t; /* * ... et la structure elle-meme */ static func_t func_table[] = { {deriv, "deriv", 1,}, {derivn, "derivn", 2}, {integ, "int", 1}, {printvars, "printvars", 0}, {help, "help", 0}, {setdisplay, "setdisplay", 1}, {reinit, "reinit", 0}, {exit_call, "exit", 0}, {setsmartprint, "setsmartprint", 1}, {NULL, NULL, -1} }; /* * On cherche simplement la routine a appeler parmi la table des fonctions */ void appel_fonction(char *nom, int arite, polynome p1, polynome p2, polynome p3) { int i = 0; int trouve = 0; while ((func_table[i].nom) && (!trouve)) { if (!strcmp(func_table[i].nom, nom)) trouve = 1; i++; } if (trouve) { if (func_table[i - 1].arite == arite) { (*func_table[i - 1].func) (p1, p2, p3); } else { exception(1, _("appel_fonction: incorrect arg number")); } } else { exception(1, _("appel_fonction: non-existent function")); } } /* * Fonction de derivation - rajoute le resultat sur la pile. */ void deriv(polynome p1, polynome p2, polynome p3) { polynome resultat = NULL, temp = NULL, t; while (p1) { if (p1->degre) { t = ply_constr(rat_constr((p1->coef.num) * (p1->degre), p1->coef.denom), (p1->degre - 1)); if (t) { if (resultat) { temp->suiv = t; temp = t; } else { resultat = t; temp = t; } } } p1 = p1->suiv; } push_pile_poly(resultat); } /* * Fonction paresseuse */ void derivn(polynome p1, polynome p2, polynome p3) { pile_elem temp; int i; if (p1) { if ((p1->degre == 0) && (p1->coef.num > 0) && (p1->coef.denom == 1)) { push_pile_poly(NULL); for (i = 0; i < p1->coef.num; i++) { temp = pop_pile(1); ply_destruct(temp.poly); deriv(temp.poly, NULL, NULL); } } else { exception(1, _("derivn: invalid 2nd arg")); } } else { exception(1, _("derivn: invalid 2nd arg")); } } /* * Integration d'un polynome */ void integ(polynome p1, polynome p2, polynome p3) { polynome resultat = NULL, temp = NULL, t; while (p1) { t = ply_constr(rat_constr((p1->coef.num), (p1->coef.denom) * (p1->degre + 1)), (p1->degre + 1)); if (t) { if (resultat) { temp->suiv = t; temp = t; } else { resultat = t; temp = t; } } p1 = p1->suiv; } push_pile_poly(resultat); } /* * Quelques fonctions explicites... */ void printvars(polynome p1, polynome p2, polynome p3) { AfficheTableau(variables); } void help(polynome p1, polynome p2, polynome p3) { printf(_("Available functions:\n" ". deriv(p); first derivative of p\n" ". derivn(p, n); nth derivative of p\n" ". integ(p); primitive of p\n" ". printvars(); print all variables\n" ". help(); this help message\n" ". setdisplay(n); set integer display format\n" "\tn=1: DECIMAL, n=2: HEXA\n" "\tn=3: OCTAL, n=4: FLOAT\n" ". smartprint(bool); toggle smart print of polynoms\n" ". reinit(); clear all variables\n" ". exit(); end program\n")); } void setdisplay(polynome p1, polynome p2, polynome p3) { if (p1) { if ((!p1->degre)) { switch (p1->coef.num) { case 1: display = DEC; break; case 2: display = HEX; break; case 3: display = OCT; break; case 4: display = FLT; break; default: exception(1, _("setdisplay: invalid arg")); } } else { exception(1, _("setdisplay: invalid arg")); } } else { exception(1, _("setdisplay: invalid arg")); } } void reinit(polynome p1, polynome p2, polynome p3) { DetruitTab(&variables); Initialise(&variables); } void exit_call(polynome p1, polynome p2, polynome p3) { quit = 1; } void setsmartprint(polynome p1, polynome p2, polynome p3) { if (p1) { if ((!p1->degre)) { smartprint = p1->coef.num; } else { exception(1, _("setsmartprint: invalid arg")); } } else { smartprint = 0; } }