-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path15_calculator.ring
More file actions
98 lines (87 loc) · 1.93 KB
/
Copy path15_calculator.ring
File metadata and controls
98 lines (87 loc) · 1.93 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
load "slint.ring"
cDisplay = "0"
cPending = ""
nOperand = 0
bNewNumber = true
oApp = new SlintApp {
loadUI("15_calculator.slint")
setCallback("digit", :onDigit)
setCallback("operator", :onOperator)
setCallback("equals", :onEquals)
setCallback("clear", :onClear)
setCallback("decimal", :onDecimal)
setCallback("negate", :onNegate)
setCallback("percent", :onPercent)
show()
run()
}
func onDigit
cDigit = oApp.callbackArg(1)
if bNewNumber
cDisplay = cDigit
bNewNumber = false
else
if cDisplay = "0"
cDisplay = cDigit
else
cDisplay += cDigit
ok
ok
updateDisplay()
func onOperator
cOp = oApp.callbackArg(1)
if cPending != ""
calculate()
ok
nOperand = number(cDisplay)
cPending = cOp
bNewNumber = true
func onEquals
if cPending != ""
calculate()
cPending = ""
ok
func onClear
cDisplay = "0"
cPending = ""
nOperand = 0
bNewNumber = true
updateDisplay()
func onDecimal
if bNewNumber
cDisplay = "0."
bNewNumber = false
else
if not substr(cDisplay, ".")
cDisplay += "."
ok
ok
updateDisplay()
func onNegate
nVal = number(cDisplay)
cDisplay = "" + (-nVal)
updateDisplay()
func onPercent
nVal = number(cDisplay)
cDisplay = "" + (nVal / 100)
updateDisplay()
func calculate
nCurrent = number(cDisplay)
switch cPending
on "+"
cDisplay = "" + (nOperand + nCurrent)
on "-"
cDisplay = "" + (nOperand - nCurrent)
on "*"
cDisplay = "" + (nOperand * nCurrent)
on "/"
if nCurrent != 0
cDisplay = "" + (nOperand / nCurrent)
else
cDisplay = "Error"
ok
off
bNewNumber = true
updateDisplay()
func updateDisplay
oApp.setString("display", cDisplay)