hingga jaman modern saat ini,, jenis kalkulator begitu beragam, diantaranya ada kalkulator yang sengaja dibuat menggunakan javascript, dan berikut ini adalah script yang digunakan untuk membuat java kalkulator seperti tampilan diatas.
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tugas kalkulator sederhana </title>
<style type="text/css">
td{text-align:center}
.kiri{text-align:left}
textarea{text-align:right;
font-size:16}
input{width:50;height:35 ; color:red}
a{font-size:18}
pre{font-size:14 ;color:red}
</style>
<script language="javascript">
temp=new Array();
sementara="";
opr_s="";
hasil="";
function periksa(a){
switch(a){
case "Bksp" :
temp.pop();document.form1.layar.value=parseInt(temp.join(""));break;
case "CE" :
hapusIsiArray(temp.length);document.form1.layar.value=0;sementara=0;hasil=0;opr_s="";break;
case "Clr" :
hapusIsiArray(temp.length);document.form1.layar.value="";sementara=0;hasil=0;opr_s="";break;
case "1" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "2" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "3" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "4" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "5" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "6" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "7" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "8" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "9" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "0" :
temp.push(a);document.form1.layar.value=parseInt(temp.join(""));break;
case "," :
alert("program ini dibuat untuk \n melengkapi \n tugas ke tiga \n\n mata kuliah yang dibimbing oleh bapak didik");break;
case "=" :
operasi(a);break;
case "+" :
operasi(a);break;
case "-" :
operasi(a);break;
case "*" :
operasi(a);break;
case "/" :
operasi(a);break;
case "Mod" :
operasi(a);break;
}
}
function hitung(opr1,opr2,operator){
var a=parseInt(opr1);
var b=parseInt(opr2);
var oprt=operator;
switch(oprt){
case '+' :
return(a+b);
break;
case '-' :
return(a-b);
break;
case '*' :
return(a*b);
break;
case '/' :
return(a/b);
break;
case "Mod" :
return(a%b);
break;
case'=' :
return(hasil);
break;}
}
function operasi(operator){
var oprt2=operator;
if(sementara==0 && opr_s==""){
sementara=parseInt(document.form1.layar.value);
opr_s=oprt2;
hapusIsiArray(temp.length);
document.form1.layar.value=sementara;
}
else {
sementara=hitung(sementara,parseInt(document.form1.layar.value),opr_s);
hasil=sementara;
hapusIsiArray(temp.length);
opr_s=oprt2;
document.form1.layar.value=hasil;
}
}
function hapusIsiArray(b){
var a=0;
while(a<b){
temp.shift();
a++;
}
}
</script>
</head>
<body bgcolor=lightgreen>
<table border=1 cellpadding=1 cellspacing=1 bgcolor=lightgreen>
<caption>Kalkulator java script o/ Rizqi ka</caption>
<form name=form1 >
<tr>
<td colspan=4>
<textarea cols=22 rows=1 name="layar"></textarea>
</td>
</tr>
<tr>
<td><input type=button value="Bksp" onClick="periksa(this.value)"></input></td>
<td><input type=button value="CE" onClick="periksa(this.value)"></input></td>
<td><input type=button value="Clr" onClick="periksa(this.value)"></input></td>
<td><input type=button value="Mod" onClick="periksa(this.value)"></input></td>
</tr>
<tr>
<td><input type=button value="7" onClick="periksa(this.value)"></input></td>
<td><input type=button value="8" onClick="periksa(this.value)"></input></td>
<td><input type=button value="9" onClick="periksa(this.value)"></input></td>
<td><input type=button value="/" onClick="periksa(this.value)"></input></td>
</tr>
<tr>
<td><input type=button value="4" onClick="periksa(this.value)"></input></td>
<td><input type=button value="5" onClick="periksa(this.value)"></input></td>
<td><input type=button value="6" onClick="periksa(this.value)"></input></td>
<td><input type=button value="*" onClick="periksa(this.value)"></input></td>
</tr>
<tr>
<td><input type=button value="1" onClick="periksa(this.value)"></input></td>
<td><input type=button value="2" onClick="periksa(this.value)"></input></td>
<td><input type=button value="3" onClick="periksa(this.value)"></input></td>
<td><input type=button value="-" onClick="periksa(this.value)"></input></td>
</tr>
<tr>
<td><input type=button value="0" onClick="periksa(this.value)"></input></td>
<td><input type=button value="," onClick="periksa(this.value)"></input></td>
<td><input type=button value="=" onClick="periksa(this.value)"></input></td>
<td><input type=button value="+" onClick="periksa(this.value)"></input></td>
</tr>
</tr>
</form>
</table>
</td>
<td class=kiri valign=top><pre><h3>
Keterangan Program Kalkulator :
<ol>
<li>Program ini menggunakan javascript untuk melakukan operasi aritmatika</li>
<li>Pastikan web browser anda mendukung javascript</li>
<li>Tombol "CE" dan "clr" digunakan untuk merefresh data</li>
<li>Tombol "Bksp" digunakan untuk menghapus satu angka terakhir</li>
</ol>
</h3></pre></td>
</tr>
</table>
</body>
</html>
Tidak ada komentar:
Posting Komentar