Advertisement
NickAndNick

Пример реализации теста

Sep 9th, 2020 (edited)
2,471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <string>
  2. #include <array>
  3. #include <vector>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <iomanip>
  7. using namespace std;
  8. class Answer {
  9. public:
  10.     Answer() = default;
  11.     Answer(string title, bool correctly = false)
  12.         : title_(title), correctly_(correctly) {}
  13.     string title()const {
  14.         return title_;
  15.     }
  16.     bool correctly()const {
  17.         return correctly_;
  18.     }
  19.     void set() {
  20.         correctly_ = true;
  21.     }
  22. private:
  23.     string title_;
  24.     bool correctly_;
  25.     friend ostream& operator<<(ostream& out, const Answer& answer) {
  26.         out << answer.title_;
  27.         return out;
  28.     }
  29. };
  30. class Question {
  31. public:
  32.     Question() : index_(0U) {}
  33.     void title(const string& value) {
  34.         title_ = value;
  35.     }
  36.     string title()const {
  37.         return title_;
  38.     }
  39.     size_t size()const {
  40.         return box_.size();
  41.     }
  42.     void correctly(const size_t index) {
  43.         box_.at(index).set();
  44.     }
  45.     bool is_correct(const size_t index)const {
  46.         return box_.at(index).correctly();
  47.     }
  48.     void add_answer(Answer&& answer) {
  49.         if (index_ < size()) {
  50.             box_.at(index_) = move(answer);
  51.             ++index_;
  52.         }
  53.     }
  54. private:
  55.     string title_;
  56.     array<Answer, 4u> box_;
  57.     size_t index_;
  58.     friend ostream& operator<<(ostream& out, const Question& question) {
  59.         out << '\t' << question.title_ << "\n\n";
  60.         for (auto i = 0U; i < question.size(); ++i) {
  61.             out << ' ' << i + 1 << ". " << question.box_.at(i) << '\n';
  62.         }
  63.         out.put('\n');
  64.         return out;
  65.     }
  66. };
  67. class Test {
  68. public:
  69.     bool load(const string& path) {
  70.         ifstream source(path);
  71.         if (source.is_open()) {
  72.             string line;
  73.             while (getline(source, line)) {
  74.                 Question question;
  75.                 auto size = question.size();
  76.                 question.title(line);
  77.                 for (auto i = 0U; i < size; ++i) {
  78.                     getline(source, line);
  79.                     question.add_answer(Answer(line));
  80.                 }
  81.                 getline(source, line);
  82.                 question.correctly(stoul(line) - 1U);
  83.                 box_.push_back(question);
  84.             }
  85.             source.close();
  86.         } else {
  87.             return false;
  88.         }
  89.         return true;
  90.     }
  91.     pair<size_t, size_t> begin() {
  92.         pair<size_t, size_t> box{ 0U, box_.size() };
  93.         auto count = 0U;
  94.         for (const auto& item : box_) {
  95.             cout << item << "\n Номер поравильного ответа? ";
  96.             size_t number;
  97.             cin >> number;
  98.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  99.             --number;
  100.             if (item.is_correct(number)) ++box.first;
  101.             cout.put('\n');
  102.         }
  103.         return box;
  104.     }
  105. private:
  106.     vector<Question> box_;
  107. };
  108. int main() {
  109.     system("chcp 1251 > nul");
  110.     Test test;
  111.     test.load("ботаника.txt"s);
  112.     auto [correct, all] = test.begin();
  113.     cout
  114.         << "Результат: " << correct << "/" << all << " = "
  115.         << fixed << setprecision(2)
  116.         << double(correct * 100U) / all << "%\n";
  117.     system("pause > nul");
  118. }
  119.  
  120. // Примерное содержимое файла ботаника.txt
  121. /*
  122. К какому семейству относится РОМАШКА
  123. Розоцветные
  124. Астровые
  125. Сложноцветные
  126. Вересковые
  127. 2
  128. К какому семейству относится ШИПОВНИК
  129. Вересковые
  130. Сложноцветные
  131. Розоцветные
  132. Астровые
  133. 3
  134. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement