Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- /*
- * Format pliku:
- * 1. Dwie pierwsze linie oznaczają kolejno tytuł i autora quizu.
- * 2. Później może nastąpić dowolna liczba zestawów, będących pojedynczym pytaniem.
- * Każdy z nich powinien mieć długość sześciu linijek, które kolejno oznaczają:
- * - treść pytania,
- * - odpowiedzi a, b, c, d,
- * - prawidłowa odpowiedź (pojedynczy znak).
- * 3. Plik musi nazywać się 'quiz' i mieć rozszerzenie '.txt'.
- */
- //---------------------------------------------------------------------------------------------------------------------
- class Question {
- public:
- void load(int question_number) {
- ifstream file("quiz.txt");
- string line;
- if(file.fail()) exit(1);
- for(int i=0; i<-4+6*question_number; ++i)
- getline(file, line);
- for(int i=0; i<6; i++) {
- getline(file, line);
- switch(i) {
- case 0: content = line; break;
- case 1:
- case 2:
- case 3:
- case 4: answers[i-1] = line; break;
- case 5: correct_answer = line.back(); break;
- }
- }
- }
- bool ask() const {
- cout << content << endl;
- char answer_char = 'a';
- for(auto &&this_answer : answers)
- cout << answer_char++ << ") " << this_answer << endl;
- cout << "Twoja odpowiedz: ";
- cin.get(answer_char);
- return answer_char == correct_answer;
- }
- private:
- string content, answers[4];
- char correct_answer;
- };
- //---------------------------------------------------------------------------------------------------------------------
- class Game {
- public:
- explicit Game(int new_question_count)
- : score(0), questions_count(new_question_count), questions(new Question[questions_count]) {
- ifstream file("quiz.txt");
- if(file.fail()) exit(1);
- getline(file, title);
- getline(file, author);
- int question_number=1;
- for(auto this_question=questions; this_question!=questions+questions_count; ++this_question)
- this_question->load(question_number++);
- }
- ~Game() {
- delete[] questions;
- }
- void play() {
- cout << "Autor quizu: " << author << endl
- << "Tytul: " << title << endl << endl;
- for(auto this_question=questions; this_question!=questions+questions_count; ++this_question) {
- cin.sync();
- score += this_question->ask();
- cout << endl;
- }
- cout << "Koniec quizu!" << endl
- << "Zdobyte punkty: " << score << endl
- << "Blednych odpowiedzi: " << questions_count-score << endl;
- cin.get();
- }
- private:
- int score, questions_count;
- string title, author;
- Question *questions;
- };
- //---------------------------------------------------------------------------------------------------------------------
- int main() {
- int questions_count;
- cout << "Ile pytan jest w Twoim quizie? ";
- cin >> questions_count;
- cout << endl;
- Game this_game(questions_count);
- this_game.play();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement