-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
121 lines (109 loc) · 2.61 KB
/
app.vue
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
<template>
<div class="p-10">
<h1 class="text-xl mb-10">CalC</h1>
<div
class="
border border-gray-200
p-5
space-y-5
"
>
<div
class="
border border-gray-200
p-2
text-xl text-right
rounded
"
>{{ line }}</div>
<div class="space-x-1">
<template v-if="operator != ''">
<span>{{ stack }}</span>
<span>{{ operator }}</span>
</template>
<span>{{ line }}</span>
</div>
<div class="grid grid-cols-3 gap-2">
<CalcButton
title="AC"
@click="acClick()"
/>
<CalcButton
title="C"
@click="cClick()"
/>
</div>
<div class="grid grid-cols-3 gap-2">
<CalcButton
v-for="i in [1,2,3,4,5,6,7,8,9,0]"
:title="i"
:class="{
'col-start-2': i === 0,
}"
@click="digitClick(i)"
/>
</div>
<div class="grid grid-cols-4 gap-2">
<CalcButton
title="+"
@click="operatorClick('+')"
/>
<CalcButton
title="-"
@click="operatorClick('-')"
/>
<CalcButton
title="*"
@click="operatorClick('*')"
/>
<CalcButton
title="/"
@click="operatorClick('/')"
/>
</div>
<div>
<CalcButton
class="w-full"
title="="
@click="calc()"
/>
</div>
</div>
</div>
</template>
<script setup lang="ts">
const line = ref('0')
const stack = ref(0)
const operator = ref('')
function digitClick(digit: number) {
if(line.value == '0') {
line.value = ''
}
line.value = line.value + digit.toString()
}
function acClick() {
line.value = '0'
stack.value = 0
operator.value = ''
}
function cClick() {
line.value = line.value.substring(0, line.value.length - 1)
if(line.value == '') {
line.value = '0'
}
}
function operatorClick(value: string) {
stack.value = parseInt(line.value)
line.value = '0'
operator.value = value
}
function calc() {
switch(operator.value) {
case '+':
line.value = String(stack.value + parseInt(line.value))
break;
default:
break;
}
}
</script>