summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorbiouman <biouman>2001-04-30 01:21:20 +0000
committerbiouman <biouman>2001-04-30 01:21:20 +0000
commit51a7864b4b789583ea445af47324a9b613336162 (patch)
tree3c786d88388db0a88f65f1fe7acbe9125280fa08 /lib
parenta578580b607c8cadf22b457d51431042e3896271 (diff)
*** empty log message ***
Diffstat (limited to 'lib')
-rw-r--r--lib/numbers.c18
-rw-r--r--lib/pile.c79
-rw-r--r--lib/polynom.c16
-rw-r--r--lib/scalaires.c18
4 files changed, 77 insertions, 54 deletions
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);
}
diff --git a/lib/pile.c b/lib/pile.c
index c3f6644..4e510b3 100644
--- a/lib/pile.c
+++ b/lib/pile.c
@@ -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;