mes fichiers locaux

This commit is contained in:
Axel O 2022-12-01 23:42:05 +01:00
commit 41b3703245
3 changed files with 128 additions and 0 deletions

22
main.c Normal file
View file

@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "pile.h"
int main(int argc, char * argv[])
{
/*struct Pile P;
Init_Pile(&P);
Empiler(&P, 4);
Empiler(&P, 5);
Affichage(P.Pil);*/
Programme * P;
P = lexer(argv[1]);
for(int i=0; i<P->taille; i++)
{
printf("TOKEN : %s \n", P->tokens[i]);
}
}

75
pile.c Normal file
View file

@ -0,0 +1,75 @@
#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;
}
void Empiler(struct Pile * P, int 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;
if (P->nbElements != 0)
{
res = P->Pil;
P->Pil= P->Pil->Suiv;
}
return res;
}
void Affichage(struct Cell * C)
{
if(C == NULL)
{
//printf("Rien a afficher");
}
else
{
printf("%d \n", 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;
}

31
pile.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef PILE_H
#define PILE_H
struct Cell;
struct Cell{
int Ent;
struct Cell * Suiv;
};
struct Pile{
int nbElements;
struct Cell * Pil;
};
typedef struct Programme {
char **tokens;
int taille;
} Programme;
void Init_Pile(struct Pile * new);
void Empiler(struct Pile * P, int i);
struct Cell * Depiler(struct Pile * P);
void Affichage(struct Cell * C);
int numberOfDelimiters(char* string);
Programme* lexer(char* chaine);
#endif