Konversi Bilangan Lengkap[PHP]

Selamat Mencoba !
Coding:

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php

error_reporting(0);

$des = $_REQUEST['des'];

$option = $_REQUEST['option'];



function bin($bil){

$decimal= $bil;

$ori=$decimal;;

while ($decimal>0){

if($decimal%2 == 0){

$binary .= 0;

$decimal /= 2;

}

else{

$binary .= 1;

$decimal = ($decimal/2)-0.5;


}

}

$result = strrev($binary);

return "Biner dari : $ori adalah : $result <br/>";

}


function oct($bil){

$des= $bil;

$ori=$des;

$oct='';

while($des>0){

$hasil=$des%8;

switch($hasil){

case 0: $oct.="0"; break;

case 1: $oct.="1"; break;

case 2: $oct.="2"; break;

case 3: $oct.="3"; break;

case 4: $oct.="4"; break;

case 5: $oct.="5"; break;

case 6: $oct.="6"; break;

case 7: $oct.="7"; break;

default : break;

}

if($des/8==0){

$sisa=($des%8);

$des=$sisa;

}

else{

$sisa=($des/8);

$des=$sisa%8;

}

}

$result = strrev($oct);

return "Octal dari : $ori adalah : $result <br/>";

}



function hex($bil){

$des= $bil;

$ori=$des;

$hex='';

while($des>0){

$hasil=$des%16;

switch($hasil){

case 0: $hex.="0"; break;

case 1: $hex.="1"; break;

case 2: $hex.="2"; break;

case 3: $hex.="3"; break;

case 4: $hex.="4"; break;

case 5: $hex.="5"; break;

case 6: $hex.="6"; break;

case 7: $hex.="7"; break;

case 8: $hex.="8"; break;

case 9: $hex.="9"; break;

case 10: $hex.="A"; break;

case 11: $hex.="B"; break;

case 12: $hex.="C"; break;

case 13: $hex.="D"; break;

case 14: $hex.="E"; break;

case 15: $hex.="F";

default : break;

}

if($des/16==0){

$sisa=($des%16);

$des=$sisa;

}

else{

$sisa=($des/16);

$des=$sisa%16;

}

}

$result = strrev($hex);

return "Hexa dari : $ori adalah : $result <br/>"; //funngsi ini mereturn hasil

}

?>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>All Convert</title>

</head>

<body>

<h3>Konversi Bilangan Desimal ke Bentuk Lainnya</h3>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"

method="post" name="form1">

<p>Masukkan bilangan desimal yang ingin anda ubah : <br/>

<input type="text" name="des" value="<?php echo $des ?>" /></p>

<p>Pilih konversi : <br/>

<input type="radio" name="option" value="bin"
     
<?php ($option=="bin") ? print 'checked=""': print ''; ?>/>

Desimal to Biner
</br>
<input type="radio" name="option" value="hex"

<?php ($option=="hex") ? print 'checked=""': print ''; ?>/>

Desimal to Hexa
</br>
<input type="radio" name="option" value="oct"

<?php ($option=="oct") ? print 'checked=""': print ''; ?>/>

Desimal to Octal

</p>

<input type="submit" name="submit" value="Submit"/>

</form>

<?php

if(isset($_POST['des'])){ //apakah data tersubmit?

/*mambuat variable untuk menyimpan data yang dikirim*/

$des = $_REQUEST['des'];

$option = $_REQUEST['option'];

//cek apakah data yang dikirim tidak kosong ?

if($des=='' || $option==''){

echo '<h2>Maaf data Kurang Lengkap</h2>';

}

else{

//cek jenis kelamin

switch($option){

case 'bin' : echo "<h3>".bin($des)."</h3>"; break;

case 'hex' : echo "<h3>".hex($des)."</h3>"; break;

case 'oct' : echo "<h3>".oct($des)."</h3>"; break;

default : break;

}

}

echo "<a href='".$_SERVER['PHP_SELF']."'>Reset</a>"; //hasil

}

?>

</body>

</html>

Screen Shoot:
Tekan Shift+f6

 

 

 

 

0 komentar:

Posting Komentar

 
Copyright © Ini Namanya Blog