Advertisement
arxeiss

Projekt do LO

Apr 18th, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3.   <head>
  4.   <meta http-equiv="content-type" content="text/html; charset=utf8">
  5.   <title>Projekt do LO</title>
  6.   </head>
  7.   <body>
  8.   <h1>Projekt do LO na 16 bitů</h1>
  9.   <?php
  10.   function doplnek($cislo,$otocit = false)
  11.   {
  12.     if($otocit){
  13.       $cislo *= -1;
  14.     }
  15.     if($cislo < 0)
  16.       return 65536 - abs($cislo);
  17.     return $cislo;
  18.   }
  19.   if(!empty($_POST)){
  20.     $q = intval($_POST["q"]);
  21.     $a = round(floatval(str_replace(",", ".", $_POST["a"])) * pow(2, $q));
  22.     $b = round(floatval(str_replace(",", ".", $_POST["b"])) * pow(2, $q));
  23.     if($q == 0 or $a == 0 or $b == 0){
  24.       echo "error";
  25.     }else{
  26.       $aD = doplnek($a);
  27.       $bD = doplnek($b);
  28.       $aH = strtoupper(dechex($aD));
  29.       $bH = strtoupper(dechex($bD));
  30.       echo "<strong>Číslo A:</strong> (".str_pad($aH, 4,'0',STR_PAD_LEFT).")H";
  31.       echo "<br /><strong>Číslo B:</strong> (".str_pad($bH, 4,'0',STR_PAD_LEFT).")H";
  32.      
  33.       $soucet = hexdec($aH) + hexdec($bH);
  34.       $rozdil = hexdec($aH) + doplnek(hexdec($bH),true);
  35.       $sH = strtoupper(dechex($soucet));
  36.       $rH = strtoupper(dechex($rozdil));
  37.       if(strlen($sH) > 4)
  38.         $sH = substr($sH,strlen($sH)-4,4);
  39.       if(strlen($rH) > 4)
  40.         $rH = substr($rH,strlen($rH)-4,4);
  41.      
  42.       echo "<br /><strong>Součet:</strong> (".str_pad($sH, 4,'0',STR_PAD_LEFT).")H";
  43.       echo "<br /><strong>Rozdíl:</strong> (".str_pad($rH, 4,'0',STR_PAD_LEFT).")H";
  44.      
  45.       $sD = hexdec($sH);
  46.       $rD = hexdec($rH);
  47.      
  48.       if($sD >= 32768)
  49.         $sD -= 65536;
  50.       if($rD >= 32768)
  51.         $rD -= 65536;
  52.      
  53.       $sD = $sD / pow(2, $q);
  54.       $rD = $rD / pow(2, $q);
  55.      
  56.       echo "<br /><strong>Součet:</strong> ".round($sD,2);
  57.       echo "<br /><strong>Rozdíl:</strong> ".round($rD,2);
  58.      
  59.     }
  60.   }
  61.   ?>
  62.   <form action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST">
  63.   <p><strong>Formát:</strong> Q<input type="text" name="w" size="1" value="<?php echo $_POST["w"];?>"/>
  64.   .<input type="text" name="q" size="1" value="<?php echo $q;?>"/></p>
  65.   <p><strong>Číslo A:</strong> <input type="text" name="a" value="<?php echo $_POST["a"];?>" /></p>
  66.   <p><strong>Číslo B:</strong> <input type="text" name="b" value="<?php echo $_POST["b"];?>" /></p>
  67.   <p><input type="submit" value="Počítej!" /></p>
  68.   </form>
  69.   </body>
  70. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement