Untitled
4 years ago in HTML
<html><head><meta charset="UTF8">
<script language="javascript">function addChar(input, character) {
if(input.value == null || input.value == "0")
input.value = character
else
input.value += character
}
function cos(form) {
form.display.value = Math.cos(form.display.value);
}
function sin(form) {
form.display.value = Math.sin(form.display.value);
}
function tan(form) {
form.display.value = Math.tan(form.display.value);
}
function sqrt(form) {
form.display.value = Math.sqrt(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 deleteChar(input) {
input.value = input.value.substring(0, input.value.length - 1)
}
var val = 0.0;
function percent(input) {
val = input.value;
input.value = input.value + "%";
}
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) {
//if (val !== 0.0) {
// var percent = form.display.value;
// percent = pcent.substring(percent.indexOf("%")+1);
// form.display.value = parseFloat(percent)/100 * val;
//val = 0.0;
// } else
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.charAt(i);
if (ch < "0" || ch > "9") {
if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "."
&& ch != "(" && ch!= ")" && ch != "%") {
alert("invalid entry!")
return false
}
}
}
return true
}</script>
<style>input {
padding: 0;
margin: 1px;
}
.calculator {
width: 350px;
height: 320px;
background-color: #d9d9d9;
box-shadow: 0px 0px 0px 10px #666;
border: 5px solid black;
border-radius: 10px;
}
#display {
width: 98%;
height: 50px;
text-align: right;
background-color: black;
border: 3px solid white;
font-size: 18px;
left: 2px;
top: 2px;
color: #7fff00;
}
.btnNum {
color: white;
background-color: black;
font-size: 14px;
margin:auto;
border-radius:50%;
}
.btnNum:hover {
background-color: gray;
color: black;
}
.btnextra {
background-color:#03cc68;
}
.btnextra1 {
background-color:#b2f9ba;
}
.btnnormal {
background-color: #f2f2f2;
}
input[type=button]:hover {
background-color: gray; /* Green */
color: black;cursor:grab;}
input[type=button] {width:70px;height:60px;font-size:30px;border:1px solid gray;border-radius:50%;transition-duration: 0.4s;cursor:grab;}</style>
</style></head>
<body><center>
<form>
<table class="calculator" cellspacing="10" cellpadding="2">
<tbody><tr>
<td colspan="5"><input id="display" name="display" value="0" size="28" maxlength="25" style="letter-spacing: 0.23em;font-size: 21px;"></td>
</tr>
<tr>
<td><input type="button" style="color:white;background-color: #ff0000;" value="C" onclick="this.form.display.value= 0 "></td>
<td><input type="button" class="btnnormal" value="<--" onclick="deleteChar(this.form.display)"></td>
<td><input type="button" style="color:white;background-color: #3333ff;" value="=" onclick="if(checkNum(this.form.display.value)) { compute(this.form) }"></td>
<td><input type="button" class="btnextra1" value="π" onclick="addChar(this.form.display,'3.14159265359')"></td>
<td><input type="button" class="btnextra" value="%" onclick=" percent(this.form.display)"></td>
</tr>
<tr>
<td><input type="button" class="btnNum" value="7" onclick="addChar(this.form.display, '7')"></td>
<td><input type="button" class="btnNum" value="8" onclick="addChar(this.form.display, '8')"></td>
<td><input type="button" class="btnNum" value="9" onclick="addChar(this.form.display, '9')"></td>
<td><input type="button" class="btnextra1" value="x^" onclick="if(checkNum(this.form.display.value)) { exp(this.form) }"></td>
<td><input type="button" class="btnextra" value="/" onclick="addChar(this.form.display, '/')"></td>
</tr><tr>
<td><input type="button" class="btnNum" value="4" onclick="addChar(this.form.display, '4')"></td>
<td><input type="button" class="btnNum" value="5" onclick="addChar(this.form.display, '5')"></td>
<td><input type="button" class="btnNum" value="6" onclick="addChar(this.form.display, '6')"></td>
<td><input type="button" class="btnextra1" value="ln" onclick="if(checkNum(this.form.display.value)) { ln(this.form) }"></td>
<td><input type="button" class="btnextra" value="*" onclick="addChar(this.form.display, '*')"></td>
</tr>
<tr>
<td><input type="button" class="btnNum" value="1" onclick="addChar(this.form.display, '1')"></td>
<td><input type="button" class="btnNum" value="2" onclick="addChar(this.form.display, '2')"></td>
<td><input type="button" class="btnNum" value="3" onclick="addChar(this.form.display, '3')"></td>
<td><input type="button" class="btnextra1" value="√" onclick="if(checkNum(this.form.display.value)) { sqrt(this.form) }"></td>
<td><input type="button" class="btnextra" value="-" onclick="addChar(this.form.display, '-')"></td>
</tr>
<tr>
<td><input type="button" class="btnnormal" value="±" onclick="changeSign(this.form.display)"></td>
<td><input type="button" class="btnNum" value="0" onclick="addChar(this.form.display, '0')"></td>
<td><input type="button" class="btnnormal" value="." onclick="addChar(this.form.display, '.')"></td>
<td><input type="button" class="btnextra1" value="x2" onclick="if(checkNum(this.form.display.value)) { square(this.form) }"></td>
<td><input type="button" class="btnextra" value="+" onclick="addChar(this.form.display, '+')"></td>
</tr>
<tr>
<td><input type="button" class="btnnormal" value="(" onclick="addChar(this.form.display, '(')"></td>
<td><input type="button" class="btnnormal" value=")" onclick="addChar(this.form.display,')')"></td>
<td><input type="button" class="btnnormal" value="cos" onclick="if(checkNum(this.form.display.value)) { cos(this.form) }"></td>
<td><input type="button" class="btnextra1" value="sin" onclick="if(checkNum(this.form.display.value)) { sin(this.form) }"></td>
<td><input type="button" class="btnextra" value="tan" onclick="if(checkNum(this.form.display.value)) { tan(this.form) }"></td>
</tr>
</tbody></table>
</form>
</center>
</body></html>