index — ti25-glauchau-code @ 37432d3138bbafd0d21f0dda8dfcaec9946b04d0

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
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <cctype> 
#include <random>

using namespace std;

// ============================= UTILS ================================= //

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);
  }
}

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;
}

// -------------------------- file utils -------------------------------- //

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;
}

void append_file(const string& filename, const string& text){
    ofstream of;

    of.open(filename, ios::app);
    if (!of)
        cout << "No such file found";
    else {
        of << text << endl;
        of.close();
    }
}

// ============================ LOGIC ============================ //

vector<vector<string>> create_question_vector(const string& fileText) {
    vector<vector<string>> questionVector;
    vector<string> lines = split(fileText, "\n"); //split on line break
    for (string& line : lines) {
        if (!line.empty()) questionVector.push_back(split(line, ";")); //create new vector per line and split on ;
    }
    return questionVector;
}

vector<vector<string>> get_questions(const string& fileName) {
    string fileText = read_file(fileName);
    vector<vector<string>> questionVector = create_question_vector(fileText); //create vector out of plain text

    return questionVector;
}

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

    for (vector<string>& question : questionVector) {
        string answer;
        cout << index << ".  ";
        cout << question[0] << endl << "    "; // ask question
        getline(cin, answer); // get answer
        
        string_tolower(answer);         // make case irellevant
        string_tolower(question[1]);
        if (answer == question[1]) {
            score++;
            cout << "    Richtig! ";
        } else {
            cout << "    Falsch!  ";
        }
        cout << "[" << score << "/" << questionVector.size() << "]\n\n"; // display score/highest reachable score

        index++; // count up in question number
    }

    return score;
}


void randomize_questions(vector<vector<string>>& questionVector) {
    for (vector<string>& question : questionVector) { // traverse vector and swap element with random index
        swap(question, questionVector[random_number(questionVector.size()-1)]);
    }
}

string enter_username() {
    string username;
    cout << "Gib deinen Nutzernamen an:\n";
    getline(cin, username);
    
    return username;
}

void write_score_list(const string& filename, const vector<vector<string>>& scoreList) {
    string text;
    for (const vector<string>& score : scoreList) {
        text += score[0] + ";" + score[1] + "\n";
    }

    ofstream file(filename);
    file << text;
    file.close();
}

vector<vector<string>> add_to_scoreboard(const string& username, const int& score) {
    // --------------------- read --------------------- //
    string filename = "scores.txt";
    vector<vector<string>> scoreList = get_questions(filename); // read existing scores from file
    vector<string> newScore = {username, to_string(score)}; // create new score from this run

    // if no file existed yet/it's empty
    if (scoreList.empty()) {
        cout << "Ignore previous error" << endl;
        scoreList.push_back(newScore);
        append_file("scores.txt", newScore[0] + ";" + newScore[1]);
        return scoreList;
    }
    
    //if file already existed
    for (vector<string>& score : scoreList) {
        if (newScore[0] == score[0]) { //if name already exists
            if (stoi(newScore[1]) > stoi(score[1])) { //and new score is higher
                score = newScore;
                write_score_list(filename, scoreList);
                return scoreList;
            }
        } 
    }
    
    scoreList.push_back(newScore);
    append_file(filename, newScore[0] + ";" + newScore[1]);

    return scoreList;
}

void show_leaderboard(const vector<vector<string>>& scoreList) {
    if (scoreList.empty()) {
        cout << "wtF";
    }
    cout << "#==================================#" << endl;
    for (vector<string> score : scoreList) {
        cout << score[0] << ": " << score[1] << endl;
    }
    cout << "#==================================#";
}

int main() {
    cout << "Willkommen im Knusprigen Quiz" << endl;

    vector<vector<string>> questionVector = get_questions("fragen.txt"); //load questions into vector
    randomize_questions(questionVector);
    int score = ask_questions(questionVector);
    string username = enter_username();
    vector<vector<string>> newScoreList = add_to_scoreboard(username, score);
    show_leaderboard(newScoreList);

    return 0;
}