Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <body>
- <?php
- for($i = 0; $i<10; $i++){
- $r = rand(0,1);
- $tab[] = $r;
- }
- echo "<table>";
- echo "<tr>";
- echo "<td> Dvojisko stevilo </td>";
- echo "<td> Desetisko stevilo </td>";
- echo "</tr>";
- echo "<tr>";
- echo "<td>";
- echo implode("",$tab);
- echo "</td>";
- echo "<td>";
- echo bindec(implode("",$tab));
- echo "</td>";
- echo "</tr>";
- echo "</table>";
- echo "<br>";
- function sumbin($tab2,$tab3){
- $predznak = 0;
- $tabsum = array();
- for($i = count($tab2)-1; $i>=0; $i--){
- if($tab2[$i] == 0 && $tab3[$i] == 0 && $predznak == 0)
- array_unshift($tabsum,0);
- else if($tab2[$i] == 0 && $tab3[$i] == 0 && $predznak == 1){
- $predznak = 0;
- array_unshift($tabsum,1);
- }
- else if((($tab2[$i] == 1 && $tab3[$i] == 0) || ($tab2[$i] == 0 && $tab3[$i] == 1)) && $predznak == 0){
- array_unshift($tabsum,1);
- $predznak = 0;
- }
- else if($tab2[$i] == 1 && $tab3[$i] == 1 && $predznak == 0){
- $predznak = 1;
- array_unshift($tabsum,0);
- }
- else if(($tab2[$i] == 1 && $tab3[$i] == 0 && $predznak == 1) || ($tab2[$i] == 0 && $tab3[$i] == 1 && $predznak == 1)){
- $predznak = 1;
- array_unshift($tabsum,0);
- }else if($tab2[$i] == 1 && $tab3[$i] == 1 && $predznak == 1){
- $predznak = 1;
- array_unshift($tabsum,1);
- }
- }
- array_unshift($tabsum,1);
- return $tabsum;
- }
- function negbindec($array){
- $vsota = 0;
- $vsota += -1*$array[0] * pow(2,(count($array)-1));
- for ($i =count($array)-2, $j = 1; $i >= 0; $i--, $j++)
- $vsota += $array[$j] * pow(2,$i);
- return $vsota;
- }
- function negbin($bin){
- foreach($bin as $neki){
- if($neki == '0')
- $stbin[] = 1;
- else $stbin[] = 0;
- $stbinosn[] = $neki;
- }
- return $stbin;
- }
- $tab2 = negbin($tab);
- foreach($tab2 as $neki)
- $tab3[] = 0;
- $tab3[count($tab3)-1] = 1;
- $tabsum = sumbin($tab2,$tab3);
- echo "<table>";
- echo "<tr>";
- echo "<td> Neg. Dvojisko stevilo </td>";
- echo "<td> Neg. Desetisko stevilo </td>";
- echo "</tr>";
- echo "<tr>";
- echo "<td>";
- echo implode("",$tabsum);
- echo "</td>";
- echo "<td>";
- echo "-" . bindec(implode("",$tab));
- echo "</td>";
- echo "</tr>";
- echo "</table>";
- echo "<br>";
- echo "<table>";
- echo "<tr>";
- echo "<td> Pred. Dvojisko stevilo </td>";
- echo "<td> Desetisko stevilo </td>";
- echo "</tr>";
- echo "<tr>";
- echo "<td>";
- if($tab[0] == 0){
- echo implode("",$tab);
- echo "</td>";
- echo "<td>";
- echo bindec(implode("",$tab));
- echo "</td>";
- }else{
- echo implode("",$tab);
- echo "</td>";
- echo "<td>";
- echo negbindec($tab);
- echo "</td>";
- }
- echo "</tr>";
- echo "</table>";
- echo "<br>";
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement