tp3_C/pile.c
2022-12-07 11:52:12 +01:00

339 lignes
Pas d'EOL
7,5 Kio
C

#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);
}