Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <time.h>
- #define ANSWER_AMOUNT 4
- #define BUFOR 256
- ///LIST OF QUESTIONS
- typedef struct node {
- int questionNumber;
- int correctAnswer;
- char answers[ANSWER_AMOUNT][BUFOR];
- char questionText[BUFOR];
- struct node * next;
- } question;
- void push(question * head, int questionNumber, char answers[ANSWER_AMOUNT][BUFOR], char questionText[BUFOR], int correctAnswer) {
- question * current = head;
- while (current->next != NULL) {
- current = current->next;
- }
- current->next = malloc(sizeof(question));
- current->next->questionNumber = questionNumber;
- current->next->correctAnswer = correctAnswer;
- strcpy(current->next->questionText, questionText);
- int i;
- for (i = 0; i < ANSWER_AMOUNT; i++)
- strcpy(current->next->answers[i], answers[i]);
- current->next->next = NULL;
- }
- void askQuestions(question * head, int questionsAmount, int shuffledQuestionsArr[], int * correctAnswers) {
- int i, j, answer;
- question * current = head;
- for (j = 0; j<questionsAmount; j++) {
- current = head;
- current = current->next;
- while (current->questionNumber != shuffledQuestionsArr[j]) {
- current = current->next;
- }
- printf("%s", current->questionText);
- for (i = 0; i < ANSWER_AMOUNT; i++)
- printf("%d: %s", i, current->answers[i]);
- printf("twoja odpowiedz: ");
- scanf("%d", &answer);
- if (answer == current->correctAnswer) (*correctAnswers)++;
- printf("\n");
- }
- }
- ///FILE
- FILE *Fopen(const char *filename, const char *mode)
- {
- FILE *fp;
- if ((fp = fopen(filename, mode)) == NULL) {
- printf("Nie mozna otworzyc pliku: %s", filename);
- strerror("fopen");
- exit(EXIT_FAILURE);
- }
- return fp;
- }
- ///SHUFFLING / RANDOM
- void fillArray(int array[], int questionNumber) {
- for (int i = 0; i < questionNumber; i++) { // fill array
- array[i] = i;
- }
- }
- void shuffleArray(int array[], int questionNumber) {
- for (int i = 0; i < questionNumber; i++) { // shuffle array
- int temp = array[i];
- int randomIndex = rand() % questionNumber;
- array[i] = array[randomIndex];
- array[randomIndex] = temp;
- }
- }
- ///MAIN
- int main(int argc, char**argv)
- {
- //variables
- FILE *input, *destination;
- char databaseName[BUFOR];
- int questionsAmount = 0,
- questionNumber = 0,
- currentLine = 0,
- correctAnswers = 0,
- switchesAmount = 0;
- char *nick = NULL;
- question * head = NULL;
- char line[BUFOR];
- char answers[ANSWER_AMOUNT][BUFOR];
- char questionText[BUFOR];
- int correctAnswer;
- char anserwsFileName[BUFOR];
- //switches handling
- int opt;
- while ((opt = getopt(argc, argv, "o:n:q:")) != -1) {
- switch (opt) {
- case 'o':
- strcpy(databaseName, optarg);
- switchesAmount++;
- break;
- case 'n':
- nick = optarg;
- switchesAmount++;
- break;
- case 'q':
- questionsAmount = atoi(optarg);
- switchesAmount++;
- break;
- default:
- exit(EXIT_FAILURE);
- }
- }
- if (switchesAmount !=3) {
- printf("Zla ilość przełączników!");
- exit(EXIT_FAILURE);
- }
- //wczytanie pytan do listy
- input = Fopen(databaseName, "r");
- head = malloc(sizeof(question));
- if (head == NULL) {
- return 1;
- }
- while (fgets(line, sizeof(line), input)) {
- //printf("line %d, %s", currentLine, line);
- if (currentLine == 0) strcpy(questionText, line);
- else if (currentLine == 1) correctAnswer = atoi(line);
- else strcpy(answers[currentLine-2], line);
- if (currentLine == 5) {
- push(head, questionNumber, answers, questionText, correctAnswer);
- questionNumber++;
- currentLine = -1;
- }
- currentLine++;
- }
- fclose(input);
- //losowanie pytan
- int *shuffledQuestionsArr = malloc(questionNumber * sizeof(int));
- fillArray(shuffledQuestionsArr, questionNumber);
- shuffleArray(shuffledQuestionsArr, questionNumber);
- //asking questions
- if (questionsAmount>questionNumber) questionsAmount=questionNumber;
- askQuestions(head, questionsAmount, shuffledQuestionsArr, &correctAnswers);
- //writing to file
- time_t t; //reading time
- time(&t);
- strcpy(anserwsFileName, "wyniki-");
- strcat(anserwsFileName, databaseName);
- destination = Fopen(anserwsFileName, "a");
- fprintf(destination, "%s - %d/%d, wykonano: %s\n", nick, correctAnswers, questionsAmount, ctime(&t));
- fclose(destination);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement