<%--
Document : ServeltKal
Created on : Feb 22, 2014, 8:38:14 PM
Author : Gunawan
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Kalkulator Servlet</title>
<script language="JavaScript">
function addChar(input,character) {
if(input.value == null || input.value == "0")
input.value = character
else
input.value += character
}
function sqrt(form) {
form.display.value = Math.sqrt(form.display.value);
}
function sin(form) {
form.display.value = Math.sin(form.display.value);
}
function cos(form) {
form.display.value = Math.cos(form.display.value);
}
function tan(form) {
form.display.value = Math.tan(form.display.value);
}
function ln(form) {
form.display.value = Math.log(form.display.value);
}
function exp(form) {
form.display.value = Math.exp(form.display.value);
}
function sin(form) {
form.display.value = Math.sin(form.display.value);
}
function deleteChar(input) {
input.value = input.value.substring(0,input.value.length -1);
}
function changeSign(input) {
if(input.value.substring(0,1) == "-")
input.value = input.value.substring(1, input.value.length)
else
input.value = "-" + input.value
}
function compute(form) {
form.display.value = eval (form.display.value)
}
function square (form) {
form.display.value = eval (form.display.value) * eval (form.display.value)
}
function checkNum(str) {
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i+1)
if (ch < "0" || ch > "9") {
if (ch != "/" && ch != "*" && ch !="+"
&& ch != "-" && ch != "." && ch != "(" && ch!= ")" ) {
alert("error")
return false
}
}
}
return true
}
</script>
</head>
<body>
<h1>Kalkulator Servlet - Gunawan</h1>
<form NAME="sci-calc">
<table cellspacing="0" cellpadding="1">
<tr>
<td colspan="5" align="center"><input name="display" value="0" size="28" maxlength="25"></td>
</tr>
<tr>
<td align="center"><input type="button" value="("
onclick="addChar(this.form.display,\'(\')"></td>
<td align="center"><input type="button" value=")"
onclick="addChar(this.form.display,\')\')"></td>
<td align="center"><input type="button" value="±"
onclick="addChar(this.form.display,\'+/-\')"></td>
<td align="center"><input type="button" value="√"
onclick="if(checkNum(this.form.display.value)) {
sqrt(this.form)}"></td>
<TD ALIGN="center"><INPUT TYPE="button" VALUE="C" ONCLICK="this.form.display.value = 0 "></TD>
</tr>
<tr>
<td align="center"><input type="button" value="ln"
onclick="if(checkNum(this.form.display.value)) {
ln(this.form)}"></td>
<td align="center"><input type="button" value="7"
onclick="addChar(this.form.display,\'7\')"></td>
<td align="center"><input type="button" value="8"
onclick="addChar(this.form.display,\'8\')"></td>
<td align="center"><input type="button" value="9"
onclick="addChar(this.form.display,\'9\')"></td>
<td align="center"><input type="button" value="/"
onclick="addChar(this.form.display,\'/\')"></td>
</tr>
<tr>
<td align="center"><input type="button" value="sin"
onclick="if(checkNum(this.form.display.value)) {
sin(this.form)}"></td>
<td align="center"><input type="button" value="4"
onclick="addChar(this.form.display,\'4\')"></td>
<td align="center"><input type="button" value="5"
onclick="addChar(this.form.display,\'5\')"></td>
<td align="center"><input type="button" value="6"
onclick="addChar(this.form.display,\'6\')"></td>
<td align="center"><input type="button" value="*"
onclick="addChar(this.form.display,\'*\')"></td>
</tr>
<tr>
<td align="center"><input type="button" value="cos"
onclick="if(checkNum(this.form.display.value)) {
cos(this.form)}"></td>
<td align="center"><input type="button" value="1"
onclick="addChar(this.form.display,\'1\')"></td>
<td align="center"><input type="button" value="2"
onclick="addChar(this.form.display,\'2\')"></td>
<td align="center"><input type="button" value="3"
onclick="addChar(this.form.display,\'3\')"></td>
<td align="center"><input type="button" value="-"
onclick="addChar(this.form.display,\'-\')"></td>
</tr>
<tr>
<td align="center"><input type="button" value="tan"
onclick="if(checkNum(this.form.display.value)) {
tan(this.form)}"></td>
<td align="center"><input type="button" value="0"
onclick="addChar(this.form.display,\'0\')"></td>
<td align="center"><input type="button" value="."
onclick="addChar(this.form.display,\'.\')"></td>
<td align="center"><input type="button" value="+"
onclick="addChar(this.form.display,\'+\')"></td>
<TD ALIGN="center"><INPUT TYPE="button" VALUE="=" NAME="enter" ONCLICK="if (checkNum(this.form.display.value)) { compute(this.form) }"></TD>
</tr>
</table>
</form>
</body>
</html>