Advertisement
kajacx

Pocitani PHP

Apr 2nd, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.43 KB | None | 0 0
  1. <?php
  2. session_start();
  3. class Priklad {
  4.     private $cislo1;
  5.     private $cislo2;
  6.     private $operace;
  7.     private $minimum;
  8.     private $maximum;
  9.    
  10.     public function __construct($min, $max) {
  11.         $this->minimum = $min;
  12.         $this->maximum = $max;
  13.         $this->novy();
  14.     }
  15.    
  16.     public function novy() {
  17.         $this->cislo1 = rand($this->minimum, $this->maximum);
  18.         $this->cislo2 = rand($this->minimum, $this->maximum);
  19.         $nahodne = rand(0, 3);
  20.         switch($nahodne) {
  21.             case 0: $this->operace = "+"; break;
  22.             case 1: $this->operace = "-"; break;
  23.             case 2: $this->operace = "*"; break;
  24.             case 3: $this->operace = "/"; break;
  25.         }
  26.     }
  27.    
  28.     public function otazka() {
  29.         return $this->cislo1 . " " . $this->operace . " " . $this->cislo2 . " =";
  30.     }
  31.    
  32.     public function cislo1() {
  33.         return $this->cislo1;
  34.     }
  35.    
  36.     public function cislo2() {
  37.         return $this->cislo2;
  38.     }
  39.    
  40.     private function vysledek() {
  41.         switch($this->operace) {
  42.             case "+": return $this->cislo1 + $this->cislo2;
  43.             case "-": return $this->cislo1 - $this->cislo2;
  44.             case "*": return $this->cislo1 * $this->cislo2;
  45.             case "/": return round($this->cislo1 / $this->cislo2);
  46.         }
  47.     }
  48.    
  49.     public function spravne($tip) {
  50.         return $this->vysledek() == $tip;
  51.     }
  52. }
  53.  
  54. if(isset($_GET["reset"])) {
  55.     session_unset();
  56. }
  57.  
  58. if(isset($_GET["nove_parametry"])) {
  59.     $priklad = new Priklad($_GET["min"], $_GET["max"]);
  60. }
  61. elseif(isset($_SESSION["priklad"])) {
  62.     $priklad = unserialize($_SESSION["priklad"]);
  63. }
  64. else {
  65.     $priklad = new Priklad(1, 100);
  66. }
  67.  
  68. if(isset($_GET["vyhodnot"])) {
  69.     if($vysledek = $priklad->spravne($_GET["vysledek"])) {
  70.         $priklad->novy();
  71.     }
  72. }
  73. elseif(isset($_GET["jiny_priklad"])) {
  74.     $priklad->novy();
  75. }
  76.  
  77.  
  78.  
  79.  
  80. ?>
  81. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  82.  
  83. <html xmlns="http://www.w3.org/1999/xhtml" lang="cs" xml:lang="cs" dir="ltr">
  84.     <head>
  85.         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  86.         <title>Procvičovák</title>
  87.     </head>
  88.    
  89.     <body>
  90.         <form method="get" action="matematika.php">
  91.            
  92.             <?php
  93.             if(isset($vysledek)) {
  94.                 if($vysledek) {
  95.                     echo "Hurá, váš výsledek <strong>".$_GET["vysledek"].
  96.                             "</strong> je správně, máte tu další příklad.<br/>\n";
  97.                 }
  98.                 else {
  99.                     echo "Bohužel, váš výsledek <strong>".$_GET["vysledek"].
  100.                             "</strong> není správně, zkuzte to znovu.<br/>\n";
  101.                 }
  102.             }
  103.             echo $priklad->otazka();
  104.             ?>
  105.            
  106.             <input name="vysledek"/>
  107.             <input type="submit" name="vyhodnot" value="Poslat odpověď"/><br/>
  108.             <input type="submit" name="jiny_priklad" value="Jiný příklad"/><br/>
  109.            
  110.             <br/>
  111.            
  112.             Minimum: <input name="min"/> <br/>
  113.             Maximum: <input name="max"/> <br/>
  114.             <input type="submit" name="nove_parametry" value="Změnit minimum a maximum"/><br/>
  115.            
  116.             <br/>
  117.            
  118.             <input type="submit" name="reset" value="Reset"/>
  119.            
  120.         </form>
  121.     </body>
  122. </html>
  123.  
  124. <?php
  125. $_SESSION["priklad"] = serialize($priklad);
  126.  
  127. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement