-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
138 lines (127 loc) · 5.41 KB
/
index.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>雅的计算器</title>
<link rel="stylesheet" href="style.css">
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<div class="calculator">
<div class="result" style="grid-area: result">
{{ equation }}
</div>
<button style="grid-area: ac" @click="clear">AC</button>
<button style="grid-area: plus-minus" @click="calculateToggle">±</button>
<button style="grid-area: percent" @click="calculatePercentage">%</button>
<button style="grid-area: add" @click="append('+')">+</button>
<button style="grid-area: subtract" @click="append('-')">-</button>
<button style="grid-area: multiply" @click="append('×')">×</button>
<button style="grid-area: divide" @click="append('÷')">÷</button>
<button style="grid-area: equal" @click="calculate">=</button>
<button style="grid-area: number-1" @click="append(1)">1</button>
<button style="grid-area: number-2" @click="append(2)">2</button>
<button style="grid-area: number-3" @click="append(3)">3</button>
<button style="grid-area: number-4" @click="append(4)">4</button>
<button style="grid-area: number-5" @click="append(5)">5</button>
<button style="grid-area: number-6" @click="append(6)">6</button>
<button style="grid-area: number-7" @click="append(7)">7</button>
<button style="grid-area: number-8" @click="append(8)">8</button>
<button style="grid-area: number-9" @click="append(9)">9</button>
<button style="grid-area: number-0" @click="append(0)">0</button>
<button style="grid-area: dot" @Click="append('.')">.</button>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
equation: '0',
isDecimalAdded: false,
isOperatorAdded: false,
isStarted: false,
isEnd: false
},
methods: {
// Check if the charachter is + - * /
isOperator(character) {
return ['+', '-', '×', '÷'].indexOf(character) > -1
},
// When pressed Operators or Numbers
append(character) {
// Start
if (this.equation === '0' && !this.isOperator(character) && !this.isEnd) {
if (character === '.') {
this.equation += '' + character
this.isDecimalAdded = true
} else {
this.equation = '' + character
}
this.isStarted = true
return
}
// If Number
if (!this.isOperator(character)) {
if (this.isEnd) {
this.clear()
this.equation = ''
this.isEnd = false
this.isStarted = true
}
if (character === '.' && this.isDecimalAdded) {
return
}
if (character === '.') {
this.isDecimalAdded = true
}
this.equation += '' + character
}
// Added Operator
if (this.isOperator(character) && !this.isOperatorAdded) {
this.equation += '' + character
this.isEnd = false
this.isOperatorAdded = true
this.isDecimalAdded = false
}
},
// When pressed '='
calculate() {
let result = this.equation.replace(new RegExp('×', 'g'),
'*').replace(new RegExp('÷', 'g'), '/')
this.equation = parseFloat(eval(result).toFixed(9)).toString()
this.isDecimalAdded = false
this.isOperatorAdded = false
this.isEnd = true
},
// When pressed '+/-'
calculateToggle() {
if (this.isOperatorAdded || !this.isStarted) {
return
}
this.equation = this.equation + '* -1'
this.calculate()
this.isEnd = false
},
//When pressed '%'
calculatePercentage() {
if (this.isOperatorAdded) {
return
}
this.equation = this.equation + '* 0.01'
this.calculate()
this.isEnd = false
},
// When pressed 'AC'
clear() {
this.equation = '0',
this.isDecimalAdded = false,
this.isOperatorAdded = false,
this.isStarted = false
}
}
})
</script>
</body>
</html>