Halo kalkulator uza PHP



Hmm…Aubada sira, iha nee hau hakarak hakerek tutorial oinsa halo kalkulator ruma uza linguajen programasaun PHP. Tamba nee hau hakarak hakerek ezemplu rua kona ba buat nee:
Ezemplu primeiru:
<html>
<head>
<title>Calculator</title>
<style type=”text/css”>
<!–
.style1 {font-size: 24px}
–>
</style>
</head>
<body><center>
<table height=”332″ border=1>
<tr>
<td width=”391″ height=”50″>
<div align=”center”>Calculator</div></td>
</tr>
<tr>
<td height=”204″>
<form method=”post” action=”<? echo $_PHP_SELF;?>”> First Number:
<input type=”text” name=”bil1″ size=”15″ value=”<? echo $_POST[‘bil1’]?>”> <br><br>
Second number: <input type=”text” name=”bil2″ size=”15″ value=”<? echo $_POST[‘bil2’]?>”> <br><br>

<center>
<input type=”submit” name=”operator” value=”+”>
<input type=”submit” name=”operator” value=”-“>
<input type=”submit” name=”operator” value=”/”>
<input type=”submit” name=”operator” value=”X”>
</center>
<br>
Result :
<input type=”text” name=”hasil” size=”15″ readonly=”yes” value=”<? echo eksekusi();?>”>
<br /><br />
</form></td>
</tr>
<tr>
<td height=”65″> <div align=”right”>Created by : Abrao Ximenes</div></td>
</tr>
</table></center>

</body>
</html>
<?
function eksekusi()
{
$op   = $_POST[‘operator’];
$bil1 = $_POST[‘bil1’];
$bil2 = $_POST[‘bil2’];
if($op == “+”)
{
$hasil = $bil1 + $bil2;
}
elseif($op == “-“)
{
$hasil = $bil1 – $bil2;
}
elseif($op == “/”)
{
$hasil = $bil1 / $bil2;
}
elseif($op == “X”)
{
$hasil = $bil1 * $bil2;
}
return $hasil;
}
?>

Ezemplu Segundu:

<html>
<head>
<title>Kalkulator</title>
</head>
<body>
<style type=”text/css”>
.Hasil {
color: black;
background-color: #a4a775;
font-size: 2em;
border: 2px solid #fa9504;
}
.Operator {
color: silver ;
background-color: black;
font-size: 1.5em;
border: 1px solid #fa9504;
width: 100px;
padding: 4px;
}
.Angka {
color: silver;
background-color: black;
font-size: 1.5em;
border: 1px solid #fa9504;
padding: 4px;
width: 100px;
}
</style>

<?php

$angka1=$_POST[angka1];
$angka2=$_POST[angka2];
$Op1=$_POST[Op1];
$tHasil=$_POST[tHasil];
$tAngka=$_POST[tAngka];
$tOperasi=$_POST[tOperasi];
$tClear=$_POST[tClear];

if(isset($tAngka)){
$tHasil.=$tAngka;
}
else if(isset($tOperasi)){
if($tOperasi!=’=’){
$Op1=$tOperasi;
$angka1=$tHasil;
unset($tHasil);
}
else{
$angka2=$tHasil;
$tHasil=Hitung($angka1,$angka2,$Op1);
$angka1=$tHasil;
}
}
else{
unset($angka1);
unset($angka2);
unset($Op1);
unset($tHasil);
unset($tAngka);
unset($tOperasi);
unset($tClear);
}

function Hitung($b1,$b2,$o){
switch($o){
case ‘+’:
return $b1+$b2;
break;
case ‘-‘:
return $b1-$b2;
break;
case ‘*’:
return $b1*$b2;
break;
case ‘/’:
{
if ($b2 == 0) return “can’t dev by zero”;
else return $b1/$b2;
break;
}
default:
}
}
?>

<form method=post action=”>
<input type=hidden name=angka1 value='<?=$angka1?>’>
<input type=hidden name=angka2 value='<?=$angka2?>’>
<input type=hidden name=Op1 value='<?=$Op1?>’>
<input type=hidden name=tHasil value='<?=$tHasil?>’>

<h3 align=center font=arial>Calculator</h3>

<table width=”141″ height=”172″ border=1 align=center>
<tr>
<td colspan=5 align=right>&nbsp;<?=$tHasil?></td>
</tr>
<tr>
<td><input type=submit name=tOperasi value=+ /></td>
<td><input type=submit name=tAngka value=1 size=10 /></td>
<td><input type=submit name=tAngka value=2 /></td>
<td><input type=submit name=tAngka value=3 /></td>
</tr>

<tr>
<td><input type=submit name=tOperasi value=- /></td>
<td><input type=submit name=tAngka value=4 /></td>
<td><input type=submit name=tAngka value=5 /></td>
<td><input type=submit name=tAngka value=6 /></td>
</tr>

<tr>
<td><input type=submit name=tOperasi value=* /></td>
<td><input type=submit name=tAngka value=7 /></td>
<td><input type=submit name=tAngka value=8 /></td>
<td><input type=submit name=tAngka value=9 /></td>
</tr>
<tr>
<td><input type=submit name=tOperasi value=/ /></td>
<td align=center><input type=submit name=tOperasi value=’=’ /></td>
<td align=center>
<input type=submit name=tAngka value=0 /></td>
<td colspan=2 align=center>
<input type=submit name=tClear value=CE /></td></tr>

</table><center><table width=”433″ border=”1″>
<tr>
<td width=”437″ bgcolor=”#99FFFF”><center>abraoximenes@yahoo.com
</center></td>
</tr>
</table></center>
</form>
</body>
</html>

This entry was posted in INFORMATION TECHNOLOGY, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s