-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorBF1.html
More file actions
109 lines (105 loc) · 3.37 KB
/
CalculatorBF1.html
File metadata and controls
109 lines (105 loc) · 3.37 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>103AC2024 Calculator</title>
<style type="text/css">
input[type="text"]{
width: 122px;
text-align: right;
}
input[type="button"]{
width: 40px;
}
</style>
<script type="text/javascript">
var tmpx, tmpy;
var tmpOpr;
var istmpOpr = false;
var isFloat = false;
function fnShowValueOnScreen(e){
if(istmpOpr){
document.getElementById("result").value = "";
istmpOpr = !istmpOpr;
}
document.getElementById("result").value += e;
tmpy = parseFloat(document.getElementById("result").value);
//console.log(document.getElementById("result").value);
}
function fnOperatorsAdd(e){
if(tmpx == null && document.getElementById("result").value != null){ //第一個數
tmpx = tmpy; //紀錄被數
tmpOpr = e; //紀錄運算子
istmpOpr = true;
//console.log(tmpx + tmpOpr); //
}
else if(tmpOpr != null){
switch(e){
case "+":
tmpx += tmpy;
console.log(tmpx);
break;
case "-":
tmpx -= tmpy;
console.log(tmpx);
break;
case "x":
tmpx *= tmpy;
console.log(tmpx);
break;
case "÷":
tmpx /= tmpy;
console.log(tmpx);
break;
default:
fnOperatorsAdd(tmpOpr);
//tmpy = null;
break;
}
document.getElementById("result").value = tmpx;
tmpOpr = (e != "=" ? e : tmpOpr); //紀錄運算子
istmpOpr = true;
}
console.log("tmpx" + tmpx);
console.log("tmpy" + tmpy);
console.log(tmpOpr);
}
function fnPointAdd(e){
if(!isFloat){
fnShowValueOnScreen(e);
isFloat = true;
}
}
function fnClear(){
tmpx = null;
tmpy = null;
isFloat = false;
document.getElementById("result").value = "";
}
</script>
</head>
<body>
<input type="button" id="clear" name="clear" value="C" onclick="fnClear();">
<input type="text" id="result" name="result" value="">
<br>
<input type="button" id="c7" name="c7" value="7" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="c8" name="c8" value="8" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="c9" name="c9" value="9" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="cDivision" name="cDivision" value="÷" onclick="fnOperatorsAdd(this.value);">
<br>
<input type="button" id="c4" name="c4" value="4" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="c5" name="c5" value="5" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="c6" name="c6" value="6" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="cMultiply" name="cMultiply" value="x" onclick="fnOperatorsAdd(this.value);">
<br>
<input type="button" id="c1" name="c1" value="1" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="c2" name="c2" value="2" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="c3" name="c3" value="3" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="cSubtract" name="cSubtract" value="-" onclick="fnOperatorsAdd(this.value);">
<br>
<input type="button" id="c0" name="c0" value="0" onclick="fnShowValueOnScreen(this.value);">
<input type="button" id="cPoint" name="cPoint" value="." onclick="fnPointAdd(this.value);">
<input type="button" id="cEqual" name="cEqual" value="=" onclick="fnOperatorsAdd(this.value);">
<input type="button" id="cAdd" name="cAdd" value="+" onclick="fnOperatorsAdd(this.value);">
</body>
</html>