fork download
  1. <?php
  2.  
  3.  
  4. abstract class Question
  5. {
  6. protected $text;
  7. protected $points;
  8. protected $correctAnswer;
  9.  
  10. abstract function getAsString();
  11. abstract function checkAnswer($answer);
  12. }
  13.  
  14. class MultipleChoiceQuestion extends Question
  15. {
  16. protected $answers;
  17. protected $almostCorrectAnswer;
  18. protected $hint;
  19.  
  20. public function __construct($text, float $points, array $answers, $correctAnswer, $hint)
  21. {
  22. $this->text = $text;
  23. $this->points = $points;
  24. $this->answers = $answers;
  25. $this->correctAnswer = $correctAnswer;
  26. $this->hint = $hint;
  27. }
  28.  
  29. public function addAlmostCorrectAnswer($almostCorrectAnswer)
  30. {
  31. $this->almostCorrectAnswer = $almostCorrectAnswer;
  32. }
  33.  
  34. public function getAsString()
  35. {
  36. $string = "{$this->text}\nВарианты ответов:\n";
  37.  
  38. foreach ($this->answers as $letter => $answer) {
  39. $string = $string."{$letter}. {$answer}\n";
  40. }
  41.  
  42. return $string;
  43. }
  44.  
  45. public function checkAnswer($answer)
  46. {
  47. $points = $this->points;
  48. $text = $this->text;
  49. $hint = $this->hint;
  50.  
  51. if ($answer == $this->correctAnswer) {
  52. return array(1, $points);
  53.  
  54. } elseif ($answer == $this->almostCorrectAnswer) {
  55. return array(1, $points, '', '', 1);
  56.  
  57. }
  58.  
  59. return array(0, $points, $text, $hint);
  60. }
  61.  
  62. }
  63.  
  64. class NumericalQuestion extends Question
  65. {
  66. protected $deviation;
  67.  
  68. function __construct($text, float $points, float $correctAnswer, float $deviation = 0)
  69. {
  70. $this->text = $text;
  71. $this->points = $points;
  72. $this->correctAnswer = $correctAnswer;
  73. $this->deviation = $deviation;
  74. }
  75.  
  76. public function getAsString()
  77. {
  78. return "{$this->text}\n";
  79. }
  80.  
  81. public function checkAnswer($answer)
  82. {
  83. $points = $this->points;
  84. $text = $this->text;
  85.  
  86. if ($answer == $this->correctAnswer) {
  87. return array(1, $points);
  88.  
  89. } elseif ($this->deviation > 0) {
  90.  
  91. $leftDeviation = $this->correctAnswer - $this->deviation;
  92. $rightDeviation = $this->correctAnswer + $this->deviation;
  93.  
  94. if ($leftDeviation < $answer and $rightDeviation > $answer) {
  95. return array(1, $points);
  96. }
  97.  
  98. }
  99.  
  100. return array(0, $points, $text);
  101. }
  102.  
  103. }
  104.  
  105. function createQuestions() {
  106. $questions = [];
  107.  
  108. $text = 'Какая планета располагается четвертой по счету от Солнца?';
  109. $answers = array('a' => 'Венера', 'b' => 'Марс', 'c' => 'Юпитер', 'd' => 'Меркурий');
  110. $hint = 'Одноименное название носит шоколадный батончик.';
  111. $q = new MultipleChoiceQuestion($text, 10, $answers, 'b', $hint);
  112.  
  113. $questions[] = $q;
  114.  
  115. $text = 'Какой город является столицей Великобритании?';
  116. $answers = array('a' => 'Париж', 'b' => 'Москва', 'c' => 'Нью-Йорк', 'd' => 'Лондон');
  117. $hint = '%Городнейм% из кэпитал оф грейт британ.';
  118. $q = new MultipleChoiceQuestion($text, 5, $answers, 'd', $hint);
  119.  
  120. $questions[] = $q;
  121.  
  122. $text = 'Кто придумал теорию относительности?';
  123. $answers = array('a' => 'Джон Леннон', 'b' => 'Джим Моррисон', 'c' => 'Альберт Эйнштейн', 'd' => 'Исаак Ньютон');
  124. $hint = 'Этим парнем был...';
  125. $q = new MultipleChoiceQuestion($text, 30, $answers, 'c', $hint);
  126. $q->addAlmostCorrectAnswer('b');
  127.  
  128. $questions[] = $q;
  129.  
  130. $q = new NumericalQuestion('Чему равна скорость света в км/с?', 15, 300000);
  131.  
  132. $questions[] = $q;
  133.  
  134. $q = new NumericalQuestion('Чему равно число Пи?', 30, 3.14, 0.01);
  135.  
  136. $questions[] = $q;
  137.  
  138. $q = new NumericalQuestion('В каком году закончилась вторая мировая война?', 10, 1945);
  139.  
  140. $questions[] = $q;
  141.  
  142. return $questions;
  143. }
  144.  
  145. function printQuestions($questions) {
  146.  
  147. $number = 1;
  148.  
  149. foreach ($questions as $question) {
  150. echo "\n{$number}. ";
  151. echo $question->getAsString();
  152.  
  153. $number ++;
  154. }
  155.  
  156. }
  157.  
  158. function checkAnswers($questions, $answers)
  159. {
  160. if (count($questions) != count($answers)) {
  161. die("Число ответов и вопросов не совпадает\n");
  162. }
  163.  
  164. $pointsTotal = 0;
  165. $pointsMax = 0;
  166. $correctAnswers = 0;
  167.  
  168. $totalQuestions = count($questions);
  169.  
  170. for ($i = 0; $i < count($questions); $i++) {
  171.  
  172. $question = $questions[$i];
  173. $answer = $answers[$i];
  174.  
  175. $resultOfCheck = $question->checkAnswer($answer);
  176.  
  177. $pointsMax += $resultOfCheck[1];
  178.  
  179. if ($resultOfCheck[0]) {
  180. $correctAnswers ++;
  181.  
  182. if (!empty($resultOfCheck[4])) {
  183. $pointsTotal += $resultOfCheck[1] / 2;
  184. } else {
  185. $pointsTotal += $resultOfCheck[1];
  186. }
  187.  
  188. } else {
  189. $number = $i + 1;
  190. echo "\nНеправильный ответ на вопрос №{$number} ({$resultOfCheck[2]})\n";
  191.  
  192. if (!empty($resultOfCheck[3])) {
  193. echo "\nПодсказка: {$resultOfCheck[3]}\n";
  194. }
  195.  
  196. }
  197.  
  198. }
  199.  
  200. echo "\nПравильных ответов: {$correctAnswers} из {$totalQuestions}, баллов набрано: $pointsTotal из $pointsMax\n";
  201. }
  202.  
  203. $questions = createQuestions();
  204. printQuestions($questions);
  205. checkAnswers($questions, array('b', 'c', 'b', 300000, 3.14444444, 1945));
Success #stdin #stdout 0.01s 82560KB
stdin
Standard input is empty
stdout
1. Какая планета располагается четвертой по счету от Солнца?
Варианты ответов:
a. Венера
b. Марс
c. Юпитер
d. Меркурий

2. Какой город является столицей Великобритании?
Варианты ответов:
a. Париж
b. Москва
c. Нью-Йорк
d. Лондон

3. Кто придумал теорию относительности?
Варианты ответов:
a. Джон Леннон
b. Джим Моррисон
c. Альберт Эйнштейн
d. Исаак Ньютон

4. Чему равна скорость света в км/с?

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

6. В каком году закончилась вторая мировая война?

Неправильный ответ на вопрос №2 (Какой город является столицей Великобритании?)

Подсказка: %Городнейм% из кэпитал оф грейт британ.

Правильных ответов: 5 из 6, баллов набрано: 80 из 100