diff options
| author | Filip Wandzio <contact@philw.dev> | 2026-03-01 17:45:00 +0100 |
|---|---|---|
| committer | Filip Wandzio <contact@philw.dev> | 2026-03-01 17:45:00 +0100 |
| commit | 9e9c1b21569faeabd33716e4153a881e2eed7134 (patch) | |
| tree | f3a7ad21aed4b1c4f51c3ee308ffef88430deafc /src/main.c | |
| parent | 57b077a4788b7fb5ed6add1df4ba3f15c9e6349b (diff) | |
| download | ysnp-9e9c1b21569faeabd33716e4153a881e2eed7134.tar.gz ysnp-9e9c1b21569faeabd33716e4153a881e2eed7134.zip | |
Signed-off-by: Filip Wandzio <contact@philw.dev>
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 94 |
1 files changed, 13 insertions, 81 deletions
| @@ -1,83 +1,15 @@ | |||
| 1 | #include <stdio.h> | 1 | #include "../include/quiz.h" // QuizSession and functions |
| 2 | #include <stdlib.h> | ||
| 3 | #include <time.h> | ||
| 4 | #include <sys/stat.h> | ||
| 5 | #include <sys/types.h> | ||
| 6 | |||
| 7 | #include "questions.h" | ||
| 8 | #include "utils.h" | ||
| 9 | |||
| 10 | int main(void) { | ||
| 11 | srand((unsigned)time(NULL)); | ||
| 12 | |||
| 13 | // utwórz katalog data/ jeśli nie istnieje | ||
| 14 | struct stat st = {0}; | ||
| 15 | if (stat("data", &st) == -1) { | ||
| 16 | #ifdef _WIN32 | ||
| 17 | _mkdir("data"); | ||
| 18 | #else | ||
| 19 | mkdir("data", 0755); | ||
| 20 | #endif | ||
| 21 | } | ||
| 22 | |||
| 23 | QuestionSet qs; | ||
| 24 | if (load_questions("data/questions.txt", &qs) != EXIT_SUCCESS) { | ||
| 25 | fprintf(stderr, "Error: cannot load questions file\n"); | ||
| 26 | return EXIT_FAILURE; | ||
| 27 | } | ||
| 28 | |||
| 29 | FILE *csv = fopen("data/results.csv", "a"); | ||
| 30 | if (!csv) { | ||
| 31 | perror("CSV open"); | ||
| 32 | free_questions(&qs); | ||
| 33 | return EXIT_FAILURE; | ||
| 34 | } | ||
| 35 | |||
| 36 | int limit = 0; | ||
| 37 | printf("Time limit (sec, 0 = unlimited): "); | ||
| 38 | if (scanf("%d", &limit) != 1) limit = 0; | ||
| 39 | wait_enter(NULL); | ||
| 40 | |||
| 41 | size_t total = 0, correct = 0; | ||
| 42 | char cont = 'y'; | ||
| 43 | |||
| 44 | while (cont == 'y' || cont == 'Y') { | ||
| 45 | const char *g = qs.general[rand_index(qs.general_count)]; | ||
| 46 | const char *m = qs.major[rand_index(qs.major_count)]; | ||
| 47 | |||
| 48 | print_line(); | ||
| 49 | printf("GENERAL:\n%s\n\nMAJOR:\n%s\n", g, m); | ||
| 50 | print_line(); | ||
| 51 | 2 | ||
| 52 | wait_enter("Press ENTER to start..."); | 3 | #include <stdlib.h> |
| 53 | time_t start = time(NULL); | ||
| 54 | |||
| 55 | wait_enter("Press ENTER when done..."); | ||
| 56 | double duration = difftime(time(NULL), start); | ||
| 57 | |||
| 58 | if (limit > 0 && duration > limit) | ||
| 59 | printf("Time exceeded!\n"); | ||
| 60 | |||
| 61 | char ans = ask_yes_no("Correct? (y/n): "); | ||
| 62 | if (ans == 'y' || ans == 'Y') correct++; | ||
| 63 | total++; | ||
| 64 | |||
| 65 | char ts[32]; | ||
| 66 | now_str(ts, sizeof(ts)); | ||
| 67 | fprintf(csv, "\"%s\",\"%s\",\"%s\",%c,%.0f\n", ts, g, m, ans, duration); | ||
| 68 | |||
| 69 | printf("Score: %zu/%zu (%.1f%%)\n", | ||
| 70 | correct, total, | ||
| 71 | total ? (double)correct / total * 100 : 0.0); | ||
| 72 | |||
| 73 | cont = ask_yes_no("Next? (y/n): "); | ||
| 74 | } | ||
| 75 | |||
| 76 | printf("\nSession score: %.1f%%\n", | ||
| 77 | total ? (double)correct / total * 100 : 0.0); | ||
| 78 | |||
| 79 | fclose(csv); | ||
| 80 | free_questions(&qs); | ||
| 81 | 4 | ||
| 82 | return EXIT_SUCCESS; | 5 | int main(void) |
| 83 | } | 6 | { |
| 7 | QuizSession session; | ||
| 8 | if (!initialize_quiz_session(&session)) | ||
| 9 | return EXIT_FAILURE; | ||
| 10 | while (quiz_iteration(&session)) { | ||
| 11 | } | ||
| 12 | print_final_score(&session); | ||
| 13 | cleanup_session(&session); | ||
| 14 | return EXIT_SUCCESS; | ||
| 15 | } \ No newline at end of file | ||
