diff options
-rw-r--r-- | include/numbers.h | 4 | ||||
-rw-r--r-- | lib/numbers.c | 18 | ||||
-rw-r--r-- | lib/pile.c | 79 | ||||
-rw-r--r-- | lib/polynom.c | 16 | ||||
-rw-r--r-- | lib/scalaires.c | 18 | ||||
-rw-r--r-- | po/cat-id-tbl.c | 46 | ||||
-rw-r--r-- | src/Polynom.c | 9 |
7 files changed, 114 insertions, 76 deletions
diff --git a/include/numbers.h b/include/numbers.h index 343ca0e..0937d56 100644 --- a/include/numbers.h +++ b/include/numbers.h @@ -1,7 +1,9 @@ #ifndef __NUMBERS_H__ #define __NUMBERS_H__ +#include "scalaires.h" + int char_to_number(char *st, int *valid); -double char_to_double(char *st, int *valid); +rationnel char_to_rat(char *st, int *valid); #endif diff --git a/lib/numbers.c b/lib/numbers.c index af8a9bb..52af66d 100644 --- a/lib/numbers.c +++ b/lib/numbers.c @@ -6,6 +6,7 @@ #include "numbers.h" +#include "scalaires.h" /* Cette fonction lit un nombre. Elle va chercher absolument à traduire la chaîne passée en argument en un nombre. Si ce nombre n'est pas valide, alors l'int valid est mis à faux. Cette fonction reconnais les nombres en décimaux, les nombres @@ -71,22 +72,20 @@ int char_to_number(char *st, int *valid) } -/* TODO: Rajouter la precision dans les valeurs retournees */ -double char_to_double(char *st, int *valid) /* cette fonction tente de traduire une chaine en flottant */ +rationnel char_to_rat(char *st, int *valid) /* cette fonction tente de traduire une chaine en flottant */ { - unsigned int dotnum = 0; - unsigned int deci = 1; - double result = 0; + int dotnum = 0, deci = 1, temp = 0; + while (*st) { if (*st == '.') { dotnum++; } else { if ((*st < '0') || (*st > '9') || (dotnum > 1)) { *valid = 0; - return 0; + return rat_constr_zero(); } else { - result *= 10; - result += *st - '0'; + temp *= 10; + temp += *st - '0'; if (dotnum == 1) deci *= 10; } @@ -94,7 +93,6 @@ double char_to_double(char *st, int *valid) /* cette fonction tente de traduire st++; } - result = result / deci; *valid = 1; - return result; + return rat_constr(temp, deci); } @@ -26,41 +26,44 @@ void push_pile(char *st) int valid1, valid2, valid3; char valid4=0; int i_number; - double d_number; + rationnel r_number; polynome poly; char buf[128]; - sprintf(buf,"appel à push_pile(%s)",st); + sprintf(buf,_("Calling push_pile(%s)"),st); pushcontext(buf); i_number = char_to_number(st, &valid1); - d_number = char_to_double(st, &valid2); + r_number = char_to_rat(st, &valid2); valid3 = is_mute(st); poly = (polynome) NomVarToVar(st, variables, &valid4); if (valid1) { /* il s agit d un entier */ - pushcontext("c est un entier"); + pushcontext(_("it's an integer")); push_pile_poly(ply_constr(rat_constr(i_number, 1), 0)); popcontext(); } else if (valid2) { /* il s agit d un flottant */ - pushcontext("c est un flottant"); - push_pile_poly(ply_constr(rat_constr_from_double(d_number), 0)); + pushcontext(_("it's a float")); + push_pile_poly(ply_constr(r_number, 0)); popcontext(); } else if (valid3) { /* il s agit de x */ - pushcontext("c est X"); + pushcontext(_("it's X")); push_pile_poly(ply_constr(rat_constr(1, 1), 1)); popcontext(); } else if (valid4) { /* il s agit d une variable */ - pushcontext("c est une variable"); + pushcontext(_("it's a variable")); push_pile_poly(ply_copy(poly)); popcontext(); } else { /* il s agit d un nom */ - pushcontext("c est un nom"); + pushcontext(_("it's a name")); push_pile_string(Estrdup(st)); popcontext(); } popcontext(); - affichage_pile(); - fprintf(stderr, "sortie de push_pile\n"); + +#ifdef DEBUG + + fprintf(stderr, "exiting push_pile\n"); +#endif } @@ -78,7 +81,6 @@ void push_pile_poly(polynome poly) void push_pile_int(int val) { - fprintf(stderr,"%d",val); if (pile_ptr != PILE_MAX) { pile[pile_ptr].type = T_INT; pile[pile_ptr].val = val; @@ -137,7 +139,7 @@ char *affichage_level_1(void) } int is_mute(char *st) -{ /* FIXME: test lowercase / uppercase */ +{ return !(strcmp(st, mute)); } @@ -148,12 +150,16 @@ void act_pile(int func) pile_elem operande1, operande2; char buf[50]; - sprintf(buf, _("Calling act_pile(%i)\n"), func); + sprintf(buf, _("Calling act_pile(%i)"), func); pushcontext(buf); +#ifdef DEBUG affichage_pile(); +#endif switch (func) { case OP_PLUS: +#ifdef DEBUG printf("----- OP_PLUS\n"); +#endif operande1 = pop_pile(1); operande2 = pop_pile(1); if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { @@ -167,9 +173,11 @@ void act_pile(int func) } break; case OP_MOINS: +#ifdef DEBUG printf("----- OP_MOINS\n"); - operande1 = pop_pile(1); +#endif operande2 = pop_pile(1); + operande1 = pop_pile(1); if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { push_pile_poly(ply_soustraction(operande1.poly, operande2.poly)); if (operande1.poly) @@ -181,7 +189,9 @@ void act_pile(int func) } break; case OP_MUL: +#ifdef DEBUG printf("----- OP_MUL\n"); +#endif operande1 = pop_pile(1); operande2 = pop_pile(1); if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { @@ -195,9 +205,11 @@ void act_pile(int func) } break; case OP_DIV: +#ifdef DEBUG printf("----- OP_DIV\n"); - operande1 = pop_pile(1); +#endif operande2 = pop_pile(1); + operande1 = pop_pile(1); if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { push_pile_poly(ply_division(operande1.poly, operande2.poly)); if (operande1.poly) @@ -209,9 +221,11 @@ void act_pile(int func) } break; case OP_MOD: +#ifdef DEBUG printf("----- OP_MOD\n"); - operande1 = pop_pile(1); +#endif operande2 = pop_pile(1); + operande1 = pop_pile(1); if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { push_pile_poly(ply_modulo(operande1.poly, operande2.poly)); if (operande1.poly) @@ -223,7 +237,9 @@ void act_pile(int func) } break; case OP_EXP: +#ifdef DEBUG printf("----- OP_EXP\n"); +#endif operande1 = pop_pile(1); operande2 = pop_pile(1); if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { @@ -246,7 +262,9 @@ void act_pile(int func) } break; case OP_ASSIGN: +#ifdef DEBUG printf("----- OP_ASSIGN\n"); +#endif operande1 = pop_pile(1); operande2 = pop_pile(1); if ((operande1.type == T_POLY) && (operande2.type == T_STRING)) { @@ -263,12 +281,14 @@ void act_pile(int func) } break; case OP_PLUS_UNARY: +#ifdef DEBUG printf("----- OP_PLUS_UNARY\n"); - +#endif break; case OP_MOINS_UNARY: +#ifdef DEBUG printf("----- OP_MOINS_UNARY\n"); - +#endif operande1 = pop_pile(1); if (operande1.type == T_POLY) { push_pile_poly(ply_soustraction @@ -280,8 +300,9 @@ void act_pile(int func) } break; case OP_FUNC_CALL: +#ifdef DEBUG printf("----- OP_FUNC_CALL\n"); - +#endif operande1 = pop_pile(1); if ((operande1.type == T_INT) && (operande1.val == 1)) { operande1 = pop_pile(1); @@ -289,19 +310,9 @@ void act_pile(int func) if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { if (operande2.poly) { if (operande1.poly->degre == 0) { - /*double soja, soja2; - rationnel rat; - polynome pl; - fprintf(stderr,"soja"); - soja=rat_to_double(operande1.poly->coef); - fprintf(stderr,"%f",soja); - soja2=ply_valuation(operande2.poly,soja); - fprintf(stderr,"%f", soja2); - rat=rat_constr_from_double(soja2); - fprintf(stderr,"pl"); - pl=ply_constr(rat, 1);*/ + -push_pile_poly(ply_constr(rat_constr_from_double(ply_valuation(operande2.poly,rat_to_double(operande1.poly->coef))), 0)); + push_pile_poly(ply_constr(rat_constr_from_double(ply_valuation(operande2.poly,rat_to_double(operande1.poly->coef))), 0)); if (operande1.poly) ply_destruct(operande1.poly); @@ -330,7 +341,7 @@ push_pile_poly(ply_constr(rat_constr_from_double(ply_valuation(operande2.poly,ra void affichage_pile(void) { int i; - printf("\t-- affichage de la pile\n"); + printf(_("\t-- Printing Stack\n")); if (pile_ptr) { for (i=0;i<=pile_ptr-1;i++) { switch (pile[i].type) { @@ -346,6 +357,6 @@ void affichage_pile(void) { } } } - printf("\t-- fin d affichage de pile\n"); + printf(_("\t-- End Printing Stack\n")); } diff --git a/lib/polynom.c b/lib/polynom.c index 4c789dd..aca2f6c 100644 --- a/lib/polynom.c +++ b/lib/polynom.c @@ -18,7 +18,7 @@ #define _(x) x #endif -/* FIXME: manque div et mod et poly_to_string */ +/* FIXME: manque div et mod */ polynome ply_constr(rationnel coef, int degre) { /* constructeur monome */ @@ -271,8 +271,9 @@ double ply_valuation(polynome poly, double point) char *ply_affichage(polynome poly) { /* routine d'affichage d'un polynome */ - char buf[BUFSIZ] = {0}, temp[BUFSIZ]; /* FIXME: pas glop comme routine, malloquer tout ca ? */ - + char buf[BUFSIZ] = {0}, temp[BUFSIZ]; + int count=0; + while (poly) { if (poly->degre != 0) { if (poly->coef.denom==1) @@ -285,11 +286,12 @@ char *ply_affichage(polynome poly) else sprintf(temp, "%+d/%d ", poly->coef.num , poly->coef.denom); } - strcat(buf, temp); /* FIXME: gerer le depassement de buf si po malloc */ + count+=strlen(temp); + if (count<BUFSIZ) + strcat(buf, temp); + else + exception(1,_("ply_affichage: strcat error, not enoug space in buffer")); poly=poly->suiv; - - - } return Estrdup(buf); } diff --git a/lib/scalaires.c b/lib/scalaires.c index b853741..14b3db2 100644 --- a/lib/scalaires.c +++ b/lib/scalaires.c @@ -7,6 +7,12 @@ #include "scalaires.h" #include "exceptions.h" #include <math.h> +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#define _(x) x +#endif + #define PRECISION 1E6 static int pgcd(int a, int b) @@ -31,19 +37,25 @@ rationnel rat_constr_zero(void) rationnel rat_constr(int num, int denom) { /* cree une fraction */ rationnel temp; + int sgnnum = 1, sgndenom = 1; + if (num < 0) { + sgnnum = -1; + num = -num; + } + if (denom < 0) { + sgndenom = -1; denom = -denom; - num = -num; } if (!num) { temp.num=0; temp.denom=1; } else if (denom) { - temp.num = num / pgcd(num, denom); + temp.num = sgnnum * sgndenom * num / pgcd(num, denom); temp.denom = denom / pgcd(num, denom); } else { - exception(1,"rat_constr: division par zero"); + exception(1,_("rat_constr: division by zero")); } return temp; diff --git a/po/cat-id-tbl.c b/po/cat-id-tbl.c index 85ff112..705b1ba 100644 --- a/po/cat-id-tbl.c +++ b/po/cat-id-tbl.c @@ -20,23 +20,33 @@ const struct _msg_ent _msg_tbl[] = { {"Parse error: too much right parenthesis", 11}, {"Parse error: enclosure mismatch", 12}, {"Invalid character", 13}, - {"push_pile_poly: Stack Overflow", 14}, - {"push_pile_int: Stack Overflow", 15}, - {"push_pile_string: Stack Overflow", 16}, - {"pop_pile: Can't pop %u elements", 17}, - {"Calling act_pile(%i)\n", 18}, - {"act_pile: OP_PLUS invalid arguments", 19}, - {"act_pile: OP_MOINS invalid arguments", 20}, - {"act_pile: OP_MUL invalid arguments", 21}, - {"act_pile: OP_DIV invalid arguments", 22}, - {"act_pile: OP_MOD invalid arguments", 23}, - {"act_pile: OP_EXP invalid arguments", 24}, - {"act_pile: OP_ASSIGN empty string", 25}, - {"act_pile: OP_ASSIGN invalid arguments", 26}, - {"act_pile: OP_MOINS_UNARY invalid argument", 27}, - {"act_pile: OP_FUNC_CALL invalid arguments", 28}, - {"act_pile: OP_FUNC_CALL incorrect argument number", 29}, - {"act_pile: Unknown operator", 30}, + {"Calling push_pile(%s)", 14}, + {"it's an integer", 15}, + {"it's a float", 16}, + {"it's X", 17}, + {"it's a variable", 18}, + {"it's a name", 19}, + {"push_pile_poly: Stack Overflow", 20}, + {"push_pile_int: Stack Overflow", 21}, + {"push_pile_string: Stack Overflow", 22}, + {"pop_pile: Can't pop %u elements", 23}, + {"Calling act_pile(%i)", 24}, + {"act_pile: OP_PLUS invalid arguments", 25}, + {"act_pile: OP_MOINS invalid arguments", 26}, + {"act_pile: OP_MUL invalid arguments", 27}, + {"act_pile: OP_DIV invalid arguments", 28}, + {"act_pile: OP_MOD invalid arguments", 29}, + {"act_pile: OP_EXP invalid arguments", 30}, + {"act_pile: OP_ASSIGN empty string", 31}, + {"act_pile: OP_ASSIGN invalid arguments", 32}, + {"act_pile: OP_MOINS_UNARY invalid argument", 33}, + {"act_pile: OP_FUNC_CALL invalid arguments", 34}, + {"act_pile: OP_FUNC_CALL incorrect argument number", 35}, + {"act_pile: Unknown operator", 36}, + {"\t-- Printing Stack\n", 37}, + {"\t-- End Printing Stack\n", 38}, + {"ply_affichage: strcat error, not enoug space in buffer", 39}, + {"rat_constr: division by zero", 40}, }; -int _msg_tbl_length = 30; +int _msg_tbl_length = 40; diff --git a/src/Polynom.c b/src/Polynom.c index 433da7b..d96d1a4 100644 --- a/src/Polynom.c +++ b/src/Polynom.c @@ -29,17 +29,20 @@ int main(void) Initialise(&variables); mute = "x"; - /* nom de la variable utilisee pour la saisie des polynomes, a recuperer en argv eventuellt */ - parse_line("P=3*x^2+2*x +4.5;"); + /* nom de la variable utilisee pour la saisie des polynomes, a recuperer en argv eventuellt + ATTENTION: elle est case sensitive*/ + parse_line("P=-4.5+2*x+3*x^2;"); //parse_line("P(2);"); //AfficheTableau(variables); //printf("%p\n",(polynome)NomVarToVar("P",variables,&valid)); //printf("-- affichage:%s\n",ply_affichage((polynome)NomVarToVar("P",variables,&valid))); parse_line("P(2);"); + pop_pile(1); parse_line("Q=6*x^3+4*x^2+x;"); - parse_line("soja=P*Q+2;"); + parse_line("soja=P-Q+2;"); parse_line("soja;"); + parse_line("soja^3;"); //fprintf(stderr, "soja"); printf("Resultat: %s\n", affichage_level_1()); return 0; |