Minggu, 06 Maret 2011

Menbuat kalkulator sederhana

Script HTML:

<!DOCTYPE html>
<html lang="en">

<head>
<title>Kalkulator Sederhana_Vertika</title>
<script language="JavaScript" type="text/javascript" src="calculator.js"></script>
<style type="text/css">
<!--
.huruf_header{
font-family: Berlin Sans FB;
font-size: 40px;
text-align: center;
color: #990066;
float:center;
}
-->
</style>
<link rel="icon" type="image/x-icon" href="a.jpg">
</head>

<body bgcolor="#CC33CC">
<center>
<table bgcolor="#CC99FF" border=0 align="justify" cellspacing=0 cellpadding=5>
<tr>
<td width="1600"><div class="huruf_header">KALKULATOR SEDERHANA</div></td>
</tr>
</table>
<br/>

<form name=form_kalkulator action="#">

<table border ="0">
<tr>
<td><input type="text" name="bil1" value="Input 1"/></td>
<td><select name="operasi" >
<option>Operator</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select></td>
<td><input type="text" name="bil2" value="Input 2"/></td>
<td><input type="button" value="PROSES" onclick="proses()" /></td>
<td><input type="text" name="hasil"/></td>
<td><input type="button" value="Clear" onclick="reset()" /></td>
<td><input type="button" value="Cetak" onclick="cetak()" /></td>
</tr>
</table>

</form>
</center>

</body>
</html>



Kode Java Script:

function proses(){

var myForm=document.form_kalkulator;

if (myForm.operasi.value == "+"){
myForm.hasil.value = parseInt(myForm.bil1.value) + parseInt(myForm.bil2.value);
}
else if (myForm.operasi.value == "-"){
myForm.hasil.value = parseInt(myForm.bil1.value) - parseInt(myForm.bil2.value);
}
else if (myForm.operasi.value == "*"){
myForm.hasil.value = parseInt(myForm.bil1.value) * parseInt(myForm.bil2.value);
}
else if (myForm.operasi.value == "/"){
myForm.hasil.value = parseInt(myForm.bil1.value) / parseInt(myForm.bil2.value);
}
}

function reset(){
var elemen = document.form_kalkulator;

elemen.bil1.value = "";
elemen.bil2.value = "";
elemen.hasil.value = "";
}

function cetak()
{
var myForm = document.form_kalkulator;
if (myForm.bil1.value=="Input 1" || myForm.operasi.value=="Operator" || myForm.bil1.value=="Input 2")
{
alert("Lengkapi Data Anda..");
}
else
{
alert("Hasil Perhitungan dari " + myForm.bil1.value + " " + myForm.operasi.value + " " + myForm.bil2.value + " = " + myForm.hasil.value);
}
}



Hasil:

Tidak ada komentar: