Advertisement
yanni_yagami

Untitled

Nov 14th, 2020 (edited)
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. typedef struct question {
  7.     char question[256], suggestions[10][256];
  8.     int type;
  9. }question;
  10.  
  11. int typeToInt(char *str);
  12. void poserQuestion(question q);
  13. void inputStr(char *str);
  14. void recupReponse(int typeReponse, char *reponse);
  15. bool verifReponse(int typeReponse, char *reponse);
  16. void extractDataFromLine(question *q, char *ligne);
  17. void saveInFile(FILE *file, question q, char *reponse);
  18.  
  19. int main(void)
  20. {
  21.     char ligne[1000], reponse[1000];
  22.     question q;
  23.     FILE *sondageFile = fopen("questionnaire.txt", "r"),
  24.              *reponseFile = fopen("reponse.txt", "w");
  25.  
  26.     while(!feof(sondageFile))
  27.     {
  28.         char reponse[500];
  29.         fgets(ligne, 1000, sondageFile);
  30.  
  31.         extractDataFromLine(&q, ligne);
  32.  
  33.         poserQuestion(q);
  34.         recupReponse(q.type, reponse);
  35.         saveInFile(reponseFile, q, reponse);
  36.  
  37.         printf("-------------------------------------------------------------\n");
  38.     }
  39.  
  40.     fclose(sondageFile);
  41.     fclose(reponseFile);
  42.     return 0;
  43. }
  44.  
  45. int typeToInt(char *str)
  46. {
  47.     if(!strcmp(str, " 1")) return 1;
  48.     if(!strcmp(str, " oui-non")) return 2;
  49.     if(!strcmp(str, " choix-3")) return 3;
  50.     if(!strcmp(str, " choix-4")) return 4;
  51. }
  52.  
  53. void printType(int type)
  54. {
  55.     switch(type)
  56.     {
  57.         case 1:
  58.             printf("type : 1\n");
  59.         break;
  60.         case 2:
  61.             printf("type : oui ou non\n");
  62.         break;
  63.         case 3:
  64.             printf("type : choix 3\n");
  65.         break;
  66.         case 4:
  67.             printf("type : choix 4\n");
  68.         break;
  69.     }
  70. }
  71.  
  72. void poserQuestion(question q)
  73. {
  74.     int i;
  75.  
  76.     printf("%s\n", q.question);
  77.     printType(q.type);
  78.  
  79.     if(q.type != 1 && q.type != 2)
  80.         for(i = 0; q.suggestions[i][0] != '\0'; i++)
  81.         {
  82.             printf("%d.%s\n", i+1, q.suggestions[i]);
  83.         }
  84.     printf("\n");
  85. }
  86.  
  87. void inputStr(char *str)
  88. {
  89.     fgets(str, 256, stdin);
  90.  
  91.     if(str[strlen(str)-1] == '\n')
  92.         str[strlen(str)-1] = 0;
  93. }
  94.  
  95. void recupReponse(int typeReponse, char *reponse)
  96. {
  97.     do
  98.     {
  99.         printf("votre reponse : ");
  100.         inputStr(reponse);
  101.     } while (verifReponse(typeReponse, reponse));
  102. }
  103.  
  104. bool verifReponse(int typeReponse, char *reponse)
  105. {
  106.     if(typeReponse == 1 && strlen(reponse) < 2)
  107.     {
  108.         printf("veillez bien entrer quelque chose !\n");
  109.         return true;
  110.     }
  111.        
  112.     if(typeReponse == 2)
  113.         if(reponse[0] != 'o' && reponse[0] != 'O' && reponse[0] != 'n' && reponse[0] != 'N')
  114.         {
  115.             printf("veillez repondre par 'oui' ou 'non' !\n");
  116.             return true;
  117.         }
  118.  
  119.     if(typeReponse == 3 || typeReponse == 4)
  120.     {
  121.         int n = atol(reponse);
  122.         if(n == 0 && reponse[0] != '0')
  123.         {
  124.             printf("veillez bien entrer l'index de votre/vos reponse(s) !\n");
  125.             return true;
  126.         }
  127.     }
  128.  
  129.     return false;
  130. }
  131.  
  132. void extractDataFromLine(question *q, char *ligne)
  133. {
  134.     int i;
  135.     char *element;
  136.  
  137.     element = strtok(ligne, ",;");
  138.  
  139.     for(i = 0 ;element && strlen(element) > 2; i++)
  140.     {
  141.         if(i == 0) strcpy(q->question, element);
  142.         else if(i == 1) q->type = typeToInt(element);
  143.         else
  144.         {
  145.             strcpy(q->suggestions[i-2], element);
  146.             q->suggestions[i-1][0] = '\0';
  147.         }
  148.  
  149.         element = strtok(NULL, ",;");
  150.     }
  151. }
  152.  
  153. void saveInFile(FILE *file, question q, char *reponse)
  154. {
  155.     fprintf(file, "%s, ", q.question);
  156.  
  157.     if(q.type == 1) fprintf(file, "1, *;\n%s;\n", reponse);
  158.     else if(q.type == 2)
  159.     {
  160.         fprintf(file, "oui-non, *;\n");
  161.         if(reponse[0] == 'o' || reponse[0] == 'O')
  162.             fprintf(file, "oui;\n");
  163.         else
  164.             fprintf(file, "non;\n");
  165.     }
  166.     else
  167.     {
  168.         int i, n;
  169.         if(q.type == 3) fprintf(file, "choix-3");
  170.         else  fprintf(file, "choix-4");
  171.  
  172.         for(i = 0; strlen(q.suggestions[i]) ; i++)
  173.             fprintf(file, ", %s", q.suggestions[i]);
  174.         fprintf(file, ";\n");
  175.  
  176.         if(q.type == 3)
  177.         {
  178.             n = atol(reponse);
  179.             fprintf(file, "%d;\n", n);
  180.         }
  181.         else fprintf(file, "%s;\n", reponse);
  182.     }
  183.  
  184.  
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement