gyro永不抽风

ああああああああああああああああおおおおおおおおおおおおおおおお

TI-nspire 式量计算

开发思路

  1. 使用on.charIn(c)on.backspaceKey()on.clearKey()on.enterKey()方法监听键盘,获取输入
  2. 页面完全使用gc绘制
  3. 计算
    • 拆分出大小写以及数字
    • 括号处理(不涉及嵌套)

代码

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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
local w = platform.window:width()
local h = platform.window:height()
local BOX_HEIGHT = 60
local BOX_WIDTH = 250
local input_text = ""
local output_text = ""

function on.paint(gc)

local titleText = "ChemCalc By Haoyun Qin (c) 2020"
local tTw = gc:getStringWidth(titleText)
gc:setColorRGB(0, 30, 120)
gc:drawString(titleText, (w - tTw) / 2, 0)
titleText = "Type in the chemical formula"
tTw = gc:getStringWidth(titleText)
gc:drawString(titleText, (w - tTw) / 2, 20)
titleText = "Then press Enter"
tTw = gc:getStringWidth(titleText)
gc:drawString(titleText, (w - tTw) / 2, 40)

gc:setColorRGB(255, 30, 30)
gc:setPen("medium", "smooth")
gc:drawRect((w - BOX_WIDTH) / 2, (h - BOX_HEIGHT) / 2, BOX_WIDTH, BOX_HEIGHT)

gc:setColorRGB(0, 0, 0)
gc:setFont("serif", "b", 16)
local itw = gc:getStringWidth(input_text)
local ith = gc:getStringHeight(input_text)
gc:drawString(input_text, (w - itw) / 2, (h - ith) / 2)
gc:setColorRGB(0, 100, 0)
gc:setFont("serif", "r", 16)
local otw = gc:getStringWidth(output_text)
gc:drawString(output_text, (w - otw) / 2, 150)
end

function on.charIn(char)
input_text = input_text..char
platform.window:invalidate()
end

function on.backspaceKey()
input_text = input_text:usub(0, -2)
platform.window:invalidate()
end

function on.clearKey()
input_text = ""
platform.window:invalidate()
end

function on.enterKey()
if (input_text == "") then
output_text = "Error: Input cannot be empty"
return
end
local c_arr = {}
local c_arr_copy = {}
local error_flag = false
local N = string.len(input_text)
for i = 1, N do
c_arr[i] = string.byte(input_text, i)
c_arr_copy[i] = string.char(c_arr[i])
end
local start_flag = 0
local ans_flag = 0
for i = 1, N do
if (string.byte(input_text, i) == string.byte('(')) then
start_flag = i
end
if (string.byte(input_text, i) == string.byte(')')) then
local tmp_mass = subMass(string.sub(input_text, start_flag + 1, i - 1))
if (tmp_mass < 0) then
error_flag = true
else
print(tmp_mass)
local tmp_num = 1
if (i + 1 <= N) then
if (c_arr[i + 1] >= 48 and c_arr[i + 1] <= 57) then
tmp_num = c_arr[i + 1] - 48
c_arr_copy[i + 1] = ""
if (i + 2 <= N) then
if (c_arr[i + 2] >= 48 and c_arr[i + 2] <= 57) then
tmp_num = tmp_num * 10 + c_arr[i + 2] - 48
c_arr_copy[i + 2] = ""
if (i + 3 <= N) then
if (c_arr[i + 3] >= 48 and c_arr[i + 3] <= 57) then
tmp_num = tmp_num * 10 + c_arr[i + 3] - 48
c_arr_copy[i + 3] = ""
end
end
end
end
end
end
print(tmp_num)
print(tmp_mass)
tmp_mass = tmp_mass * tmp_num
if (math.floor(tmp_mass) ~= tmp_mass) then
ans_flag = ans_flag + tmp_mass - math.floor(tmp_mass)
tmp_mass = math.floor(tmp_mass)
end
print(tmp_mass)
for k = start_flag, i do
c_arr_copy[k] = ''
end
c_arr_copy[i] = 'H'..tmp_mass
end
end
end
local working_text = ''
for i = 1, N do
working_text = working_text..c_arr_copy[i]
end
print(working_text)
N = string.len(working_text)
for i = 1, N do
c_arr[i] = string.byte(working_text, i)
end
local ans = 0
local last_upper = -100000
local last_lower = 0
local num = 0
local tmp_ans = 0
--- up: 1, down: 2, num: 3
local last_kind = 0
for i = 1, N do
local c = c_arr[i]

if (c >= 48 and c <= 57) then
if (last_kind == 0) then
error_flag = true
end

num = num * 10 + c - 48
last_kind = 3
end
if (c >= 65 and c <= 90) then
--- calculate ---
if (last_upper > 0) then
if (last_lower == 0) then
if (num == 0) then
ans = ans + getMass(string.char(last_upper))
else
ans = ans + getMass(string.char(last_upper)) * num
end
else
if (num == 0) then
ans = ans + getMass(string.char(last_upper, last_lower))
else
ans = ans + getMass(string.char(last_upper, last_lower)) * num
end
end
end
num = 0
last_upper = c
last_lower = 0
last_kind = 1
end
if (c >= 97 and c <= 122) then
if (last_kind == 0) then
error_flag = true
end
if (last_kind == 2 or last_kind == 3) then
error_flag = true
end
num = 0
last_lower = c
last_kind = 2
end
end
if (error_flag == false) then
if (last_lower == 0) then
if (num == 0) then
ans = ans + getMass(string.char(last_upper))
else
ans = ans + getMass(string.char(last_upper)) * num
end
else
if (num == 0) then
ans = ans + getMass(string.char(last_upper, last_lower))
else
ans = ans + getMass(string.char(last_upper, last_lower)) * num
end
end
end
ans = ans + ans_flag
if (error_flag == false) then
output_text = "M("..input_text..")="..ans
else
output_text = "Error: Wrong Format"
end
platform.window:invalidate()
end

function subMass(text)
local c_arr = {}
local N = string.len(text)
for i = 1, N do
c_arr[i] = string.byte(text, i)
end

local ans = 0
local last_upper = -100000
local last_lower = 0
local num = 0
local error_flag = false
--- up: 1, down: 2, num: 3
local last_kind = 0
for i = 1, N do
local c = c_arr[i]

if (c >= 48 and c <= 57) then
if (last_kind == 0) then
error_flag = true
end
num = num * 10 + c - 48

last_kind = 3
end
if (c >= 65 and c <= 90) then
--- calculate ---
if (last_upper > 0) then
if (last_lower == 0) then
if (num == 0) then
ans = ans + getMass(string.char(last_upper))
else
ans = ans + getMass(string.char(last_upper)) * num
end
else
if (num == 0) then
ans = ans + getMass(string.char(last_upper, last_lower))
else
ans = ans + getMass(string.char(last_upper, last_lower)) * num
end
end
end
num = 0
last_upper = c
last_lower = 0
last_kind = 1
end
if (c >= 97 and c <= 122) then
if (last_kind == 0) then
error_flag = true
end
if (last_kind == 2 or last_kind == 3) then
error_flag = true
end
num = 0
last_lower = c
last_kind = 2
end
end
if (error_flag == false) then
if (last_lower == 0) then
if (num == 0) then
ans = ans + getMass(string.char(last_upper))
else
ans = ans + getMass(string.char(last_upper)) * num
end
else
if (num == 0) then
ans = ans + getMass(string.char(last_upper, last_lower))
else
ans = ans + getMass(string.char(last_upper, last_lower)) * num
end
end
end
if (error_flag == false) then
return ans
else
return -1000000
end
platform.window:invalidate()
end

function getMass(c)
if (c == 'H') then
return 1
end
if (c == 'H') then
return 1
end
if (c == 'He') then
return 4
end
if (c == 'Li') then
return 7
end
if (c == 'Be') then
return 9
end
if (c == 'B') then
return 11
end
if (c == 'C') then
return 12
end
if (c == 'N') then
return 14
end
if (c == 'O') then
return 16
end
if (c == 'F') then
return 19
end
if (c == 'Ne') then
return 20
end
if (c == 'Na') then
return 23
end
if (c == 'Mg') then
return 24
end
if (c == 'Al') then
return 27
end
if (c == 'Si') then
return 28
end
if (c == 'P') then
return 31
end
if (c == 'S') then
return 32
end
if (c == 'Cl') then
return 35.5
end
if (c == 'Ar') then
return 40
end
if (c == 'K') then
return 39
end
if (c == 'Ca') then
return 40
end
if (c == 'Sc') then
return 45
end
if (c == 'Ti') then
return 48
end
if (c == 'V') then
return 51
end
if (c == 'Cr') then
return 52
end
if (c == 'Mn') then
return 55
end
if (c == 'Fe') then
return 56
end
if (c == 'Co') then
return 59
end
if (c == 'Ni') then
return 59
end
if (c == 'Cu') then
return 64
end
if (c == 'Zn') then
return 65
end
if (c == 'Ga') then
return 70
end
if (c == 'Ge') then
return 73
end
if (c == 'As') then
return 75
end
if (c == 'Se') then
return 79
end
if (c == 'Br') then
return 80
end
if (c == 'Kr') then
return 84
end
if (c == 'Rb') then
return 85
end
if (c == 'Sr') then
return 88
end
if (c == 'Y') then
return 89
end
if (c == 'Zr') then
return 91
end
if (c == 'Nb') then
return 93
end
if (c == 'Mo') then
return 96
end
if (c == 'Tc') then
return 99
end
if (c == 'Ru') then
return 101
end
if (c == 'Rh') then
return 103
end
if (c == 'Pd') then
return 106
end
if (c == 'Ag') then
return 108
end
if (c == 'Cd') then
return 112
end
if (c == 'In') then
return 115
end
if (c == 'Sn') then
return 119
end
if (c == 'Sb') then
return 122
end
if (c == 'Te') then
return 128
end
if (c == 'I') then
return 127
end
if (c == 'Xe') then
return 131
end
if (c == 'Cs') then
return 133
end
if (c == 'Ba') then
return 137
end
if (c == 'W') then
return 184
end
if (c == 'Ir') then
return 192
end
if (c == 'Pt') then
return 195
end
if (c == 'Au') then
return 197
end
if (c == 'Hg') then
return 201
end
if (c == 'Tl') then
return 204
end
if (c == 'Pb') then
return 207
end
if (c == 'Bi') then
return 209
end
if (c == 'Po') then
return 209
end
if (c == 'At') then
return 201
end
if (c == 'Ac') then
return 59
end
end

第一次写Lua而且急于求成,代码质量很差(flag满天飞),勿喷

反思总结

  1. 计算部分太冗长了,其实可以调用内部math.eval()方法来进行计算,先分割出一个一个元素,然后将数字与元素之间用加号、乘号连接,元素替换为数字,然后执行该方法会非常简单
  2. 输入有点简陋(但是毕竟快),可以使用mathbox(教程中有提到)
__EOF__
-------------本文结束感谢您的阅读-------------

本文标题:TI-nspire 式量计算

文章作者:gyro永不抽风

发布时间:2020年03月22日 - 16:03

最后更新:2020年09月15日 - 10:09

原始链接:http://gyrojeff.moe/2020/03/22/TI-nspire-%E5%BC%8F%E9%87%8F%E8%AE%A1%E7%AE%97/

许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 转载请保留原文链接及作者!

真的不买杯奶茶吗?T^T

欢迎关注我的其它发布渠道