fork(1) download
  1. <?php
  2.  
  3. abstract class AbstractQuestion {
  4. public $text; //текст вопроса
  5. public $correctAnswer; //правильный ответ
  6. public $points; //баллы за правильный ответ
  7.  
  8. abstract function showQuestion();
  9. abstract function checkAnswer($answer);
  10. }
  11.  
  12. class ChoiceQuestion extends AbstractQuestion {
  13. public $options; // варианты ответов
  14.  
  15. public function __construct($text, $correctAnswer, $points = 5, $options) {
  16. $this->text = $text;
  17. $this->correctAnswer = $correctAnswer;
  18. $this->points = $points;
  19. $this->options = $options;
  20. }
  21.  
  22. public function showQuestion() {
  23. $str = '';
  24. $str .= "{$this->text}\n";
  25. foreach($this->options as $key => $option) {
  26. $str .= " {$key}. {$option}\n";
  27. }
  28. return $str;
  29. }
  30.  
  31. public function checkAnswer($answer) {
  32. if($this->correctAnswer == $answer) {
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. }
  39.  
  40. class NumericQuestion extends AbstractQuestion {
  41. public $deviation; // допустимая погрешность
  42.  
  43. public function __construct($text, $correctAnswer, $points = 5, $deviation) {
  44. $this->text = $text;
  45. $this->correctAnswer = $correctAnswer;
  46. $this->points = $points;
  47. $this->deviation = $deviation;
  48. }
  49.  
  50. public function showQuestion() {
  51. $str = '';
  52. $str .= "{$this->text}\n";
  53. return $str;
  54. }
  55.  
  56. public function checkAnswer($answer) {
  57. $plusDeviation = $this->correctAnswer + $this->deviation;
  58. $minusDeviation = $this->correctAnswer - $this->deviation;
  59.  
  60. /*проверяем совпадает или ответ с правильным или входит ли он в диапазон погрешности*/
  61. if (($this->correctAnswer == $answer) || (($answer <= $plusDeviation) && ($answer >= $minusDeviation))) {
  62. return true;
  63. } else {
  64. return false;
  65. }
  66. }
  67. }
  68.  
  69. function createQuestions() {
  70. $questions = array();
  71.  
  72. $q1 = new ChoiceQuestion("Какая планета располагается четвертой по счету от Солнца?", 'b', 5, ['a' => 'Венера', 'b' => 'Марс', 'c' => 'Юпитер', 'd' => 'Меркурий']);
  73.  
  74. $questions[] = $q1;
  75.  
  76. $q2 = new NumericQuestion("Чему равна скорость света в км/с?", 300000, 20, 20000);
  77.  
  78. $questions[] = $q2;
  79.  
  80. return $questions;
  81. }
  82.  
  83. function showQuestions($questions) {
  84. foreach ($questions as $question) {
  85. echo "{$question->showQuestion()}\n";
  86. }
  87. }
  88.  
  89. function checkAnswers($questions, $answers) {
  90.  
  91.  
  92. $pointsTotal = 0; // баллов набрано
  93. $pointsMax = 0; // сколько вообще можно набрать
  94. $correctAnswers = 0; // сколько отвечено верно
  95. $totalQuestions = count($questions); //всего вопросов
  96.  
  97. for($i = 0; $i < count($questions); $i++) {
  98. $currentQuestion = $questions[$i];
  99. $currentAnswer = $answers[$i];
  100.  
  101. $pointsMax += $currentQuestion->points;
  102.  
  103. if ($currentQuestion->checkAnswer($currentAnswer)) {
  104. $correctAnswers++;
  105. $pointsTotal += $currentQuestion->points;
  106. } else {
  107. $number = $i + 1;
  108. echo "\nНеправильный ответ на вопрос №{$number}: {$currentQuestion->text}\n";
  109. }
  110. }
  111. echo "Правильных ответов {$correctAnswers} из {$totalQuestions}, баллов набрано: {$pointsTotal} из {$pointsMax}";
  112. }
  113.  
  114. $questions = createQuestions();
  115. showQuestions($questions);
  116.  
  117. $answers = array('b', 320001);
  118.  
  119. checkAnswers($questions, $answers);
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
Какая планета располагается четвертой по счету от Солнца?
  a. Венера
  b. Марс
  c. Юпитер
  d. Меркурий

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


Неправильный ответ на вопрос №2: Чему равна скорость света в км/с?
Правильных ответов 1 из 2, баллов набрано: 5 из 25