-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1358.bas
More file actions
290 lines (265 loc) · 6.03 KB
/
Copy path1358.bas
File metadata and controls
290 lines (265 loc) · 6.03 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'app-plug-in
'menu Calculator
'
' $Id: calc.bas,v 1.5 2005-05-11 23:30:14 zeeb90au Exp $
' calc.bas Copyright (c) Chris Warren-Smith April 2005
' Demonstration for html user interfaces
'
' Version 2.0
'
' This program is distributed under the terms of the GPL v2.0 or later
' Download the GNU Public License (GPL) from www.gnu.org
'
'
option predef grmode 390X290
out_x = 0
out_y = 0
func compute(byref operator, lval, rval)
local result = 0
if (operator = "+") then
result = lval + rval
elseif (operator = "-") then
result = lval - rval
elseif (operator = "/") then
if (rval == 0) then
rval = 1
fi
result = lval / rval
elseif (operator = "*") then
result = lval * rval
elseif (lval > 0) then
result = lval
else
result = rval
fi
compute = result
end
' evaluate the expression string
func eval(byref expr)
local strl,num,pt,result,operator,inner,endb,i,j,nbracket
strl = len(expr)
num = 0
pt = 0
result = 0
operator = ""
for i = 0 to strl
if (i < strl)
ch = mid(expr, i+1, 1)
else
' end of last operand
eval = compute(operator, result, num)
exit func
fi
if ch = "." then
pt = 1
elseif isnumber(ch) then
if pt > 0 then
num = num + (ch/(10^pt))
pt++
else
num = num*10+ch
fi
else
' non number
if ch = "(" then
' find the matching )
if (len(operator) = 0) then
' short form notation eg 3(1+1)
operator = "*"
if (result = 0) then
result = num
else
result = compute(operator, result, num)
fi
else
' complete the previous expression
result = compute(operator, result, num)
fi
nbracket = 1
endb = 0
i++
for j = i to strl-1
ch = mid(expr, j+1, 1)
if ch = "(" then
nbracket++
elseif ch = ")" then
nbracket--
if nbracket = 0 then
endb = j
exit for
fi
fi
next j
if (endb = 0) then
eval = "ERR"
exit func
endif
inner = mid(expr, i+1, endb-i)
if len(operator) = 0 then
result = eval(inner)
else
num = eval(inner)
result = compute(operator, result, num)
fi
i = endb
if (i+1 = strl) then
' nothing more to process
eval = result
exit func
fi
' scan for next operator
operator = ""
elseif ch = ")" then
eval = result
exit func
else
' operator character
if (len(operator) = 0) then
' first operand
result = num
else
' resolve the current expression
result = compute(operator, result, num)
endif
operator = ch
fi
' scan for next number
num = 0
pt = 0
fi
next i
eval = result
end
sub mk_bn(byref f, fgc, bgc, x, y, w, h, caption)
local button
button.x = x
button.y = y
button.width = w
button.height = h
button.label = caption
button.color = fgc
button.backgroundColor = bgc
f.inputs << button
end
func createForm
local w = 60
local h = 60
local y = 40
local x = 10
local fcb = 3
local ncb = 4
local ocb = 5
local fcf = 1
local ncf = 15
local ocf = 7
out_x = x + 3
out_y = 5
mk_bn(f, fcf, fcb, x, y, w, h, "MC")
mk_bn(f, ncf, ncb, -1, y, w, h, "7")
mk_bn(f, ncf, ncb, -1, y, w, h, "8")
mk_bn(f, ncf, ncb, -1, y, w, h, "9")
mk_bn(f, ocf, ocb, -1, y, w, h, "+")
mk_bn(f, ocf, ocb, -1, y, w, h, "-")
y += h
mk_bn(f, fcf, fcb, x, y, w, h, "MR")
mk_bn(f, ncf, ncb, -1, y, w, h, "4")
mk_bn(f, ncf, ncb, -1, y, w, h, "5")
mk_bn(f, ncf, ncb, -1, y, w, h, "6")
mk_bn(f, ocf, ocb, -1, y, w, h, "*")
mk_bn(f, ocf, ocb, -1, y, w, h, "/")
y += h
mk_bn(f, fcf, fcb, x, y, w, h, "MS")
mk_bn(f, ncf, ncb, -1, y, w, h, "1")
mk_bn(f, ncf, ncb, -1, y, w, h, "2")
mk_bn(f, ncf, ncb, -1, y, w, h, "3")
mk_bn(f, ocf, ocb, -1, y, w, h, "(")
mk_bn(f, ocf, ocb, -1, y, w, h, ")")
y += h
mk_bn(f, fcf, fcb, x, y, w, h, "EXIT")
mk_bn(f, ncf, ncb, -1, y, w, h, "0")
mk_bn(f, ncf, ncb, -1, y, w, h, ".")
mk_bn(f, fcf, fcb, -1, y, w, h, "BS")
mk_bn(f, fcf, fcb, -1, y, w, h, "CE")
mk_bn(f, ocf, ocb, -1, y, w, h, "=")
f = form(f)
createForm = f
end
func showResult(result)
local bgnd = 3
local w = 362
local h = 30
rect out_x, out_y STEP w, h, COLOR bgnd FILLED
rect out_x - 1, out_y - 1 STEP w + 1, h + 1, COLOR 2
local out_str = chr(27) + "[15 C" + result
local out_str_w = textwidth(result)
local x = (out_x + w) - out_str_w - textwidth("0")
if (x < out_x) then
x = out_x
out_str = " ERR"
fi
color 15, bgnd
at x, out_y + 7
print cat(1) + out_str
end
sub main
local result = "0"
local mem = ""
rect 0, 0, xmax, ymax, 1 FILLED
f = createForm
showResult result
while 1
f.doEvents()
k = inkey
form_var = f.value
if (len(k) == 1) then
if (asc(k) == 8) then
form_var = "BS"
else if (asc(k) == 127) then
form_var = "CE"
else
form_var = k
fi
fi
select case form_var
case "CE"
' clear all
result = "0"
case "BS"
' back space
if len(result) > 1 then
result = left(result, len(result)-1)
else
result = "0"
fi
case "MC"
mem = ""
case "MR"
if (mem != "") then
result = mem
fi
case "MS"
mem = result
case "EXIT"
exit loop
case "."
if len(result) > 0 && right(result, 1) != "." then
result += "."
fi
case "="
if (instr(result, "=") == 0) then
result = result + form_var + eval(result)
fi
case else
' expression input
if (result = "0") then
result = ""
fi
if (instr(result, "=") > 0) then
result = ""
fi
result = result + form_var
end select
showResult result
wend
end
main