我有一个js计算器的简单代码
<html><head>
<script language="javascript">
var among = 0
var oper = "+"
function cal(obj) {
var press = obj.value //得到所按键的信息
var result = document.getElementById("result") //得到结果框的输出结果
var result_arr = result.value.split("") //将结果框中的结果转化成数组
if((press>=0&&press<=9) || (press==".")) {
result.value = result.value + press
} else if(press == "Backspace"){
result.value = ""
result_arr.pop()
for(var i=0 i<result_arr.length i++) {
result.value += result_arr[i]
}
} else if(press == "Clear") {
result.value = ""
} else if(press == "+") {
oper = "+"
among = result.value
result.value = ""
} else if(press == "=") {
var temp = result.value
var temp2 = 1
result.value = ""
if(oper == "+") {
result.value = parseFloat(among) + parseFloat(temp)
} else if(oper == "-") {
result.value = parseFloat(among) - parseFloat(temp)
} else if(oper == "*") {
result.value = parseFloat(among) * parseFloat(temp)
} else if(oper == "/") {
result.value = parseFloat(among) / parseFloat(temp)
} else if(oper == "POWER"){
for(var i=0 i<parseFloat(temp) i++) {
temp2 = temp2*among
}
result.value = temp2
} else {
window.alert("input error!")
}
} else if(press == "-") {
oper = "-"
among = result.value
result.value = ""
} else if(press == "*") {
oper = "*"
among = result.value
result.value = ""
} else if(press == "/") {
oper = "/"
among = result.value
result.value = ""
} else if(press == "POWER"){
oper = "POWER"
among = result.value
result.value = ""
} else {
window.alert("input error!")
}
}
</script>
</head>
<body>
<table border="1px" class="style">
<tr><th colspan="4"><input id="result" style="width:300px" type="text" value=""/></th></tr>
<tr><td><input type="button" value="POWER" onclick="cal(this)"/></td><td><input type="button" value="Backspace" onclick="cal(this)"/></td><td><input type="button" value="Clear" onclick="cal(this)"/></td><td>&nbsp</td></tr>
<tr><td><input type="button" value="7" onclick="cal(this)"/></td><td><input type="button" value="8" onclick="cal(this)"/></td><td><input type="button" value="9" onclick="cal(this)"/></td><td><input type="button" value="-" onclick="cal(this)"/></td></tr>
<tr><td><input type="button" value="4" onclick="cal(this)"/></td><td><input type="button" value="5" onclick="cal(this)"/></td><td><input type="button" value="6" onclick="cal(this)"/></td><td><input type="button" value="*" onclick="cal(this)"/></td></tr>
<tr><td><input type="button" value="1" onclick="cal(this)"/></td><td><input type="button" value="2" onclick="cal(this)"/></td><td><input type="button" value="3" onclick="cal(this)"/></td><td><input type="button" value="/" onclick="cal(this)"/></td></tr>
<tr><td><input type="button" value="." onclick="cal(this)"/></td><td><input type="button" value="0" onclick="cal(this)"/></td><td><input type="button" value="+" onclick="cal(this)"/></td><td><input type="button" value="=" onclick="cal(this)"/></td></tr>
</table>
</body>
</html>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
<title>计算器</title>
</head>
<script language="javascript">
function test(){
var num1 = parseInt(document.getElementById("num1").value)
var num2 = parseFloat(document.getElementById("num2").value)
document.write("相加:"+(num1+num2))
document.write("相减:"+(num1-num2))
document.write("相乘:"+(num1*num2))
document.write("相除:"+(num1/num2))
}
</script>
<body>
<input type="test" id="num1" value/>
<input type="test" id="num2" value/>
<script>
</script>
<input type="button" value="+" onclick="test()"/>
<input type="button" value="-" onclick="test()"/>
<input type="button" value="*" onclick="test()"/>
<input type="button" value="/" onclick="test()"/>
</body>
</html>
计算机专业语言。简单来说,就是编写编定程序,让计算机代码解决某个问题,对某个计算体系规定一定的运算方式,使计算体系按照该计算方式运行,并最终得到相应结果的过程。
为了使计算机能够理解人的想法,人类通过某种方式,将需解决的问题的思路、方法和手段通过计算机能够理解的形式告诉它,让计算机完成人的指令。