From 800a188a2b034d6c0c83dcaae9080295be211673 Mon Sep 17 00:00:00 2001 From: Olougouna Axel Date: Wed, 7 Dec 2022 11:52:12 +0100 Subject: [PATCH] on peut executer entier ou float --- main.c | 8 ++-- pile.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++------- pile.h | 4 ++ 3 files changed, 136 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index 71f3277..d0c2eee 100644 --- a/main.c +++ b/main.c @@ -6,14 +6,14 @@ int main(int argc, char * argv[]) { - struct Pile P; + /*struct Pile P; Init_Pile(&P); Empiler(&P, "4"); Empiler(&P, "1.0"); //struct Cell * res = Depiler(&P); //Supprimer(&P); Affichage(P.Pil); - //Affiche_Nombre(res->Ent); + //Affiche_Nombre(res->Ent);*/ /*Programme * P; P = lexer(argv[1]); @@ -28,7 +28,7 @@ int main(int argc, char * argv[]) printf("TOKEN : %s \n", E->Prog->tokens[i]); }*/ - /*Etat * E; + Etat * E; E= malloc(sizeof(Etat)); E->Donnee = malloc(sizeof(struct Pile)); Init_Pile(E->Donnee); @@ -36,5 +36,5 @@ int main(int argc, char * argv[]) E->Prog = lexer(argv[1]); - Executer(E);*/ + Executer(E); } \ No newline at end of file diff --git a/pile.c b/pile.c index 1671fe3..fb0913b 100644 --- a/pile.c +++ b/pile.c @@ -39,6 +39,110 @@ struct Nombre Typenum(char * C) } +struct Nombre Addition(struct Nombre a, struct Nombre b) +{ + struct Nombre e; + if(a.typ == Entier && b.typ == Entier) + { + e.typ = Entier; + e.nombr.ValEntier = a.nombr.ValEntier + b.nombr.ValEntier; + } + else if(a.typ == Entier && b.typ == Reel) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValEntier + b.nombr.ValReel; + } + else if(a.typ == Reel && b.typ == Entier) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValReel + b.nombr.ValEntier; + } + else if(a.typ == Reel && b.typ == Reel) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValReel + b.nombr.ValReel; + } + return e; +} + +struct Nombre Soustraire(struct Nombre a, struct Nombre b) +{ + struct Nombre e; + if(a.typ == Entier && b.typ == Entier) + { + e.typ = Entier; + e.nombr.ValEntier = a.nombr.ValEntier - b.nombr.ValEntier; + } + else if(a.typ == Entier && b.typ == Reel) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValEntier - b.nombr.ValReel; + } + else if(a.typ == Reel && b.typ == Entier) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValReel - b.nombr.ValEntier; + } + else if(a.typ == Reel && b.typ == Reel) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValReel - b.nombr.ValReel; + } + return e; +} + +struct Nombre Multiplier(struct Nombre a, struct Nombre b) +{ + struct Nombre e; + if(a.typ == Entier && b.typ == Entier) + { + e.typ = Entier; + e.nombr.ValEntier = a.nombr.ValEntier * b.nombr.ValEntier; + } + else if(a.typ == Entier && b.typ == Reel) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValEntier * b.nombr.ValReel; + } + else if(a.typ == Reel && b.typ == Entier) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValReel * b.nombr.ValEntier; + } + else if(a.typ == Reel && b.typ == Reel) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValReel * b.nombr.ValReel; + } + return e; +} + +struct Nombre Diviser(struct Nombre a, struct Nombre b) +{ + struct Nombre e; + if(a.typ == Entier && b.typ == Entier) + { + e.typ = Entier; + e.nombr.ValEntier = a.nombr.ValEntier / b.nombr.ValEntier; + } + else if(a.typ == Entier && b.typ == Reel) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValEntier / b.nombr.ValReel; + } + else if(a.typ == Reel && b.typ == Entier) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValReel / b.nombr.ValEntier; + } + else if(a.typ == Reel && b.typ == Reel) + { + e.typ = Reel; + e.nombr.ValReel = a.nombr.ValReel / b.nombr.ValReel; + } + return e; +} + void Empiler(struct Pile * P, char * nb) { struct Cell * cel= malloc(sizeof(struct Cell)); @@ -61,14 +165,16 @@ void Empiler(struct Pile * P, char * nb) P->nbElements +=1; } -/*void EmpilerR(struct Pile * P, float i) + + +void EmpilerR(struct Pile * P, struct Nombre i) { struct Cell * cel= malloc(sizeof(struct Cell)); - cel->Ent.ValReel = i; + cel->Ent = i; cel->Suiv = P->Pil; P->Pil = cel; P->nbElements +=1; -}*/ +} struct Cell * Depiler(struct Pile * P) { @@ -146,12 +252,12 @@ Programme* lexer(char* chaine) { return retour; } -/*void Executer(Etat * etat){ +void Executer(Etat * etat){ //struct Pile P = etat->Donnee; //Programme Program = etat->Prog; struct Cell *C1, *C2; int tail = etat->Prog->taille; - int r=0; + struct Nombre r; char * element; for(int i = 0;i Donnee); if(C1 != NULL && C2 != NULL) { - r= C1->Ent.ValEntier + C2->Ent.ValEntier; - - Empiler(etat->Donnee, r ); + //r= C1->Ent.ValEntier + C2->Ent.ValEntier; + r = Addition(C1->Ent, C2->Ent); + EmpilerR(etat->Donnee, r ); } else { @@ -180,8 +286,9 @@ Programme* lexer(char* chaine) { C1 = Depiler(etat->Donnee); if(C1 != NULL && C2 != NULL) { - r= C1->Ent.ValEntier / C2->Ent.ValEntier; - Empiler(etat->Donnee, r ); + //r= C1->Ent.ValEntier / C2->Ent.ValEntier; + r=Diviser(C1->Ent,C2->Ent); + EmpilerR(etat->Donnee, r ); } else { @@ -194,8 +301,9 @@ Programme* lexer(char* chaine) { C1 = Depiler(etat->Donnee); if(C1 != NULL && C2 != NULL) { - r= C1->Ent.ValEntier - C2->Ent.ValEntier; - Empiler(etat->Donnee, r ); + //r= C1->Ent.ValEntier - C2->Ent.ValEntier; + r = Soustraire(C1->Ent ,C2->Ent); + EmpilerR(etat->Donnee, r ); } else { @@ -209,8 +317,9 @@ Programme* lexer(char* chaine) { C1 = Depiler(etat->Donnee); if(C1 != NULL && C2 != NULL) { - r= C1->Ent.ValEntier * C2->Ent.ValEntier; - Empiler(etat->Donnee, r ); + //r= C1->Ent.ValEntier * C2->Ent.ValEntier; + r = Multiplier(C1->Ent,C2->Ent); + EmpilerR(etat->Donnee, r ); } else { @@ -219,10 +328,12 @@ Programme* lexer(char* chaine) { } else { - Empiler(etat->Donnee, atoi(element)); + Empiler(etat->Donnee, element); } } - printf("SORTIE : %d \n", etat->Donnee->Pil->Ent.ValEntier); -}*/ \ No newline at end of file + //printf("SORTIE : %d \n", etat->Donnee->Pil->Ent.ValEntier); + printf("SORTIE :"); + Affiche_Nombre(etat->Donnee->Pil->Ent); +} \ No newline at end of file diff --git a/pile.h b/pile.h index 19b4431..6056fc7 100644 --- a/pile.h +++ b/pile.h @@ -34,6 +34,10 @@ typedef struct Etat{ } Etat; struct Nombre Typenum(char * C); +struct Nombre Addition(struct Nombre a, struct Nombre b); +struct Nombre Soustraire(struct Nombre a, struct Nombre b); +struct Nombre Multiplier(struct Nombre a, struct Nombre b); +struct Nombre Diviser(struct Nombre a, struct Nombre b); void Init_Pile(struct Pile * new); void Empiler(struct Pile * P, char * nb); struct Cell * Depiler(struct Pile * P);