Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- typedef struct question {
- char question[256], suggestions[10][256];
- int type;
- }question;
- int typeToInt(char *str);
- void poserQuestion(question q);
- void inputStr(char *str);
- void recupReponse(int typeReponse, char *reponse);
- bool verifReponse(int typeReponse, char *reponse);
- void extractDataFromLine(question *q, char *ligne);
- void saveInFile(FILE *file, question q, char *reponse);
- int main(void)
- {
- char ligne[1000], reponse[1000];
- question q;
- FILE *sondageFile = fopen("questionnaire.txt", "r"),
- *reponseFile = fopen("reponse.txt", "w");
- while(!feof(sondageFile))
- {
- char reponse[500];
- fgets(ligne, 1000, sondageFile);
- extractDataFromLine(&q, ligne);
- poserQuestion(q);
- recupReponse(q.type, reponse);
- saveInFile(reponseFile, q, reponse);
- printf("-------------------------------------------------------------\n");
- }
- fclose(sondageFile);
- fclose(reponseFile);
- return 0;
- }
- int typeToInt(char *str)
- {
- if(!strcmp(str, " 1")) return 1;
- if(!strcmp(str, " oui-non")) return 2;
- if(!strcmp(str, " choix-3")) return 3;
- if(!strcmp(str, " choix-4")) return 4;
- }
- void printType(int type)
- {
- switch(type)
- {
- case 1:
- printf("type : 1\n");
- break;
- case 2:
- printf("type : oui ou non\n");
- break;
- case 3:
- printf("type : choix 3\n");
- break;
- case 4:
- printf("type : choix 4\n");
- break;
- }
- }
- void poserQuestion(question q)
- {
- int i;
- printf("%s\n", q.question);
- printType(q.type);
- if(q.type != 1 && q.type != 2)
- for(i = 0; q.suggestions[i][0] != '\0'; i++)
- {
- printf("%d.%s\n", i+1, q.suggestions[i]);
- }
- printf("\n");
- }
- void inputStr(char *str)
- {
- fgets(str, 256, stdin);
- if(str[strlen(str)-1] == '\n')
- str[strlen(str)-1] = 0;
- }
- void recupReponse(int typeReponse, char *reponse)
- {
- do
- {
- printf("votre reponse : ");
- inputStr(reponse);
- } while (verifReponse(typeReponse, reponse));
- }
- bool verifReponse(int typeReponse, char *reponse)
- {
- if(typeReponse == 1 && strlen(reponse) < 2)
- {
- printf("veillez bien entrer quelque chose !\n");
- return true;
- }
- if(typeReponse == 2)
- if(reponse[0] != 'o' && reponse[0] != 'O' && reponse[0] != 'n' && reponse[0] != 'N')
- {
- printf("veillez repondre par 'oui' ou 'non' !\n");
- return true;
- }
- if(typeReponse == 3 || typeReponse == 4)
- {
- int n = atol(reponse);
- if(n == 0 && reponse[0] != '0')
- {
- printf("veillez bien entrer l'index de votre/vos reponse(s) !\n");
- return true;
- }
- }
- return false;
- }
- void extractDataFromLine(question *q, char *ligne)
- {
- int i;
- char *element;
- element = strtok(ligne, ",;");
- for(i = 0 ;element && strlen(element) > 2; i++)
- {
- if(i == 0) strcpy(q->question, element);
- else if(i == 1) q->type = typeToInt(element);
- else
- {
- strcpy(q->suggestions[i-2], element);
- q->suggestions[i-1][0] = '\0';
- }
- element = strtok(NULL, ",;");
- }
- }
- void saveInFile(FILE *file, question q, char *reponse)
- {
- fprintf(file, "%s, ", q.question);
- if(q.type == 1) fprintf(file, "1, *;\n%s;\n", reponse);
- else if(q.type == 2)
- {
- fprintf(file, "oui-non, *;\n");
- if(reponse[0] == 'o' || reponse[0] == 'O')
- fprintf(file, "oui;\n");
- else
- fprintf(file, "non;\n");
- }
- else
- {
- int i, n;
- if(q.type == 3) fprintf(file, "choix-3");
- else fprintf(file, "choix-4");
- for(i = 0; strlen(q.suggestions[i]) ; i++)
- fprintf(file, ", %s", q.suggestions[i]);
- fprintf(file, ";\n");
- if(q.type == 3)
- {
- n = atol(reponse);
- fprintf(file, "%d;\n", n);
- }
- else fprintf(file, "%s;\n", reponse);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement