tp3_C/pile.c
2022-12-06 21:37:58 +01:00

220 行
無檔案結尾符
4.5 KiB
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;
}
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;
else if (i.typ == Reel)
cel->Ent.nombr.ValReel = i.nombr.ValReel;
cel->Suiv = P->Pil;
P->Pil = cel;
P->nbElements +=1;
}
/*void EmpilerR(struct Pile * P, float i)
{
struct Cell * cel= malloc(sizeof(struct Cell));
cel->Ent.ValReel = 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;
int r=0;
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;
Empiler(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;
Empiler(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;
Empiler(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;
Empiler(etat->Donnee, r );
}
else
{
printf("ERROR \n");
}
}
else
{
Empiler(etat->Donnee, atoi(element));
}
}
printf("SORTIE : %d \n", etat->Donnee->Pil->Ent.ValEntier);
}*/