123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "pile.h"
-
- void Init_Pile(struct Pile *new)
- {
- //struct Pile new;
- new->Pil=NULL;
- new->nbElements=0;
- //return new;
- }
-
- struct Nombre Typenum(char * C)
- {
- struct Nombre k;
- //enum Numtype type;
- //union Test i;
- k.typ = Entier;
- for(int i=0; i<strlen(C); i++)
- {
- if( C[i] == '.')
- {
- k.typ=Reel;
- }
- }
-
- if(k.typ == Entier)
- {
- k.nombr.ValEntier = atoi(C);
- }
- else if(k.typ == Reel)
- {
- k.nombr.ValReel = atof(C);
- }
-
- return k;
-
- }
-
- 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));
- struct Nombre i = Typenum(nb);
-
- if (i.typ == Entier)
- {
- //cel->Ent.nombr.ValEntier = i.nombr.ValEntier;
- //cel->Ent.typ = Entier;
- cel->Ent = i;
- }
- else if (i.typ == Reel)
- {
- //cel->Ent.nombr.ValReel = i.nombr.ValReel;
- //cel->Ent.typ = Reel;
- cel->Ent = i;
- }
- cel->Suiv = P->Pil;
- P->Pil = cel;
- P->nbElements +=1;
- }
-
-
-
- void EmpilerR(struct Pile * P, struct Nombre i)
- {
- struct Cell * cel= malloc(sizeof(struct Cell));
- cel->Ent = i;
- cel->Suiv = P->Pil;
- P->Pil = cel;
- P->nbElements +=1;
- }
-
- struct Cell * Depiler(struct Pile * P)
- {
- struct Cell * res= NULL;
- if (P->Pil != NULL)
- {
- res = P->Pil;
- P->Pil= P->Pil->Suiv;
-
- }
-
- return res;
- }
-
- void Supprimer(struct Pile *P){
- struct Cell * tmp;
- if(P->nbElements != 0)
- {
- tmp = P->Pil;
- P->Pil = P->Pil->Suiv;
- free(tmp);
- }
- }
-
-
- void Affiche_Nombre(struct Nombre nb)
- {
- if(nb.typ == Entier)
- {
- printf("%d \n", nb.nombr.ValEntier);
- }
- else if(nb.typ == Reel)
- {
- printf("%f \n", nb.nombr.ValReel);
- }
- }
-
- void Affichage(struct Cell * C)
- {
- if(C == NULL)
- {
- //printf("Rien a afficher");
- }
- else
- {
- Affiche_Nombre(C->Ent);
- Affichage(C->Suiv);
- }
- }
-
- int numberOfDelimiters(char* string) {
- int count = 0;
- for (int i=0;i<strlen(string);i++) {
- if (string[i] == ' ')
- {count++;}
- }
- return count;
- }
-
- Programme* lexer(char* chaine) {
- char *token,*str;
- str = strdup(chaine);
- int i=0;
- int arraysize = (numberOfDelimiters(str)+1);
- char** programme = (char**)malloc(sizeof(char*)*arraysize);
-
- while ( (token = strsep(&str, " ")) ) {
- programme[i] = token;
- i++;
- }
-
- Programme *retour = (Programme*)malloc(sizeof(Programme));
- retour->tokens = programme;
- retour->taille = i;
- return retour;
- }
-
- void Executer(Etat * etat){
- //struct Pile P = etat->Donnee;
- //Programme Program = etat->Prog;
- struct Cell *C1, *C2;
- int tail = etat->Prog->taille;
- struct Nombre r;
- char * element;
- for(int i = 0;i <tail; i++)
- {
- element = (etat->Prog->tokens[i]);
- //printf("%s \n", element);
-
-
- if ( *element == '+')
- {
- C2 = Depiler(etat->Donnee);
- C1 = Depiler(etat->Donnee);
- if(C1 != NULL && C2 != NULL)
- {
- //r= C1->Ent.ValEntier + C2->Ent.ValEntier;
- r = Addition(C1->Ent, C2->Ent);
- EmpilerR(etat->Donnee, r );
- }
- else
- {
- printf("ERROR \n");
- }
- }
- else if( *element == '/')
- {
- C2 = Depiler(etat->Donnee);
- C1 = Depiler(etat->Donnee);
- if(C1 != NULL && C2 != NULL)
- {
- //r= C1->Ent.ValEntier / C2->Ent.ValEntier;
- r=Diviser(C1->Ent,C2->Ent);
- EmpilerR(etat->Donnee, r );
- }
- else
- {
- printf("ERROR \n");
- }
- }
- else if( *element == '-')
- {
- C2 = Depiler(etat->Donnee);
- C1 = Depiler(etat->Donnee);
- if(C1 != NULL && C2 != NULL)
- {
- //r= C1->Ent.ValEntier - C2->Ent.ValEntier;
- r = Soustraire(C1->Ent ,C2->Ent);
- EmpilerR(etat->Donnee, r );
- }
- else
- {
- printf("ERROR \n");
- }
-
- }
- else if(*element == '*')
- {
- C2 = Depiler(etat->Donnee);
- C1 = Depiler(etat->Donnee);
- if(C1 != NULL && C2 != NULL)
- {
- //r= C1->Ent.ValEntier * C2->Ent.ValEntier;
- r = Multiplier(C1->Ent,C2->Ent);
- EmpilerR(etat->Donnee, r );
- }
- else
- {
- printf("ERROR \n");
- }
- }
- else
- {
- Empiler(etat->Donnee, element);
- }
-
- }
-
- //printf("SORTIE : %d \n", etat->Donnee->Pil->Ent.ValEntier);
- printf("SORTIE :");
- Affiche_Nombre(etat->Donnee->Pil->Ent);
- }
|