Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- class Priklad {
- private $cislo1;
- private $cislo2;
- private $operace;
- private $minimum;
- private $maximum;
- public function __construct($min, $max) {
- $this->minimum = $min;
- $this->maximum = $max;
- $this->novy();
- }
- public function novy() {
- $this->cislo1 = rand($this->minimum, $this->maximum);
- $this->cislo2 = rand($this->minimum, $this->maximum);
- $nahodne = rand(0, 3);
- switch($nahodne) {
- case 0: $this->operace = "+"; break;
- case 1: $this->operace = "-"; break;
- case 2: $this->operace = "*"; break;
- case 3: $this->operace = "/"; break;
- }
- }
- public function otazka() {
- return $this->cislo1 . " " . $this->operace . " " . $this->cislo2 . " =";
- }
- public function cislo1() {
- return $this->cislo1;
- }
- public function cislo2() {
- return $this->cislo2;
- }
- private function vysledek() {
- switch($this->operace) {
- case "+": return $this->cislo1 + $this->cislo2;
- case "-": return $this->cislo1 - $this->cislo2;
- case "*": return $this->cislo1 * $this->cislo2;
- case "/": return round($this->cislo1 / $this->cislo2);
- }
- }
- public function spravne($tip) {
- return $this->vysledek() == $tip;
- }
- }
- if(isset($_GET["reset"])) {
- session_unset();
- }
- if(isset($_GET["nove_parametry"])) {
- $priklad = new Priklad($_GET["min"], $_GET["max"]);
- }
- elseif(isset($_SESSION["priklad"])) {
- $priklad = unserialize($_SESSION["priklad"]);
- }
- else {
- $priklad = new Priklad(1, 100);
- }
- if(isset($_GET["vyhodnot"])) {
- if($vysledek = $priklad->spravne($_GET["vysledek"])) {
- $priklad->novy();
- }
- }
- elseif(isset($_GET["jiny_priklad"])) {
- $priklad->novy();
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" lang="cs" xml:lang="cs" dir="ltr">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title>Procvičovák</title>
- </head>
- <body>
- <form method="get" action="matematika.php">
- <?php
- if(isset($vysledek)) {
- if($vysledek) {
- echo "Hurá, váš výsledek <strong>".$_GET["vysledek"].
- "</strong> je správně, máte tu další příklad.<br/>\n";
- }
- else {
- echo "Bohužel, váš výsledek <strong>".$_GET["vysledek"].
- "</strong> není správně, zkuzte to znovu.<br/>\n";
- }
- }
- echo $priklad->otazka();
- ?>
- <input name="vysledek"/>
- <input type="submit" name="vyhodnot" value="Poslat odpověď"/><br/>
- <input type="submit" name="jiny_priklad" value="Jiný příklad"/><br/>
- <br/>
- Minimum: <input name="min"/> <br/>
- Maximum: <input name="max"/> <br/>
- <input type="submit" name="nove_parametry" value="Změnit minimum a maximum"/><br/>
- <br/>
- <input type="submit" name="reset" value="Reset"/>
- </form>
- </body>
- </html>
- <?php
- $_SESSION["priklad"] = serialize($priklad);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement