diff --git a/dico.c b/dico.c index 698a257..3ebb1a4 100644 --- a/dico.c +++ b/dico.c @@ -6,13 +6,13 @@ void make_empty_dico(dico d){ } void delete_dico(dico d){ for(int i=0;i +#include "functions.h" + +int saisie(int min,int max){ + int n; + do{scanf("%d",&n); + }while(nmax); + return n; +} + +int ctoi(char c){ + if(c>'9' || c<'0') + return -1; + return c-'0'; +} + +void clear(){ +#ifdef WIN32 + system("cls"); +#else + #ifdef UNIX + system("clear"); + #else + for(int i=0;i<30;i++)printf("\n"); + #endif +#endif +} + +void pause(){ + getchar(); + printf("tapez la touche entree"); + fflush(stdout); + while(getchar()!='\n'); +} + + + +//stringLIFO +stringLIFO init_stringLIFO(){return NULL;} + +int is_emptyLIFO(stringLIFO list){return list==NULL;} + +void delete_LIFO(stringLIFO*list){ + while(!is_emptyLIFO(*list)) + free(pop(list)); +} + +string top(stringLIFO list){return is_emptyLIFO(list)?NULL:list->val;} + +void push(stringLIFO *list,string s){ + cell*c=malloc(sizeof(cell)); + c->val=calloc((strlen(s)+1),sizeof(char)); + strcpy(c->val,s); + c->next=*list; + *list=c; +} + +string pop(stringLIFO*list){ + string ret=NULL; + if(!is_emptyLIFO(*list)){ + cell*c=(*list)->next; + ret=(*list)->val; + free(*list); + *list=c; + } + return ret; +} diff --git a/functions.h b/functions.h new file mode 100644 index 0000000..0923f15 --- /dev/null +++ b/functions.h @@ -0,0 +1,23 @@ +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +#include +#include + +int saisie(int min,int max); +int ctoi(char); +void pause(); +void clear(); + +typedef char* string; +typedef struct _cell cell,*stringLIFO; +struct _cell{string val;cell* next;}; + +stringLIFO init_stringLIFO(); +int is_emptyLIFO(stringLIFO); +void push(stringLIFO*,string); +string pop(stringLIFO*); +string top(stringLIFO); +void delete_LIFO(stringLIFO*); + +#endif diff --git a/main.c b/main.c index 062b677..69ce860 100644 --- a/main.c +++ b/main.c @@ -3,26 +3,130 @@ -int main(int argc, char* argv[]){ - if(argc<2){ - printf("not enough arguments\n"); - return EXIT_FAILURE; - } - FILE*fileIn=fopen(argv[1],"r"); - if(fileIn==NULL){ - printf("wrong arguments\n"); - return EXIT_FAILURE; + + +FILE* getFile(string mode){ + if(strcmp(mode,"r"))//mode!"=r", gestion de l'affichage suivant si mode= "r" ou "w" + printf("Tapez un nom du fichier avec son extension pour diriger la sortie des tests vers ce fichier.\n"); + else printf("Tapez \"terminal\" pour entrer des mots depuis le terminal.\nTapez le nom du fichier avec son extension pour charger des mots depuis ce fichier\n"); + FILE*file=NULL; + char fileName[30]={0}; + + //get the fileName + scanf("%30s",fileName); + if(!strcmp(fileName,"terminal")){ + if(!strcmp(mode,"r")) + file=stdin;} + else file=fopen(fileName,mode); + + //while there is a problem about it + while(file==NULL){ + printf("desole, impossible d'ouvrir ce fichier.\nVeuillez essayer un autre fichier"); + scanf("%30s",fileName); + if(!strcmp(fileName,"terminal")){ + if(!strcmp(mode,"r")) + file=stdin;} + else file=fopen(fileName,mode); } - FILE*fileOut=fopen("out.txt","w"); - printf("bienvenue dans le dictonnaire\n\n"); - dico monDico; - make_empty_dico(monDico); + return file; +} + + + + + +void menu(dico monDico){ + int choice=0; + char word[2]={0}; + //initialisation + printf("Pour commencer, veuillez charger un dictionnaire.\n"); + FILE*fileIn=getFile("r"),*fileOut=stdout; loadfrom_file(monDico,fileIn); - printto_file(monDico,stdout); - delete_dico(monDico); fclose(fileIn); - fclose(fileOut); + //boucle + while(1){ + clear(); + //menu + printf("La sortie est branchee sur %s.\n",fileOut==stdout?"le terminal":"un fichier"); + printf("Que voulez-vous faire ?\n"); + //end programme + printf("0) Fermer le programme.\n\n"); + //just about the dictionnary + printf("1) Lire le dictionnaire.\n"); + printf("2) Rajouter des mots au dictionnaire\n"); + printf("3) Effacer le dictionnaire\n"); + printf("4) Remplacer le dictionnaire actuel par un autre dictionnaire\n\n"); + //word correction + printf("5) Tester si un mot, un ensemble de mot, une phrase ou un paragraphe appartient au dictionnaire.\n\n"); + //out stream + printf("6) Définir un fichier de sortie.\n"); + printf("7) Définir la sortie sur le terminal.\n"); + + scanf("%1s",word); + choice=ctoi(*word); + + switch(choice){ + case 0: {//sortie du programme + if(fileOut!=stdout) + fclose(fileOut); + return;} + case 1: {//Lire le dictionnaire + printto_file(monDico,fileOut); + printf("dictionnaire imprimme\n"); + break;} + + //manip sur le dictionnaire + case 2: {//ajout + fileIn=getFile("r"); + loadfrom_file(monDico,fileIn); + fclose(fileIn); + break;} + case 3: {//effacer + delete_dico(monDico); + printf("Le dictionnaire est maintenant vide.\n"); + break;} + case 4: {//remplacer + delete_dico(monDico); + fileIn=getFile("r"); + loadfrom_file(monDico,fileIn); + fclose(fileIn); + break;} + + + case 5: {//faire des tests + fileIn=getFile("r"); + test_words(monDico,fileIn,fileOut); + fclose(fileIn); + break;} + + //définir la sortie des tests et de "Lire" le dictionnaire + case 6: { + if(fileOut!=stdout) + fclose(fileOut); + fileOut=getFile("w"); + break;} + case 7: { + if(fileOut!=stdout) + fclose(fileOut); + fileOut=stdout; + break;} + //si le choix n'est pas parmi les précédents + default: {break;} + } + choice=0; + pause(); + }//end of while +} + + +int main(void){ + printf("Bienvenue dans le dictonnaire !\n\n"); + dico monDico; + make_empty_dico(monDico); + menu(monDico); + delete_dico(monDico); + printf("Le dictionnaire vous dit aurevoir.\n\n\n"); return EXIT_SUCCESS; } diff --git a/makefile b/makefile index 3005d75..f0f62a9 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ #makefile pour la branche withHash #nom de l'executeble -EXEC = dico.run +EXEC = dico.exec #nom du compilateur CC = gcc #warnings utilisés diff --git a/treeh.c b/treeh.c index d0c8bb2..fae6b94 100644 --- a/treeh.c +++ b/treeh.c @@ -21,11 +21,11 @@ node* make_node(char l,byte end){ n->next[i]=make_empty_tree(); return n; } -void delete_tree(tree t){ - if(is_empty(t))return; +void delete_tree(tree *t){ + if(is_empty(*t))return; for(int i=0;inext[i]); - free(t); + delete_tree(&((*t)->next[i])); + free(*t); } diff --git a/treeh.h b/treeh.h index fbd2fa1..c07a15f 100644 --- a/treeh.h +++ b/treeh.h @@ -31,7 +31,7 @@ struct _node{ tree make_empty_tree();//create a null node* node* make_empty_node();//malloc a node and initialize it node* make_node(char,byte);//id -void delete_tree(tree);//free(tree) and delete tree on all t->next +void delete_tree(tree*);//free(tree) and delete tree on all t->next bool is_empty(const tree);//==NULL bool is_followed(const tree);//if true tree has following letters -- libgit2 0.21.2