From 9e9c1b21569faeabd33716e4153a881e2eed7134 Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Sun, 1 Mar 2026 17:45:00 +0100 Subject: Separate quiz logic from main function fo dedicated module Signed-off-by: Filip Wandzio --- src/main.c | 94 +++++++++----------------------------------------------------- 1 file changed, 13 insertions(+), 81 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 33d4b39..f69d734 100644 --- a/src/main.c +++ b/src/main.c @@ -1,83 +1,15 @@ -#include -#include -#include -#include -#include - -#include "questions.h" -#include "utils.h" - -int main(void) { - srand((unsigned)time(NULL)); - - // utwórz katalog data/ jeśli nie istnieje - struct stat st = {0}; - if (stat("data", &st) == -1) { -#ifdef _WIN32 - _mkdir("data"); -#else - mkdir("data", 0755); -#endif - } - - QuestionSet qs; - if (load_questions("data/questions.txt", &qs) != EXIT_SUCCESS) { - fprintf(stderr, "Error: cannot load questions file\n"); - return EXIT_FAILURE; - } - - FILE *csv = fopen("data/results.csv", "a"); - if (!csv) { - perror("CSV open"); - free_questions(&qs); - return EXIT_FAILURE; - } - - int limit = 0; - printf("Time limit (sec, 0 = unlimited): "); - if (scanf("%d", &limit) != 1) limit = 0; - wait_enter(NULL); - - size_t total = 0, correct = 0; - char cont = 'y'; - - while (cont == 'y' || cont == 'Y') { - const char *g = qs.general[rand_index(qs.general_count)]; - const char *m = qs.major[rand_index(qs.major_count)]; - - print_line(); - printf("GENERAL:\n%s\n\nMAJOR:\n%s\n", g, m); - print_line(); +#include "../include/quiz.h" // QuizSession and functions - wait_enter("Press ENTER to start..."); - time_t start = time(NULL); - - wait_enter("Press ENTER when done..."); - double duration = difftime(time(NULL), start); - - if (limit > 0 && duration > limit) - printf("Time exceeded!\n"); - - char ans = ask_yes_no("Correct? (y/n): "); - if (ans == 'y' || ans == 'Y') correct++; - total++; - - char ts[32]; - now_str(ts, sizeof(ts)); - fprintf(csv, "\"%s\",\"%s\",\"%s\",%c,%.0f\n", ts, g, m, ans, duration); - - printf("Score: %zu/%zu (%.1f%%)\n", - correct, total, - total ? (double)correct / total * 100 : 0.0); - - cont = ask_yes_no("Next? (y/n): "); - } - - printf("\nSession score: %.1f%%\n", - total ? (double)correct / total * 100 : 0.0); - - fclose(csv); - free_questions(&qs); +#include - return EXIT_SUCCESS; -} +int main(void) +{ + QuizSession session; + if (!initialize_quiz_session(&session)) + return EXIT_FAILURE; + while (quiz_iteration(&session)) { + } + print_final_score(&session); + cleanup_session(&session); + return EXIT_SUCCESS; +} \ No newline at end of file -- cgit v1.2.3