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

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

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