Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <getopt.h>
  5. #include <time.h>
  6.  
  7. #define ANSWER_AMOUNT 4
  8. #define BUFOR 256
  9.  
  10. ///LIST OF QUESTIONS
  11. typedef struct node {
  12. int questionNumber;
  13. int correctAnswer;
  14. char answers[ANSWER_AMOUNT][BUFOR];
  15. char questionText[BUFOR];
  16. struct node * next;
  17. } question;
  18.  
  19. void push(question * head, int questionNumber, char answers[ANSWER_AMOUNT][BUFOR], char questionText[BUFOR], int correctAnswer) {
  20. question * current = head;
  21. while (current->next != NULL) {
  22. current = current->next;
  23. }
  24.  
  25. current->next = malloc(sizeof(question));
  26. current->next->questionNumber = questionNumber;
  27. current->next->correctAnswer = correctAnswer;
  28. strcpy(current->next->questionText, questionText);
  29.  
  30. int i;
  31. for (i = 0; i < ANSWER_AMOUNT; i++)
  32. strcpy(current->next->answers[i], answers[i]);
  33.  
  34. current->next->next = NULL;
  35. }
  36.  
  37. void askQuestions(question * head, int questionsAmount, int shuffledQuestionsArr[], int * correctAnswers) {
  38. int i, j, answer;
  39. question * current = head;
  40.  
  41. for (j = 0; j<questionsAmount; j++) {
  42. current = head;
  43. current = current->next;
  44.  
  45.  
  46. while (current->questionNumber != shuffledQuestionsArr[j]) {
  47. current = current->next;
  48. }
  49.  
  50. printf("%s", current->questionText);
  51. for (i = 0; i < ANSWER_AMOUNT; i++)
  52. printf("%d: %s", i, current->answers[i]);
  53.  
  54. printf("twoja odpowiedz: ");
  55. scanf("%d", &answer);
  56. if (answer == current->correctAnswer) (*correctAnswers)++;
  57. printf("\n");
  58. }
  59. }
  60.  
  61. ///FILE
  62. FILE *Fopen(const char *filename, const char *mode)
  63. {
  64. FILE *fp;
  65.  
  66. if ((fp = fopen(filename, mode)) == NULL) {
  67. printf("Nie mozna otworzyc pliku: %s", filename);
  68. strerror("fopen");
  69. exit(EXIT_FAILURE);
  70. }
  71. return fp;
  72. }
  73.  
  74. ///SHUFFLING / RANDOM
  75. void fillArray(int array[], int questionNumber) {
  76. for (int i = 0; i < questionNumber; i++) { // fill array
  77. array[i] = i;
  78. }
  79. }
  80.  
  81. void shuffleArray(int array[], int questionNumber) {
  82. for (int i = 0; i < questionNumber; i++) { // shuffle array
  83. int temp = array[i];
  84. int randomIndex = rand() % questionNumber;
  85.  
  86. array[i] = array[randomIndex];
  87. array[randomIndex] = temp;
  88. }
  89. }
  90.  
  91. ///MAIN
  92. int main(int argc, char**argv)
  93. {
  94. //variables
  95. FILE *input, *destination;
  96. char databaseName[BUFOR];
  97. int questionsAmount = 0,
  98. questionNumber = 0,
  99. currentLine = 0,
  100. correctAnswers = 0,
  101. switchesAmount = 0;
  102. char *nick = NULL;
  103. question * head = NULL;
  104. char line[BUFOR];
  105. char answers[ANSWER_AMOUNT][BUFOR];
  106. char questionText[BUFOR];
  107. int correctAnswer;
  108. char anserwsFileName[BUFOR];
  109.  
  110. //switches handling
  111. int opt;
  112. while ((opt = getopt(argc, argv, "o:n:q:")) != -1) {
  113. switch (opt) {
  114. case 'o':
  115. strcpy(databaseName, optarg);
  116. switchesAmount++;
  117. break;
  118. case 'n':
  119. nick = optarg;
  120. switchesAmount++;
  121. break;
  122. case 'q':
  123. questionsAmount = atoi(optarg);
  124. switchesAmount++;
  125. break;
  126. default:
  127. exit(EXIT_FAILURE);
  128. }
  129. }
  130.  
  131. if (switchesAmount !=3) {
  132. printf("Zla ilość przełączników!");
  133. exit(EXIT_FAILURE);
  134. }
  135.  
  136. //wczytanie pytan do listy
  137. input = Fopen(databaseName, "r");
  138. head = malloc(sizeof(question));
  139. if (head == NULL) {
  140. return 1;
  141. }
  142.  
  143. while (fgets(line, sizeof(line), input)) {
  144. //printf("line %d, %s", currentLine, line);
  145.  
  146. if (currentLine == 0) strcpy(questionText, line);
  147. else if (currentLine == 1) correctAnswer = atoi(line);
  148. else strcpy(answers[currentLine-2], line);
  149.  
  150. if (currentLine == 5) {
  151. push(head, questionNumber, answers, questionText, correctAnswer);
  152. questionNumber++;
  153. currentLine = -1;
  154. }
  155.  
  156. currentLine++;
  157. }
  158. fclose(input);
  159.  
  160. //losowanie pytan
  161. int *shuffledQuestionsArr = malloc(questionNumber * sizeof(int));
  162. fillArray(shuffledQuestionsArr, questionNumber);
  163. shuffleArray(shuffledQuestionsArr, questionNumber);
  164.  
  165. //asking questions
  166. if (questionsAmount>questionNumber) questionsAmount=questionNumber;
  167. askQuestions(head, questionsAmount, shuffledQuestionsArr, &correctAnswers);
  168.  
  169. //writing to file
  170. time_t t; //reading time
  171. time(&t);
  172.  
  173. strcpy(anserwsFileName, "wyniki-");
  174. strcat(anserwsFileName, databaseName);
  175. destination = Fopen(anserwsFileName, "a");
  176. fprintf(destination, "%s - %d/%d, wykonano: %s\n", nick, correctAnswers, questionsAmount, ctime(&t));
  177. fclose(destination);
  178.  
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement