Kostiggig

Untitled

Apr 12th, 2023
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <set>
  5. #include <vector>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <unordered_map>
  9.  
  10. #define QUESTION_LENGTH 1024
  11. #define RADNOM_QUESTIONS_COUNT 7
  12. #define MAX_QUESTIONS_COUNT_PER_TOPIC 10
  13. #define QUESTION_ANSWERS_DIVIDER 'd'
  14.  
  15. #define TOPICS_COUNT 3
  16. #define MAX_FINAL_TEST_QUESTIONS_COUNT 5
  17.  
  18. #define EXCELLENT_MARK_IN_PERCENTAGE 90
  19. #define GOOD_MARK_IN_PERCENTAGE 80
  20. #define SATISFIED_MARK_IN_PERCENTAGE 60
  21. #define BAD_MARK_IN_PERCENTAGE 50
  22.  
  23. using namespace std;
  24.  
  25. struct QuestionAndAnswers {
  26.     int number;
  27.     int topic_index;
  28.     string question;
  29.     string answers;
  30. };
  31.  
  32. struct FuckedUpQuestion {
  33.     int number;
  34.     string question;
  35. };
  36.  
  37. void train_by_selected_topic() { // todo
  38.     cout << "train_by_selected_topic";
  39. }
  40.  
  41. int generate_random_questions(
  42.     const QuestionAndAnswers all_questions[MAX_QUESTIONS_COUNT_PER_TOPIC],
  43.     QuestionAndAnswers random_questions[RADNOM_QUESTIONS_COUNT]
  44. ) {
  45.     srand(time(0));
  46.     set<int> seen_question;
  47.  
  48.     int curr = 0;
  49.  
  50.     while(curr < RADNOM_QUESTIONS_COUNT) {
  51.         int random_question_index = 0 + (rand() % MAX_QUESTIONS_COUNT_PER_TOPIC);
  52.         cout << "curr is " << curr;
  53.         if(seen_question.count(random_question_index) <= 0) {
  54.             cout << "write index " << random_question_index << endl;
  55.             seen_question.insert(random_question_index);
  56.             random_questions[curr++] = all_questions[random_question_index];
  57.         }
  58.     }
  59. }
  60.  
  61. vector<string> split_string(const string& s, char delimiter) {
  62.     vector<string> tokens;
  63.     size_t start = 0, end = 0;
  64.     while ((end = s.find(delimiter, start)) != string::npos) {
  65.         tokens.push_back(s.substr(start, end - start));
  66.         start = end + 1;
  67.     }
  68.     tokens.push_back(s.substr(start));
  69.     return tokens;
  70. }
  71.  
  72. string generate_formatted_answer(string answer) {
  73.     vector<string> answers = split_string(answer, QUESTION_ANSWERS_DIVIDER);
  74.         string formatted_answer = "";
  75.         if(answers.size() > 0) {
  76.             for (const string& answer : answers) {
  77.                 formatted_answer = formatted_answer + "\n" + answer;
  78.             }
  79.         } else {
  80.             formatted_answer = answer;
  81.         }
  82.  
  83.     return formatted_answer;
  84. }
  85.  
  86. unordered_map<string, int> topic_title_to_index_map;
  87.  
  88. void generate_quesitons_and_answers_by_topic(string file_name, string topic, QuestionAndAnswers all_questions[MAX_QUESTIONS_COUNT_PER_TOPIC]) {
  89.  
  90.     // todo move later
  91.    
  92.  
  93.     ifstream file(file_name);
  94.     string q_number, q, a, divider;
  95.  
  96.     int question_index = 0;
  97.     while(getline(file, q_number) && getline(file, q) && getline(file, a) && getline(file, divider)) {
  98.         QuestionAndAnswers question;
  99.         question.number = stoi(q_number);
  100.         question.question = q;
  101.         question.topic_index = topic_title_to_index_map[topic];
  102.         question.answers = generate_formatted_answer(a);
  103.  
  104.         all_questions[question_index++] = question;
  105.     }
  106. }
  107.  
  108. void generate_correct_answers_by_topic(string file_name, int correct_answers[MAX_QUESTIONS_COUNT_PER_TOPIC]) {
  109.     ifstream file(file_name);
  110.     string answer;
  111.     int ans_index = 0;
  112.     while(getline(file, answer)) {
  113.         correct_answers[ans_index++] = stoi(answer, nullptr, 10);
  114.     }
  115.  
  116.     for(int i = 0; i < MAX_QUESTIONS_COUNT_PER_TOPIC; i++) {
  117.         cout << "Ans for " << i + 1 << " is " << correct_answers[i] << endl;
  118.     }
  119. }
  120.  
  121. void print_errors_after_quiz_if_needed(int size, FuckedUpQuestion *fucked_up_questions) {
  122.     if(size > 0) {
  123.  
  124.         cout << "Count of fucked up questions - " << size << endl;
  125.         for(int i = 0; i < size; i++) {
  126.             FuckedUpQuestion question = fucked_up_questions[i];
  127.             cout << "Q" << question.number << ": " << question.question << endl;
  128.         }
  129.     } else {
  130.         cout << "You have answered all the questions correctly! Congrats!!!";
  131.     }
  132. }
  133.  
  134. int calculate_mark(int fucked_questions_size, int all_questions_size) {
  135.     int mark = -1;
  136.  
  137.     cout << "fucked up " << fucked_questions_size << " all " << all_questions_size;
  138.     double in_percentage = (double(all_questions_size - fucked_questions_size) / double(all_questions_size)) * 100;
  139.     cout << endl << "in percentage " << in_percentage << endl;
  140.     if(in_percentage >= EXCELLENT_MARK_IN_PERCENTAGE) {
  141.         mark = 5;
  142.     } else {
  143.         if(in_percentage >= GOOD_MARK_IN_PERCENTAGE) {
  144.             mark = 4;
  145.         } else {
  146.             if(in_percentage >= SATISFIED_MARK_IN_PERCENTAGE) {
  147.                 mark = 3;
  148.             } else {
  149.                 mark = 2;
  150.             }
  151.         }
  152.     }
  153.  
  154.     return mark;
  155. }
  156.  
  157. void test_by_selected_topic() { // todo
  158.     cout << endl << "------Choose the topic------" << endl;
  159.     cout << endl << "Ok, topic is chosen" << endl;
  160.  
  161.     int correct_answers[MAX_QUESTIONS_COUNT_PER_TOPIC];
  162.     generate_correct_answers_by_topic("q_and_a/correct_answers_1.txt" ,correct_answers);
  163.    
  164.     QuestionAndAnswers all_questions[MAX_QUESTIONS_COUNT_PER_TOPIC];
  165.     generate_quesitons_and_answers_by_topic("q_and_a/questions_1.txt", "T1", all_questions);
  166.  
  167.     QuestionAndAnswers random_questions[RADNOM_QUESTIONS_COUNT];
  168.     generate_random_questions(all_questions, random_questions);
  169.  
  170.  
  171.     FuckedUpQuestion *fucked_up_questions = NULL;
  172.     int fuckued_up_question_index = 0;
  173.  
  174.     for(int i = 0; i < RADNOM_QUESTIONS_COUNT; i++) {
  175.         QuestionAndAnswers question = random_questions[i];
  176.         int correct_anwer_to_the_question = correct_answers[question.number - 1];
  177.  
  178.         int answer = -1;
  179.         while(true) {
  180.             cout << endl << endl << "Q" << question.number << ": " << question.question << "\n " << question.answers << ": ";
  181.             cin >> answer;
  182.             if(answer > 0 && answer < 5) break;
  183.         }
  184.  
  185.         if(correct_anwer_to_the_question != answer) {
  186.             fucked_up_questions = (FuckedUpQuestion*)realloc(fucked_up_questions, sizeof(FuckedUpQuestion) * (fuckued_up_question_index + 1));
  187.            
  188.             FuckedUpQuestion fucked_up_question;
  189.             fucked_up_question.number = question.number;
  190.             fucked_up_question.question = question.question;
  191.  
  192.             fucked_up_questions[fuckued_up_question_index++] = fucked_up_question;
  193.         }
  194.     }    
  195.  
  196.     print_errors_after_quiz_if_needed(fuckued_up_question_index, fucked_up_questions);
  197.  
  198.     cout << endl << "Your mark is " << calculate_mark(fuckued_up_question_index, RADNOM_QUESTIONS_COUNT);
  199. }
  200.  
  201. void pass_final_test() {
  202.     srand(time(0));
  203.     cout << "pass_final_test";
  204.  
  205.     topic_title_to_index_map["T1"] = 0;
  206.     topic_title_to_index_map["T2"] = 1;
  207.     topic_title_to_index_map["T3"] = 2;
  208.  
  209.     unordered_map<int, string> topic_index_to_title_map;
  210.     topic_index_to_title_map[0] = "T1";
  211.     topic_index_to_title_map[1] = "T2";
  212.     topic_index_to_title_map[2] = "T3";
  213.  
  214.     QuestionAndAnswers topic_to_question_and_answers[TOPICS_COUNT][MAX_QUESTIONS_COUNT_PER_TOPIC];
  215.     int topic_to_question_answer[TOPICS_COUNT][MAX_QUESTIONS_COUNT_PER_TOPIC];
  216.     string topic_to_questions[TOPICS_COUNT] = {"q_and_a/questions_1.txt", "q_and_a/questions_2.txt", "q_and_a/questions_3.txt"};
  217.     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"};
  218.  
  219.     for(int i = 0; i < TOPICS_COUNT; i++) {
  220.         string questions_file_name = topic_to_questions[i];
  221.         string answers_file_name = topic_to_answers[i];
  222.         generate_quesitons_and_answers_by_topic(questions_file_name, topic_index_to_title_map[i], topic_to_question_and_answers[i]);
  223.         generate_correct_answers_by_topic(answers_file_name, topic_to_question_answer[i]);
  224.     }
  225.  
  226.     for(int i = 0; i < TOPICS_COUNT; i++) {
  227.         for(int j = 0; j < MAX_QUESTIONS_COUNT_PER_TOPIC; j++) {
  228.             cout << "T" << (i + 1) << "-" << (j + 1) << ": " << topic_to_question_answer[i][j] << endl;
  229.         }
  230.     }
  231.  
  232.  
  233.     // for(int i = 0; i < TOPICS_COUNT; i++) {
  234.     //     cout << "---------Topic--------- " << i + 1 << endl;
  235.     //     for(int j = 0; j < MAX_QUESTIONS_COUNT_PER_TOPIC; j++) {
  236.     //         QuestionAndAnswers question = topic_to_question_and_answers[i][j];
  237.     //         int correct_answer = topic_to_question_answer[i][question.number - 1];
  238.     //         cout << "Q" << question.number << ": " << question.question << "-" << correct_answer << endl;
  239.     //     }
  240.     // }
  241.  
  242.     int curr = 0;
  243.     int last_topic_index = -1;
  244.     set<string> seen;
  245.  
  246.     QuestionAndAnswers random_questions[MAX_FINAL_TEST_QUESTIONS_COUNT];
  247.     while(curr < MAX_FINAL_TEST_QUESTIONS_COUNT) {
  248.         int rand_topic_index = 0 + (rand() % TOPICS_COUNT);
  249.         if(last_topic_index == rand_topic_index) continue;
  250.             int rand_question_by_topic_index = 0 + (rand() % MAX_QUESTIONS_COUNT_PER_TOPIC);
  251.             string question_id = topic_to_questions[rand_topic_index] + ":" + to_string(rand_question_by_topic_index);
  252.             if(seen.count(question_id) <= 0) {
  253.                 seen.insert(question_id);
  254.                 random_questions[curr++] = topic_to_question_and_answers[rand_topic_index][rand_question_by_topic_index];
  255.                 last_topic_index = rand_topic_index;
  256.             }
  257.     }
  258.  
  259.     // cout << "-----Random questions-----";
  260.     // for(int i = 0; i < MAX_FINAL_TEST_QUESTIONS_COUNT; i++) {
  261.     //     QuestionAndAnswers question = random_questions[i];
  262.     //     int correct_answer = topic_to_question_answer[question.topic_index][question.number - 1];
  263.     // }
  264.  
  265.     FuckedUpQuestion *fucked_up_questions = NULL;
  266.     int fuckued_up_question_index = 0;
  267.  
  268.     for(int i = 0; i < MAX_FINAL_TEST_QUESTIONS_COUNT; i++) {
  269.         QuestionAndAnswers question = random_questions[i];
  270.         int correct_anwer_to_the_question = topic_to_question_answer[question.topic_index][question.number - 1];
  271.  
  272.         int answer = -1;
  273.         while(true) {
  274.             cout << endl << endl << "Q" << question.number << ": " << question.question << "\n " << question.answers << ": ";
  275.             cin >> answer;
  276.             if(answer > 0 && answer < 5) break;
  277.         }
  278.  
  279.         if(correct_anwer_to_the_question != answer) {
  280.             fucked_up_questions = (FuckedUpQuestion*)realloc(fucked_up_questions, sizeof(FuckedUpQuestion) * (fuckued_up_question_index + 1));
  281.            
  282.             FuckedUpQuestion fucked_up_question;
  283.             fucked_up_question.number = question.number;
  284.             fucked_up_question.question = question.question;
  285.  
  286.             fucked_up_questions[fuckued_up_question_index++] = fucked_up_question;
  287.         }
  288.     }
  289.  
  290.     print_errors_after_quiz_if_needed(fuckued_up_question_index,fucked_up_questions);
  291.     cout << endl << "Your mark is " << calculate_mark(fuckued_up_question_index, MAX_FINAL_TEST_QUESTIONS_COUNT);
  292. }
  293.  
  294. void show_student_menu() {
  295.     cout << endl << "------Student menu------" << endl;
  296.  
  297.     int menu_item = -1;
  298.     while(true) {
  299.          cout << "\n1 - Training by a topic"
  300.         << "\n2 - Testing by a topic"
  301.         << "\n3 - Final test"
  302.         << "\n0 - Exit: ";
  303.          cin >> menu_item;
  304.  
  305.         if(menu_item == 0) break;
  306.  
  307.         switch(menu_item) {
  308.             case 1: train_by_selected_topic(); break;
  309.             case 2: test_by_selected_topic(); break;
  310.             case 3: pass_final_test(); break;
  311.             default: cout << endl << "Menu item by " << menu_item << " cannot be found" << endl;
  312.         }
  313.        
  314.     }
  315. }
  316.  
  317. int main() {
  318.     int menu_item = -1; // think of making a string and convert string to
  319.     // test_by_selected_topic();
  320.     pass_final_test();
  321.    
  322.     // while(true) {
  323.     //      cout << endl << "Login yourself. "
  324.     //      << "\n 1 - Teacher"
  325.     //      << "\n 2 - Student"
  326.     //      << "\n 0 - Exit: ";
  327.     //      cin >> menu_item;
  328.  
  329.     //     if (menu_item == 0) break;
  330.  
  331.     //     switch(menu_item) {
  332.     //         case 1: break; // todo login as a teacher
  333.     //         case 2: show_student_menu(); break; // todo login as a student
  334.     //         default: cout << endl << "Menu item by " << menu_item << " cannot be found" << endl; break;
  335.     //     }
  336.        
  337.     // }
  338.  
  339.     return 0;
  340. }
  341.  
Add Comment
Please, Sign In to add comment