<?php
function createQuestions($text, $points, $answers, $correctAnswer) {
$q = new Question;
$questions[] = $q;
$q->text = $text;
$q->points = $points;
$q->answers=$answers;
$q->correctAnswer=$correctAnswer;
return $questions;
}
function printQuestions($questions)
{
$i = 1; // номер вопроса
foreach ($questions as $question) {
echo "<br>{$i}. {$question->text}<br>";
echo "Variants of answer:<br>";
foreach ($question->answers as $letter => $answer) {
echo " {$letter}. {$answer}<br>";
}
$i++;
}
}
function checkAnswers($questions, $answers){
die("The amount of questions and answers doesn't fit"); }
$pointsTotal = 0; // сколько набрано баллов
$pointsMax = 0; // сколько можно набрать баллов при всех правильных ответах
$correctAnswers = 0; // сколько отвечено верно
$totalQuestions = count($questions); // Сколько всего вопросов
// Цикл для обхода вопросов и ответов
for ($i = 0; $i < count($questions); $i++) { $question = $questions[$i]; // Текущий вопрос
$answer = $answers[$i]; // текущий ответ
// Считаем максимальную сумму баллов
$pointsMax += $question->points;
// Проверяем ответ
if ($answer == $question->correctAnswer) {
// Добавляем баллы
$correctAnswers ++;
$pointsTotal += $question->points;
} else {
// Неправильный ответ
$number = $i + 1;
echo "Wrong answer on question №{$number} ({$question->text})<br>";
}
}
// Выведем итог
echo "Right answers: {$correctAnswers} from {$totalQuestions}, Points: {$pointsTotal} from {$pointsMax} <br>";
}
$questions = CreateQuestions('Name a town', 5, ['a'=>'Paris', 'b'=>'Moscow', 'c'=>'London'], 'c');
printQuestions($questions);
$questions2 = CreateQuestions('Name an artist', 7, ['a'=>'Sergei', 'b'=>'Danila', 'c'=>'Tolyan'], 'b');
printQuestions($questions2);
checkAnswers($questions, 'c');
checkAnswers($questions2, 'b');
PD9waHAKCgpmdW5jdGlvbiBjcmVhdGVRdWVzdGlvbnMoJHRleHQsICRwb2ludHMsICRhbnN3ZXJzLCAkY29ycmVjdEFuc3dlcikgewogICAgJHEgPSBuZXcgUXVlc3Rpb247CiAgICAkcXVlc3Rpb25zW10gPSAkcTsKICAKICAKICAgICRxLT50ZXh0ID0gJHRleHQ7CiAg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