Advertisement
rozman50

Untitled

Dec 29th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.57 KB | None | 0 0
  1. <html>
  2. <body>
  3. <?php
  4.  
  5. for($i = 0; $i<10; $i++){
  6. $r = rand(0,1);
  7. $tab[] = $r;
  8.  
  9. }
  10. echo "<table>";
  11. echo "<tr>";
  12. echo "<td> Dvojisko stevilo </td>";
  13. echo "<td> Desetisko stevilo </td>";
  14. echo "</tr>";
  15. echo "<tr>";
  16. echo "<td>";
  17. echo implode("",$tab);
  18. echo "</td>";
  19. echo "<td>";
  20. echo bindec(implode("",$tab));
  21. echo "</td>";
  22. echo "</tr>";
  23. echo "</table>";
  24. echo "<br>";
  25.  
  26.  
  27. function sumbin($tab2,$tab3){
  28. $predznak = 0;
  29. $tabsum = array();
  30. for($i = count($tab2)-1; $i>=0; $i--){
  31.         if($tab2[$i] == 0 && $tab3[$i] == 0 && $predznak == 0)
  32.             array_unshift($tabsum,0);
  33.         else if($tab2[$i] == 0 && $tab3[$i] == 0 && $predznak == 1){
  34.             $predznak = 0;
  35.             array_unshift($tabsum,1);
  36.         }
  37.         else if((($tab2[$i] == 1 && $tab3[$i] == 0) || ($tab2[$i] == 0 && $tab3[$i] == 1)) && $predznak == 0){
  38.                 array_unshift($tabsum,1);
  39.                 $predznak = 0;
  40.             }
  41.         else if($tab2[$i] == 1 && $tab3[$i] == 1 && $predznak == 0){
  42.                 $predznak = 1;
  43.                 array_unshift($tabsum,0);
  44.         }
  45.         else if(($tab2[$i] == 1 && $tab3[$i] == 0 && $predznak == 1) || ($tab2[$i] == 0 && $tab3[$i] == 1 && $predznak == 1)){
  46.             $predznak = 1;
  47.             array_unshift($tabsum,0);
  48.         }else if($tab2[$i] == 1 && $tab3[$i] == 1 && $predznak == 1){
  49.                 $predznak = 1;
  50.                 array_unshift($tabsum,1);
  51.         }
  52.     }
  53.         array_unshift($tabsum,1);
  54.  
  55.     return $tabsum;
  56. }
  57.  
  58. function negbindec($array){
  59. $vsota = 0;
  60. $vsota +=  -1*$array[0] * pow(2,(count($array)-1));
  61.  
  62. for ($i =count($array)-2, $j = 1; $i >= 0; $i--, $j++)
  63.   $vsota += $array[$j] * pow(2,$i);
  64.  
  65.  
  66. return $vsota;
  67. }
  68.  
  69.  
  70. function negbin($bin){
  71.  
  72.     foreach($bin as $neki){
  73.         if($neki == '0')
  74.             $stbin[] = 1;
  75.         else $stbin[] = 0;
  76.  
  77.         $stbinosn[] = $neki;
  78.     }
  79.  
  80.     return $stbin;
  81. }
  82.  
  83. $tab2 = negbin($tab);
  84.  
  85. foreach($tab2 as $neki)
  86.     $tab3[] = 0;
  87.  
  88. $tab3[count($tab3)-1] = 1;
  89.  
  90. $tabsum = sumbin($tab2,$tab3);
  91.  
  92. echo "<table>";
  93. echo "<tr>";
  94. echo "<td> Neg. Dvojisko stevilo </td>";
  95. echo "<td> Neg. Desetisko stevilo </td>";
  96. echo "</tr>";
  97. echo "<tr>";
  98. echo "<td>";
  99. echo implode("",$tabsum);
  100. echo "</td>";
  101. echo "<td>";
  102. echo "-" . bindec(implode("",$tab));
  103. echo "</td>";
  104. echo "</tr>";
  105. echo "</table>";
  106. echo "<br>";
  107.  
  108.  
  109. echo "<table>";
  110. echo "<tr>";
  111. echo "<td> Pred. Dvojisko stevilo </td>";
  112. echo "<td> Desetisko stevilo </td>";
  113. echo "</tr>";
  114. echo "<tr>";
  115. echo "<td>";
  116.  
  117. if($tab[0] == 0){
  118.   echo implode("",$tab);
  119.   echo "</td>";
  120.   echo "<td>";
  121.   echo bindec(implode("",$tab));
  122.   echo "</td>";
  123. }else{
  124.   echo implode("",$tab);
  125.   echo "</td>";
  126.   echo "<td>";
  127.   echo negbindec($tab);
  128.   echo "</td>";
  129. }
  130. echo "</tr>";
  131. echo "</table>";
  132. echo "<br>";
  133.  
  134. ?>
  135. </body>
  136. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement