on peut executer entier ou float

This commit is contained in:
Olougouna Axel 2022-12-07 11:52:12 +01:00
parent cdc968af29
commit 800a188a2b
3 changed files with 136 additions and 21 deletions

8
main.c
View file

@ -6,14 +6,14 @@
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
struct Pile P; /*struct Pile P;
Init_Pile(&P); Init_Pile(&P);
Empiler(&P, "4"); Empiler(&P, "4");
Empiler(&P, "1.0"); Empiler(&P, "1.0");
//struct Cell * res = Depiler(&P); //struct Cell * res = Depiler(&P);
//Supprimer(&P); //Supprimer(&P);
Affichage(P.Pil); Affichage(P.Pil);
//Affiche_Nombre(res->Ent); //Affiche_Nombre(res->Ent);*/
/*Programme * P; /*Programme * P;
P = lexer(argv[1]); P = lexer(argv[1]);
@ -28,7 +28,7 @@ int main(int argc, char * argv[])
printf("TOKEN : %s \n", E->Prog->tokens[i]); printf("TOKEN : %s \n", E->Prog->tokens[i]);
}*/ }*/
/*Etat * E; Etat * E;
E= malloc(sizeof(Etat)); E= malloc(sizeof(Etat));
E->Donnee = malloc(sizeof(struct Pile)); E->Donnee = malloc(sizeof(struct Pile));
Init_Pile(E->Donnee); Init_Pile(E->Donnee);
@ -36,5 +36,5 @@ int main(int argc, char * argv[])
E->Prog = lexer(argv[1]); E->Prog = lexer(argv[1]);
Executer(E);*/ Executer(E);
} }

145
pile.c
View file

@ -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) void Empiler(struct Pile * P, char * nb)
{ {
struct Cell * cel= malloc(sizeof(struct Cell)); struct Cell * cel= malloc(sizeof(struct Cell));
@ -61,14 +165,16 @@ void Empiler(struct Pile * P, char * nb)
P->nbElements +=1; 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)); struct Cell * cel= malloc(sizeof(struct Cell));
cel->Ent.ValReel = i; cel->Ent = i;
cel->Suiv = P->Pil; cel->Suiv = P->Pil;
P->Pil = cel; P->Pil = cel;
P->nbElements +=1; P->nbElements +=1;
}*/ }
struct Cell * Depiler(struct Pile * P) struct Cell * Depiler(struct Pile * P)
{ {
@ -146,12 +252,12 @@ Programme* lexer(char* chaine) {
return retour; return retour;
} }
/*void Executer(Etat * etat){ void Executer(Etat * etat){
//struct Pile P = etat->Donnee; //struct Pile P = etat->Donnee;
//Programme Program = etat->Prog; //Programme Program = etat->Prog;
struct Cell *C1, *C2; struct Cell *C1, *C2;
int tail = etat->Prog->taille; int tail = etat->Prog->taille;
int r=0; struct Nombre r;
char * element; char * element;
for(int i = 0;i <tail; i++) for(int i = 0;i <tail; i++)
{ {
@ -165,9 +271,9 @@ Programme* lexer(char* chaine) {
C1 = Depiler(etat->Donnee); C1 = Depiler(etat->Donnee);
if(C1 != NULL && C2 != NULL) if(C1 != NULL && C2 != NULL)
{ {
r= C1->Ent.ValEntier + C2->Ent.ValEntier; //r= C1->Ent.ValEntier + C2->Ent.ValEntier;
r = Addition(C1->Ent, C2->Ent);
Empiler(etat->Donnee, r ); EmpilerR(etat->Donnee, r );
} }
else else
{ {
@ -180,8 +286,9 @@ Programme* lexer(char* chaine) {
C1 = Depiler(etat->Donnee); C1 = Depiler(etat->Donnee);
if(C1 != NULL && C2 != NULL) if(C1 != NULL && C2 != NULL)
{ {
r= C1->Ent.ValEntier / C2->Ent.ValEntier; //r= C1->Ent.ValEntier / C2->Ent.ValEntier;
Empiler(etat->Donnee, r ); r=Diviser(C1->Ent,C2->Ent);
EmpilerR(etat->Donnee, r );
} }
else else
{ {
@ -194,8 +301,9 @@ Programme* lexer(char* chaine) {
C1 = Depiler(etat->Donnee); C1 = Depiler(etat->Donnee);
if(C1 != NULL && C2 != NULL) if(C1 != NULL && C2 != NULL)
{ {
r= C1->Ent.ValEntier - C2->Ent.ValEntier; //r= C1->Ent.ValEntier - C2->Ent.ValEntier;
Empiler(etat->Donnee, r ); r = Soustraire(C1->Ent ,C2->Ent);
EmpilerR(etat->Donnee, r );
} }
else else
{ {
@ -209,8 +317,9 @@ Programme* lexer(char* chaine) {
C1 = Depiler(etat->Donnee); C1 = Depiler(etat->Donnee);
if(C1 != NULL && C2 != NULL) if(C1 != NULL && C2 != NULL)
{ {
r= C1->Ent.ValEntier * C2->Ent.ValEntier; //r= C1->Ent.ValEntier * C2->Ent.ValEntier;
Empiler(etat->Donnee, r ); r = Multiplier(C1->Ent,C2->Ent);
EmpilerR(etat->Donnee, r );
} }
else else
{ {
@ -219,10 +328,12 @@ Programme* lexer(char* chaine) {
} }
else else
{ {
Empiler(etat->Donnee, atoi(element)); Empiler(etat->Donnee, element);
} }
} }
printf("SORTIE : %d \n", etat->Donnee->Pil->Ent.ValEntier); //printf("SORTIE : %d \n", etat->Donnee->Pil->Ent.ValEntier);
}*/ printf("SORTIE :");
Affiche_Nombre(etat->Donnee->Pil->Ent);
}

4
pile.h
View file

@ -34,6 +34,10 @@ typedef struct Etat{
} Etat; } Etat;
struct Nombre Typenum(char * C); 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 Init_Pile(struct Pile * new);
void Empiler(struct Pile * P, char * nb); void Empiler(struct Pile * P, char * nb);
struct Cell * Depiler(struct Pile * P); struct Cell * Depiler(struct Pile * P);