<html>
<head>
<title>科学计算器</title>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<!--written by GoldHuman li hai-->
<!--2000.8-->
<style>
.buttons table input {
width: 50px;
}
</style>
<script language="javascript">
<!--
var endNumber = true
var mem = 0
var carry = 10
var hexnum = "0123456789abcdef"
var angle = "d"
var stack = ""
var level = "0"
var layer = 0
//数字键
function inputkey(key) {
var index = key.charCodeAt(0);
if ((carry == 2 && (index == 48 || index == 49))
|| (carry == 8 && index >= 48 && index <= 55)
|| (carry == 10 && (index >= 48 && index <= 57 || index == 46))
|| (carry == 16 && ((index >= 48 && index <= 57) || (index >= 97 && index <= 102))))
if (endNumber) {
endNumber = false
document.calc.display.value = key
}
else if (document.calc.display.value == null || document.calc.display.value == "0")
document.calc.display.value = key
else
document.calc.display.value += key
}
function changeSign() {
if (document.calc.display.value != "0")
if (document.calc.display.value.substr(0, 1) == "-")
document.calc.display.value = document.calc.display.value.substr(1)
else
document.calc.display.value = "-" + document.calc.display.value
}
//运算符
function operation(join, newlevel) {
endNumber = true
var temp = stack.substr(stack.lastIndexOf("(") + 1) + document.calc.display.value
while (newlevel != 0 && (newlevel <= (level.charAt(level.length - 1)))) {
temp = parse(temp)
level = level.slice(0, -1)
}
if (temp.match(/^(.*\d[\+\-\*\/\%\^\&\|x])?([+-]?[0-9a-f\.]+)$/))
document.calc.display.value = RegExp.$2
stack = stack.substr(0, stack.lastIndexOf("(") + 1) + temp + join
document.calc.operator.value = " " + join + " "
level = level + newlevel
}
//等号
function result() {
endNumber = true
while (layer > 0)
disbracket()
var temp = stack + document.calc.display.value
while ((level.charAt(level.length - 1)) > 0) {
temp = parse(temp)
level = level.slice(0, -1)
}
document.calc.display.value = temp
document.calc.bracket.value = ""
document.calc.operator.value = ""
stack = ""
level = "0"
}
//十进制转换
function todec(num, oldcarry) {
if (oldcarry == 10 || num == 0) return (num)
var neg = (num.charAt(0) == "-")
if (neg) num = num.substr(1)
var newnum = 0
for (var index = 1; index <= num.length; index++)
newnum = newnum * oldcarry + hexnum.indexOf(num.charAt(index - 1))
if (neg)
newnum = -newnum
return (newnum)
}
function decto(num, newcarry) {
var neg = (num < 0)
if (newcarry == 10 || num == 0) return (num)
num = "" + Math.abs(num)
var newnum = ""
while (num != 0) {
newnum = hexnum.charAt(num % newcarry) + newnum
num = Math.floor(num / newcarry)
}
if (neg)
newnum = "-" + newnum
return (newnum)
}
//表达式解析
function parse(string) {
if (string.match(/^(.*\d[\+\-\*\/\%\^\&\|x\<])?([+-]?[0-9a-f\.]+)([\+\-\*\/\%\^\&\|x\<])([+-]?[0-9a-f\.]+)$/))
return (RegExp.$1 + cypher(RegExp.$2, RegExp.$3, RegExp.$4))
else
return (string)
}
//数学运算和位运算
function cypher(left, join, right) {
left = todec(left, carry)
right = todec(right, carry)
if (join == "+")
return (decto(parseFloat(left) + parseFloat(right), carry))
if (join == "-")
return (decto(left - right, carry))
if (join == "*")
return (decto(left * right, carry))
if (join == "/" && right != 0)
return (decto(left / right, carry))
if (join == "%")
return (decto(left % right, carry))
if (join == "&")
return (decto(left & right, carry))
if (join == "|")
return (decto(left | right, carry))
if (join == "^")
return (decto(Math.pow(left, right), carry))
if (join == "x")
return (decto(left ^ right, carry))
if (join == "<")
return (decto(left << right, carry))
alert("除数不能为零")
return (left)
}
//界面
//-->
</script>
<!--written by GoldHuman li hai-->
<!--2000.8-->
<meta name="GENERATOR" content="MSHTML 8.00.6001.18702">
</head>
<body>
<div align="center">
<form name="calc">
<table border="1" width="500" height="250">
<tbody>
<tr>
<td height="50">
<table width="500">
<tbody>
<tr>
<td></td>
<td>
<div align="center">
<input value="0" readonly size="40" name="display">
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table width="500">
<tbody>
<tr>
<td>
<input style="background-color: lightgrey" readonly size="3" name="operator">
</td>
<td width="183">
<input style="color: red" onClick="backspace()" value=" 退格 " type="button">
<input style="color: red" onClick="document.calc.display.value = 0 " value=" 清屏 "
type="button">
</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr align="middle">
<td>
<input style="color: blue" onClick="inputkey('7')" value=" 7 " type="button" name="k7">
</td>
<td>
<input style="color: blue" onClick="inputkey('8')" value=" 8 " type="button" name="k8">
</td>
<td>
<input style="color: blue" onClick="inputkey('9')" value=" 9 " type="button" name="k9">
</td>
<td>
<input style="color: red" onClick="operation('/', 6)" value=" / " type="button">
</td>
</tr>
<tr align="middle">
<td>
<input style="color: blue" onClick="inputkey('4')" value=" 4 " type="button" name="k4">
</td>
<td>
<input style="color: blue" onClick="inputkey('5')" value=" 5 " type="button" name="k5">
</td>
<td>
<input style="color: blue" onClick="inputkey('6')" value=" 6 " type="button" name="k6">
</td>
<td>
<input style="color: red" onClick="operation('*', 6)" value=" * " type="button">
</td>
</tr>
<tr align="middle">
<td>
<input style="color: blue" onClick="inputkey('1')" value=" 1 " type="button">
</td>
<td>
<input style="color: blue" onClick="inputkey('2')" value=" 2 " type="button" name="k2">
</td>
<td>
<input style="color: blue" onClick="inputkey('3')" value=" 3 " type="button" name="k3">
</td>
<td>
<input style="color: red" onClick="operation('-', 5)" value=" - " type="button">
</td>
</td>
</tr>
<tr align="middle">
<td>
<input style="color: blue" onClick="inputkey('0')" value=" 0 " type="button">
</td>
<td>
<input style="color: blue" onClick="changeSign()" value="+/-" type="button">
</td>
<td>
<input style="color: blue" onClick="inputkey('.')" value=" . " type="button" name="kp">
</td>
<td>
<input style="color: red" onClick="operation('+', 5)" value=" + " type="button">
</td>
<td>
<input style="color: red" onClick="result()" value=" = " type="button">
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
简单计算器
最新推荐文章于 2023-09-18 09:54:05 发布