Advertisement
Radfler

quiz (PL)

Jun 1st, 2015
2,764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. /*
  8.  * Format pliku:
  9.  * 1. Dwie pierwsze linie oznaczają kolejno tytuł i autora quizu.
  10.  * 2. Później może nastąpić dowolna liczba zestawów, będących pojedynczym pytaniem.
  11.  *    Każdy z nich powinien mieć długość sześciu linijek, które kolejno oznaczają:
  12.  *  - treść pytania,
  13.  *  - odpowiedzi a, b, c, d,
  14.  *  - prawidłowa odpowiedź (pojedynczy znak).
  15.  * 3. Plik musi nazywać się 'quiz' i mieć rozszerzenie '.txt'.
  16.  */
  17.  
  18. //---------------------------------------------------------------------------------------------------------------------
  19.  
  20. class Question {
  21.  
  22. public:
  23.  
  24.     void load(int question_number) {
  25.  
  26.         ifstream file("quiz.txt");
  27.         string line;
  28.  
  29.         if(file.fail()) exit(1);
  30.  
  31.         for(int i=0; i<-4+6*question_number; ++i)
  32.             getline(file, line);
  33.  
  34.         for(int i=0; i<6; i++) {
  35.  
  36.             getline(file, line);
  37.             switch(i) {
  38.  
  39.                 case 0: content = line;               break;
  40.                 case 1:
  41.                 case 2:
  42.                 case 3:
  43.                 case 4: answers[i-1] = line;          break;
  44.                 case 5: correct_answer = line.back(); break;
  45.             }
  46.         }
  47.     }
  48.  
  49.     bool ask() const {
  50.  
  51.         cout << content << endl;
  52.  
  53.         char answer_char = 'a';
  54.         for(auto &&this_answer : answers)
  55.             cout << answer_char++ << ") " << this_answer << endl;
  56.  
  57.         cout << "Twoja odpowiedz: ";
  58.         cin.get(answer_char);
  59.  
  60.         return answer_char == correct_answer;
  61.     }
  62.  
  63. private:
  64.  
  65.     string content, answers[4];
  66.     char   correct_answer;
  67. };
  68.  
  69. //---------------------------------------------------------------------------------------------------------------------
  70.  
  71. class Game {
  72.  
  73. public:
  74.  
  75.     explicit Game(int new_question_count)
  76.     : score(0), questions_count(new_question_count), questions(new Question[questions_count]) {
  77.  
  78.         ifstream file("quiz.txt");
  79.         if(file.fail()) exit(1);
  80.  
  81.         getline(file, title);
  82.         getline(file, author);
  83.  
  84.         int question_number=1;
  85.  
  86.         for(auto this_question=questions; this_question!=questions+questions_count; ++this_question)
  87.             this_question->load(question_number++);
  88.     }
  89.  
  90.     ~Game() {
  91.  
  92.         delete[] questions;
  93.     }
  94.  
  95.     void play() {
  96.  
  97.         cout << "Autor quizu: " << author << endl
  98.              << "Tytul: " << title << endl << endl;
  99.  
  100.         for(auto this_question=questions; this_question!=questions+questions_count; ++this_question) {
  101.  
  102.             cin.sync();
  103.             score += this_question->ask();
  104.             cout << endl;
  105.         }
  106.  
  107.         cout << "Koniec quizu!" << endl
  108.              << "Zdobyte punkty: " << score << endl
  109.              << "Blednych odpowiedzi: " << questions_count-score << endl;
  110.  
  111.         cin.get();
  112.     }
  113.  
  114. private:
  115.  
  116.     int score, questions_count;
  117.  
  118.     string title, author;
  119.     Question *questions;
  120. };
  121.  
  122. //---------------------------------------------------------------------------------------------------------------------
  123.  
  124. int main() {
  125.  
  126.     int questions_count;
  127.     cout << "Ile pytan jest w Twoim quizie? ";
  128.     cin >> questions_count;
  129.     cout << endl;
  130.  
  131.     Game this_game(questions_count);
  132.     this_game.play();
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement