Blame view

main.c 3.71 KB
a89bb625   bjeanlou   Update 8 withHash
1
2
3
4
5
  #include "dico.h"
  
  
  
  
ccb47d03   bjeanlou   menu quit ok
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  
  
  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);
a89bb625   bjeanlou   Update 8 withHash
30
    }
a89bb625   bjeanlou   Update 8 withHash
31
    
ccb47d03   bjeanlou   menu quit ok
32
33
34
35
36
37
38
39
40
41
42
43
44
    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;
b2638a8a   bjeanlou   création rapport
45
46
    if(fileIn==stdin)
      printf("Entrez vos mots (ctrl-D pour terminer le chargement):\n");
2c6f0266   bjeanlou   load and print OK
47
    loadfrom_file(monDico,fileIn);
2c6f0266   bjeanlou   load and print OK
48
    fclose(fileIn);
2c6f0266   bjeanlou   load and print OK
49
  
ccb47d03   bjeanlou   menu quit ok
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    //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");
b2638a8a   bjeanlou   création rapport
68
69
70
  
      //saisie du choix
      scanf("%2s",word);
ccb47d03   bjeanlou   menu quit ok
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
      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");
b2638a8a   bjeanlou   création rapport
86
87
        if(fileIn==stdin)
  	printf("Entrez vos mots (ctrl-D pour terminer le chargement):\n");
ccb47d03   bjeanlou   menu quit ok
88
        loadfrom_file(monDico,fileIn);
b2638a8a   bjeanlou   création rapport
89
90
        if(fileIn!=stdin)
  	fclose(fileIn);
ccb47d03   bjeanlou   menu quit ok
91
92
93
94
95
96
97
98
99
        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);
b2638a8a   bjeanlou   création rapport
100
101
        if(fileIn!=stdin)
  	fclose(fileIn);
ccb47d03   bjeanlou   menu quit ok
102
103
104
105
106
        break;}
        
        
      case 5: {//faire des tests
        fileIn=getFile("r");
b2638a8a   bjeanlou   création rapport
107
108
        if(fileIn==stdin)
  	printf("Entrez vos mots (ctrl-D pour terminer les tests):\n");
ccb47d03   bjeanlou   menu quit ok
109
        test_words(monDico,fileIn,fileOut);
b2638a8a   bjeanlou   création rapport
110
111
        if(fileIn!=stdin)
  	fclose(fileIn);
ccb47d03   bjeanlou   menu quit ok
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        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");
a89bb625   bjeanlou   Update 8 withHash
141
    return EXIT_SUCCESS;
8c671959   bjeanlou   No more seg fault
142
  }