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

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

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