Senin, 10 September 2012

Konversi Biner . php


<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if (isset($_POST['decimal'])){//apakah data ter-submit?
        $decimal = $_POST['decimal'];
        $original = $_POST['decimal'];
        $binary='';
        if (preg_match('/[^0-9]/',$decimal)) {//memastikan inputan adalah angka
          die("Maaf. Inputan salah..");
        }
        else{
            while ($decimal > 0) {//Looping memutuskan apakah 1 atau 0 yang dita
                if($decimal%2 == 0){//..menambah 0
                    $binary .= 0;//$binary=0+$binary;
                    $decimal /= 2;//$binary=$binary/2;
                }
                else {//menambahkan 1
                    $binary .=1;//$binary=1+$binary;
                    $decimal = ($decimal1/2)-0.5;
                }
            }
          $result = strrev($binary);//hasile diwalik
          echo "Bilangan $original (desimal) dalam biner adalah $result.
          <a href='konversibiner.php'>Back</a> to the script";// hasil
          }
        }
        else{
        ?>
</html>
<head><title>koversi biner</title>
<head>
<body> tes
    <form action="<?php echo $_SERVER['PHP_SELF'];?>"
          method="POST">
        <h3> Masukkan bilangan Desimal disini (cepat!!):
            </h3><input type="text" size="50" name="decimal">
                <input type="submit" value="Konversikan!">
                </form>
    <?php
        echo "<br>".$_SERVER['PHP_SELF'];
        ?>
</body>
</html>
<?php
        }
       ?>




Tidak ada komentar:

Posting Komentar