fork download
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace Anon\Quiz;
  6.  
  7. class QuestionException extends \Exception {}
  8.  
  9. abstract class Question
  10. {
  11. protected $text;
  12. protected $correctAnswer;
  13.  
  14. public function __construct(string $text, $correctAnswer)
  15. {
  16.  
  17. $this->text = $text;
  18. $this->correctAnswer = $correctAnswer;
  19.  
  20. }
  21.  
  22. public function getQuestionText(): string
  23. {
  24.  
  25. return $this->text;
  26.  
  27. }
  28.  
  29. public function getCorrectAnswer()
  30. {
  31.  
  32. return $this->correctAnswer;
  33.  
  34. }
  35.  
  36. abstract public function getQuestionTips(): string;
  37.  
  38. abstract public function checkAnswer($userAnswer): bool;
  39.  
  40. }
  41.  
  42.  
  43. class NumericQuestion extends Question
  44. {
  45.  
  46. private $answerDeviation;
  47.  
  48. public function __construct(string $text, float $correctAnswer, float $answerDeviation)
  49. {
  50.  
  51. parent::__construct($text, $correctAnswer);
  52. $this->answerDeviation = $answerDeviation;
  53.  
  54. }
  55.  
  56. public function getQuestionTips(): string
  57. {
  58.  
  59. $output = "Введите вариант ответа. Допустимая погрешность - {$this->answerDeviation}.";
  60.  
  61. return $output;
  62.  
  63. }
  64.  
  65. public function checkAnswer($userAnswer): bool
  66. {
  67.  
  68. $leftLimit = $this->correctAnswer - $this->answerDeviation;
  69. $rightLimit = $this->correctAnswer + $this->answerDeviation;
  70.  
  71. return ($userAnswer >= $leftLimit && $userAnswer <= $rightLimit);
  72.  
  73. }
  74.  
  75. }
  76.  
  77.  
  78. class ChoiceQuestion extends Question
  79. {
  80.  
  81. private $choices = array();
  82.  
  83. public function __construct(string $text, string $correctAnswer, array $choices)
  84. {
  85.  
  86. if (empty($choices)) {
  87.  
  88. throw new QuestionException('Массив с вариантами ответов пуст.');
  89.  
  90. }
  91.  
  92. parent::__construct($text, $correctAnswer);
  93. $this->choices = $choices;
  94.  
  95. }
  96.  
  97. public function getQuestionTips(): string
  98. {
  99.  
  100. $output = "Варианты ответа:\n";
  101.  
  102. foreach ($this->choices as $choiceIndex => $choiceText) {
  103.  
  104. $output .= " {$choiceIndex}) {$choiceText}\n";
  105.  
  106. }
  107.  
  108. return rtrim($output);
  109.  
  110. }
  111.  
  112. public function checkAnswer($userAnswer): bool
  113. {
  114.  
  115. return $userAnswer == $this->correctAnswer;
  116.  
  117. }
  118.  
  119. public function getAvailableChoices(): array
  120. {
  121.  
  122. return $this->choices;
  123.  
  124. }
  125.  
  126. }
  127.  
  128.  
  129. class QuestionWriter
  130. {
  131.  
  132. private $questions = array();
  133.  
  134. public function addQuestion(Question $question): void
  135. {
  136.  
  137. $this->questions[] = $question;
  138.  
  139. }
  140.  
  141. public function addQuestions(array $questions): void
  142. {
  143.  
  144. if (empty($questions)) {
  145.  
  146. throw new QuestionException('Переданный массив с вопросами пуст.');
  147.  
  148. }
  149.  
  150. foreach ($questions as $question) {
  151.  
  152. $this->addQuestion($question);
  153.  
  154. }
  155.  
  156. }
  157.  
  158. public function writeQuestions(): void
  159. {
  160.  
  161. $output = '';
  162. $questionNumber = 1;
  163.  
  164. foreach ($this->questions as $question) {
  165.  
  166. $output .= "$questionNumber. {$question->getQuestionText()}\n\n";
  167.  
  168. $output .= "{$question->getQuestionTips()}\n\n\n";
  169.  
  170. $questionNumber++;
  171.  
  172. }
  173.  
  174. echo rtrim($output);
  175.  
  176. }
  177.  
  178. public function writeAnswers(array $userAnswers): void
  179. {
  180.  
  181. $output = '';
  182. $questionNumber = 1;
  183.  
  184. foreach ($this->questions as $question) {
  185.  
  186. $arrayIndex = $questionNumber - 1;
  187.  
  188. if (!isset($userAnswers[$arrayIndex])) {
  189.  
  190. throw new QuestionException("Ответ на вопрос не найден (индекс $arrayIndex)");
  191.  
  192. }
  193.  
  194. $userAnswer = $userAnswers[$arrayIndex];
  195. $result = $question->checkAnswer($userAnswer);
  196.  
  197. $output .= "$questionNumber. Ваш ответ - $userAnswer. ";
  198.  
  199. $output .= $result ? "Верно!" : "Неверно! Правильный ответ - {$question->getCorrectAnswer()}.";
  200.  
  201. $output .= "\n";
  202.  
  203. $questionNumber++;
  204.  
  205. }
  206.  
  207. echo rtrim($output);
  208.  
  209. }
  210.  
  211. }
  212.  
  213.  
  214. function getQuestions(): array
  215. {
  216.  
  217. $questions = array();
  218.  
  219. $questions[] = new ChoiceQuestion('В каком фильме Георгия Данелия низшим сословиям предписывалось носить цак?', 'b',
  220. array('a' => 'Новая планета',
  221. 'b' => 'Кин-дза-дза!',
  222. 'c' => 'Убить Билла',
  223. 'd' => 'Не грози Южному Централу, попивая сок у себя в квартале'
  224. ));
  225.  
  226. $questions[] = new ChoiceQuestion('Первый и последний президент Советского Союза?', 'd',
  227. array('a' => 'Сталин',
  228. 'b' => 'Черненко',
  229. 'c' => 'Ельцин',
  230. 'd' => 'Горбачев'
  231. ));
  232.  
  233. $questions[] = new NumericQuestion('Чему равняется число Пи?', 3.14, 0.002);
  234. $questions[] = new NumericQuestion('Сколько существует чудес света?', 7, 0);
  235.  
  236. return $questions;
  237.  
  238. }
  239.  
  240. $questions = getQuestions();
  241.  
  242. $questionWriter = new QuestionWriter();
  243. $questionWriter->addQuestions($questions);
  244. $questionWriter->writeQuestions();
  245.  
  246. $answers = array('b', 'c', 3.141592, 8);
  247.  
  248. echo "\n\n\n";
  249.  
  250. $questionWriter->writeAnswers($answers);
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
1. В каком фильме Георгия Данелия низшим сословиям предписывалось носить цак?

Варианты ответа:
    a) Новая планета
    b) Кин-дза-дза!
    c) Убить Билла
    d) Не грози Южному Централу, попивая сок у себя в квартале


2. Первый и последний президент Советского Союза?

Варианты ответа:
    a) Сталин
    b) Черненко
    c) Ельцин
    d) Горбачев


3. Чему равняется число Пи?

Введите вариант ответа. Допустимая погрешность - 0.002.


4. Сколько существует чудес света?

Введите вариант ответа. Допустимая погрешность - 0.


1. Ваш ответ - b. Верно!
2. Ваш ответ - c. Неверно! Правильный ответ - d.
3. Ваш ответ - 3.141592. Верно!
4. Ваш ответ - 8. Неверно! Правильный ответ - 7.