Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <set>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- #include <unordered_map>
- #define QUESTION_LENGTH 1024
- #define RADNOM_QUESTIONS_COUNT 7
- #define MAX_QUESTIONS_COUNT_PER_TOPIC 10
- #define QUESTION_ANSWERS_DIVIDER 'd'
- #define TOPICS_COUNT 3
- #define MAX_FINAL_TEST_QUESTIONS_COUNT 5
- #define EXCELLENT_MARK_IN_PERCENTAGE 90
- #define GOOD_MARK_IN_PERCENTAGE 80
- #define SATISFIED_MARK_IN_PERCENTAGE 60
- #define BAD_MARK_IN_PERCENTAGE 50
- using namespace std;
- struct QuestionAndAnswers {
- int number;
- int topic_index;
- string question;
- string answers;
- };
- struct FuckedUpQuestion {
- int number;
- string question;
- };
- void train_by_selected_topic() { // todo
- cout << "train_by_selected_topic";
- }
- int generate_random_questions(
- const QuestionAndAnswers all_questions[MAX_QUESTIONS_COUNT_PER_TOPIC],
- QuestionAndAnswers random_questions[RADNOM_QUESTIONS_COUNT]
- ) {
- srand(time(0));
- set<int> seen_question;
- int curr = 0;
- while(curr < RADNOM_QUESTIONS_COUNT) {
- int random_question_index = 0 + (rand() % MAX_QUESTIONS_COUNT_PER_TOPIC);
- cout << "curr is " << curr;
- if(seen_question.count(random_question_index) <= 0) {
- cout << "write index " << random_question_index << endl;
- seen_question.insert(random_question_index);
- random_questions[curr++] = all_questions[random_question_index];
- }
- }
- }
- vector<string> split_string(const string& s, char delimiter) {
- vector<string> tokens;
- size_t start = 0, end = 0;
- while ((end = s.find(delimiter, start)) != string::npos) {
- tokens.push_back(s.substr(start, end - start));
- start = end + 1;
- }
- tokens.push_back(s.substr(start));
- return tokens;
- }
- string generate_formatted_answer(string answer) {
- vector<string> answers = split_string(answer, QUESTION_ANSWERS_DIVIDER);
- string formatted_answer = "";
- if(answers.size() > 0) {
- for (const string& answer : answers) {
- formatted_answer = formatted_answer + "\n" + answer;
- }
- } else {
- formatted_answer = answer;
- }
- return formatted_answer;
- }
- unordered_map<string, int> topic_title_to_index_map;
- void generate_quesitons_and_answers_by_topic(string file_name, string topic, QuestionAndAnswers all_questions[MAX_QUESTIONS_COUNT_PER_TOPIC]) {
- // todo move later
- ifstream file(file_name);
- string q_number, q, a, divider;
- int question_index = 0;
- while(getline(file, q_number) && getline(file, q) && getline(file, a) && getline(file, divider)) {
- QuestionAndAnswers question;
- question.number = stoi(q_number);
- question.question = q;
- question.topic_index = topic_title_to_index_map[topic];
- question.answers = generate_formatted_answer(a);
- all_questions[question_index++] = question;
- }
- }
- void generate_correct_answers_by_topic(string file_name, int correct_answers[MAX_QUESTIONS_COUNT_PER_TOPIC]) {
- ifstream file(file_name);
- string answer;
- int ans_index = 0;
- while(getline(file, answer)) {
- correct_answers[ans_index++] = stoi(answer, nullptr, 10);
- }
- for(int i = 0; i < MAX_QUESTIONS_COUNT_PER_TOPIC; i++) {
- cout << "Ans for " << i + 1 << " is " << correct_answers[i] << endl;
- }
- }
- void print_errors_after_quiz_if_needed(int size, FuckedUpQuestion *fucked_up_questions) {
- if(size > 0) {
- cout << "Count of fucked up questions - " << size << endl;
- for(int i = 0; i < size; i++) {
- FuckedUpQuestion question = fucked_up_questions[i];
- cout << "Q" << question.number << ": " << question.question << endl;
- }
- } else {
- cout << "You have answered all the questions correctly! Congrats!!!";
- }
- }
- int calculate_mark(int fucked_questions_size, int all_questions_size) {
- int mark = -1;
- cout << "fucked up " << fucked_questions_size << " all " << all_questions_size;
- double in_percentage = (double(all_questions_size - fucked_questions_size) / double(all_questions_size)) * 100;
- cout << endl << "in percentage " << in_percentage << endl;
- if(in_percentage >= EXCELLENT_MARK_IN_PERCENTAGE) {
- mark = 5;
- } else {
- if(in_percentage >= GOOD_MARK_IN_PERCENTAGE) {
- mark = 4;
- } else {
- if(in_percentage >= SATISFIED_MARK_IN_PERCENTAGE) {
- mark = 3;
- } else {
- mark = 2;
- }
- }
- }
- return mark;
- }
- void test_by_selected_topic() { // todo
- cout << endl << "------Choose the topic------" << endl;
- cout << endl << "Ok, topic is chosen" << endl;
- int correct_answers[MAX_QUESTIONS_COUNT_PER_TOPIC];
- generate_correct_answers_by_topic("q_and_a/correct_answers_1.txt" ,correct_answers);
- QuestionAndAnswers all_questions[MAX_QUESTIONS_COUNT_PER_TOPIC];
- generate_quesitons_and_answers_by_topic("q_and_a/questions_1.txt", "T1", all_questions);
- QuestionAndAnswers random_questions[RADNOM_QUESTIONS_COUNT];
- generate_random_questions(all_questions, random_questions);
- FuckedUpQuestion *fucked_up_questions = NULL;
- int fuckued_up_question_index = 0;
- for(int i = 0; i < RADNOM_QUESTIONS_COUNT; i++) {
- QuestionAndAnswers question = random_questions[i];
- int correct_anwer_to_the_question = correct_answers[question.number - 1];
- int answer = -1;
- while(true) {
- cout << endl << endl << "Q" << question.number << ": " << question.question << "\n " << question.answers << ": ";
- cin >> answer;
- if(answer > 0 && answer < 5) break;
- }
- if(correct_anwer_to_the_question != answer) {
- fucked_up_questions = (FuckedUpQuestion*)realloc(fucked_up_questions, sizeof(FuckedUpQuestion) * (fuckued_up_question_index + 1));
- FuckedUpQuestion fucked_up_question;
- fucked_up_question.number = question.number;
- fucked_up_question.question = question.question;
- fucked_up_questions[fuckued_up_question_index++] = fucked_up_question;
- }
- }
- print_errors_after_quiz_if_needed(fuckued_up_question_index, fucked_up_questions);
- cout << endl << "Your mark is " << calculate_mark(fuckued_up_question_index, RADNOM_QUESTIONS_COUNT);
- }
- void pass_final_test() {
- srand(time(0));
- cout << "pass_final_test";
- topic_title_to_index_map["T1"] = 0;
- topic_title_to_index_map["T2"] = 1;
- topic_title_to_index_map["T3"] = 2;
- unordered_map<int, string> topic_index_to_title_map;
- topic_index_to_title_map[0] = "T1";
- topic_index_to_title_map[1] = "T2";
- topic_index_to_title_map[2] = "T3";
- QuestionAndAnswers topic_to_question_and_answers[TOPICS_COUNT][MAX_QUESTIONS_COUNT_PER_TOPIC];
- int topic_to_question_answer[TOPICS_COUNT][MAX_QUESTIONS_COUNT_PER_TOPIC];
- string topic_to_questions[TOPICS_COUNT] = {"q_and_a/questions_1.txt", "q_and_a/questions_2.txt", "q_and_a/questions_3.txt"};
- string topic_to_answers[TOPICS_COUNT] = {"q_and_a/correct_answers_1.txt", "q_and_a/correct_answers_2.txt", "q_and_a/correct_answers_3.txt"};
- for(int i = 0; i < TOPICS_COUNT; i++) {
- string questions_file_name = topic_to_questions[i];
- string answers_file_name = topic_to_answers[i];
- generate_quesitons_and_answers_by_topic(questions_file_name, topic_index_to_title_map[i], topic_to_question_and_answers[i]);
- generate_correct_answers_by_topic(answers_file_name, topic_to_question_answer[i]);
- }
- for(int i = 0; i < TOPICS_COUNT; i++) {
- for(int j = 0; j < MAX_QUESTIONS_COUNT_PER_TOPIC; j++) {
- cout << "T" << (i + 1) << "-" << (j + 1) << ": " << topic_to_question_answer[i][j] << endl;
- }
- }
- // for(int i = 0; i < TOPICS_COUNT; i++) {
- // cout << "---------Topic--------- " << i + 1 << endl;
- // for(int j = 0; j < MAX_QUESTIONS_COUNT_PER_TOPIC; j++) {
- // QuestionAndAnswers question = topic_to_question_and_answers[i][j];
- // int correct_answer = topic_to_question_answer[i][question.number - 1];
- // cout << "Q" << question.number << ": " << question.question << "-" << correct_answer << endl;
- // }
- // }
- int curr = 0;
- int last_topic_index = -1;
- set<string> seen;
- QuestionAndAnswers random_questions[MAX_FINAL_TEST_QUESTIONS_COUNT];
- while(curr < MAX_FINAL_TEST_QUESTIONS_COUNT) {
- int rand_topic_index = 0 + (rand() % TOPICS_COUNT);
- if(last_topic_index == rand_topic_index) continue;
- int rand_question_by_topic_index = 0 + (rand() % MAX_QUESTIONS_COUNT_PER_TOPIC);
- string question_id = topic_to_questions[rand_topic_index] + ":" + to_string(rand_question_by_topic_index);
- if(seen.count(question_id) <= 0) {
- seen.insert(question_id);
- random_questions[curr++] = topic_to_question_and_answers[rand_topic_index][rand_question_by_topic_index];
- last_topic_index = rand_topic_index;
- }
- }
- // cout << "-----Random questions-----";
- // for(int i = 0; i < MAX_FINAL_TEST_QUESTIONS_COUNT; i++) {
- // QuestionAndAnswers question = random_questions[i];
- // int correct_answer = topic_to_question_answer[question.topic_index][question.number - 1];
- // }
- FuckedUpQuestion *fucked_up_questions = NULL;
- int fuckued_up_question_index = 0;
- for(int i = 0; i < MAX_FINAL_TEST_QUESTIONS_COUNT; i++) {
- QuestionAndAnswers question = random_questions[i];
- int correct_anwer_to_the_question = topic_to_question_answer[question.topic_index][question.number - 1];
- int answer = -1;
- while(true) {
- cout << endl << endl << "Q" << question.number << ": " << question.question << "\n " << question.answers << ": ";
- cin >> answer;
- if(answer > 0 && answer < 5) break;
- }
- if(correct_anwer_to_the_question != answer) {
- fucked_up_questions = (FuckedUpQuestion*)realloc(fucked_up_questions, sizeof(FuckedUpQuestion) * (fuckued_up_question_index + 1));
- FuckedUpQuestion fucked_up_question;
- fucked_up_question.number = question.number;
- fucked_up_question.question = question.question;
- fucked_up_questions[fuckued_up_question_index++] = fucked_up_question;
- }
- }
- print_errors_after_quiz_if_needed(fuckued_up_question_index,fucked_up_questions);
- cout << endl << "Your mark is " << calculate_mark(fuckued_up_question_index, MAX_FINAL_TEST_QUESTIONS_COUNT);
- }
- void show_student_menu() {
- cout << endl << "------Student menu------" << endl;
- int menu_item = -1;
- while(true) {
- cout << "\n1 - Training by a topic"
- << "\n2 - Testing by a topic"
- << "\n3 - Final test"
- << "\n0 - Exit: ";
- cin >> menu_item;
- if(menu_item == 0) break;
- switch(menu_item) {
- case 1: train_by_selected_topic(); break;
- case 2: test_by_selected_topic(); break;
- case 3: pass_final_test(); break;
- default: cout << endl << "Menu item by " << menu_item << " cannot be found" << endl;
- }
- }
- }
- int main() {
- int menu_item = -1; // think of making a string and convert string to
- // test_by_selected_topic();
- pass_final_test();
- // while(true) {
- // cout << endl << "Login yourself. "
- // << "\n 1 - Teacher"
- // << "\n 2 - Student"
- // << "\n 0 - Exit: ";
- // cin >> menu_item;
- // if (menu_item == 0) break;
- // switch(menu_item) {
- // case 1: break; // todo login as a teacher
- // case 2: show_student_menu(); break; // todo login as a student
- // default: cout << endl << "Menu item by " << menu_item << " cannot be found" << endl; break;
- // }
- // }
- return 0;
- }
Add Comment
Please, Sign In to add comment