index — ti25-glauchau-code @ 23c14956042998dc03f6c03c44d76fd9b2c932f6

Meine Lösungen (oder auch nicht) für die Programmieraufgaben in der TI25 an der Staatlichen Studienakademie Glauchau

discord_programmieraufgaben/2025-10-17_2/quiz.cpp (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <cctype> 
#include <random>

using namespace std;

vector<string> split(string s, const string& delimiter) {
    vector<string> tokens;
    size_t pos = 0;
    while ((pos = s.find(delimiter)) != string::npos) {
        tokens.push_back(s.substr(0, pos));
        s.erase(0, pos + delimiter.length());
    }
    tokens.push_back(s);
    return tokens;
}

void string_tolower(string& str) {
for (char& chr : str) {
    chr = tolower(chr);
  }
}

string read_file(const string& filename) {
    ifstream file(filename);
    if (!file.is_open()) {
        cerr << "Error: Could not open " << filename << endl;
        return "";
    }
    string line, content;
    while (getline(file, line)) content += line + "\n";
    return content;
}

vector<vector<string>> create_question_array(const string& fileText) {
    vector<vector<string>> question_array;
    vector<string> lines = split(fileText, "\n");
    for (string& line : lines) {
        if (!line.empty()) question_array.push_back(split(line, ";"));
    }
    return question_array;
}

vector<vector<string>> get_questions(const string& filename) {
    string fileText = read_file(filename);
    vector<vector<string>> questionArray = create_question_array(fileText);

    return questionArray;
}

void ask_questions(vector<vector<string>>& question_array) {
    unsigned int score= 0, index = 1;

    for (vector<string>& question : question_array) {
        string answer;
        cout << index << ".  ";
        cout << question[0] << endl << "    ";
        getline(cin, answer);
        
        string_tolower(answer);
        string_tolower(question[1]);
        if (answer == question[1]) {
            score++;
            cout << "    Richtig! ";
        } else {
            cout << "    Falsch!  ";
        }
        cout << "[" << score << "/" << question_array.size() << "]\n\n";

        index++;
    }
}

int random_number(int max) {

    // Define range
    int min = 0;

    // Initialize a random number generator
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> distrib(min, max);

    // Generate random number in the range [min, max]
    int randomValue = distrib(gen);

    return randomValue;
}

void randomize_questions(vector<vector<string>>& question_array) {
    for (vector<string>& question : question_array) {
        swap(question, question_array[random_number(question_array.size()-1)]);
    }
}

int main() {
    cout << "Willkommen im Knusprigen Quiz" << endl;
    vector<vector<string>> question_array = get_questions("fragen.txt");
    randomize_questions(question_array);
    ask_questions(question_array);
    return 0;
}