如何用js做计算机?

如题所述

是做一个计算器吧。首先放一堆button上去,绑定onclick事件,定义全局变量来接收每一个点击获取到的值,最后放一个input框显示输出的值。这是最简单的一个实现方法,如果看不懂的话可以再问,给你写一下。代码自己写的好,看别人不如自己写。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-16
我有一个js计算器的简单代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

<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> </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>
相似回答