<?php
class Question
{
public $text; // текст вопроса
public $points = 5; // число баллов, по умолчанию 5
public $answers; // варианты ответов
public $correctAnswer; // правильный ответ
public $hint; // подсказка
}
$array = [
'question1' => ['question1' => 'Какая планета располагается четвертой по счету от Солнца?',
'points' => '10',
'answers' => ['a' => 'Венера марс юпитер меркурий',
'b' => 'Марс',
'c' => 'Юпитер',
'd' => 'Меркурий'],
'rightAnswer' => 'b',
'hint' => 'Она красная'],
'question2' => ['question2' => 'Столица Англии?',
'points' => '5',
'answers' => ['a' => 'Москва',
'b' => 'Бангладеш',
'c' => 'Нью-Йорк',
'd' => 'Лондон'],
'rightAnswer' => 'd',
'hint' => 'Начинается на "л"'],
'question3' => ['question3' => 'Кто придумал теорию относительности??',
'points' => '30',
'answers' => ['a' => 'Твоя мамка',
'b' => 'Сява',
'c' => 'Эйнштейн',
'd' => 'Путин'],
'rightAnswer' => 'c',
'hint' => 'Поехавший он']
];
function createQuestions($array)
{
// Создаем пустой массив
$questions = [];
// Создаем и заполняем первый объект
$q1 = new Question;
$q1->text = $array['question1']['question1'];
$q1->points = $array['question1']['points'];
$q1->answers = $array['question1']['answers'];
$q1->correctAnswer = $array['question1']['rightAnswer'];
$q1->hint = $array['question1']['hint'];
// Кладем вопрос в массив
$questions[] = $q1;
// Создаем второй объект
$q2 = new Question;
$q2->text = $array['question2']['question2'];
$q2->points = $array['question2']['points'];
$q2->answers = $array['question2']['answers'];
$q2->correctAnswer = $array['question2']['rightAnswer'];
$q2->hint = $array['question2']['hint'];
$questions[] = $q2;
$q3 = new Question;
$q3->text = $array['question3']['question3'];
$q3->points = $array['question3']['points'];
$q3->answers = $array['question3']['answers'];
$q3->correctAnswer = $array['question3']['rightAnswer'];
$q3->hint = $array['question3']['hint'];
$questions[] = $q3;
return $questions;
}
function printQuestions($array)
{
$number = 1; // номер вопроса
foreach ($array as $question) {
echo "\n{$number}. {$question->text}\n";
echo "\nВарианты ответов:\n";
foreach ($question->answers as $letter => $answer) {
echo " {$letter}. {$answer}\n";
}
$number++;
}
}
function checkAnswers($array, $answers)
{
// Проверим, что число ответов равно числу вопросов (защищаемся от ошибки)
die("Число ответов и вопросов не совпадает"); }
$pointsTotal = 0; // сколько набрано баллов
// сколько можно набрать баллов при всех правильных ответах
$pointsMax = 0;
// сколько отвечено верно
$correctAnswers = 0;
$totalQuestions = count($array); // Сколько всего вопросов
// Цикл для обхода вопросов и ответов
for ($i = 0; $i < count($array); $i++) { $question = $array[$i]; // Текущий вопрос
$answer = $answers[$i]; // текущий ответ
// Считаем максимальную сумму баллов
$pointsMax += $question->points;
// Проверяем ответ
if ($answer == $question->correctAnswer) {
// Добавляем баллы
$correctAnswers ++;
$pointsTotal += $question->points;
} else {
// Неправильный ответ
$number = $i + 1;
echo "Неправильный ответ на вопрос №{$number} ({$question->text})\n";
}
}
// Выведем итог
echo "Правильных ответов: {$correctAnswers} из {$totalQuestions}, баллов набрано: {$pointsTotal} из {$pointsMax}\n";
}
function hint ($array, $answers){
$totalQuestions = count($array); // Сколько всего вопросов
// Цикл для обхода вопросов и ответов
for ($i = 0; $i < count($array); $i++) { $question = $array[$i]; // Текущий вопрос
$answer = $answers[$i]; // текущий ответ
// Проверяем ответ
if ($answer == $question->correctAnswer) {
} else {
// Неправильный ответ
$number = $i + 1;
echo "\nПодсказка к вопросу №:{$number} {$question->hint}";
}
}
}
$questions = createQuestions($array);
printQuestions($questions);
$answers = array('b', 'd', 'a'); $a = checkAnswers($questions, $answers);
$b = hint ($questions, $answers);
echo "{$a} {$b}";
?>
// your code goes here