Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5205 clevermous 1
-- simple calculator on LUA for KolibriOS
2
 
3
--init global variables
4
disp=0 --display
5
stack=0 --stack
6
will=0 --operation
7
 
8
function drawwin()
9
--here we draw window
10
paintstart()
11
--begin redraw
12
window(10,10,153,180,65069280)
13
--define window
14
textout(3,13,0,"LuaCalc")
15
--print title
16
textout(6, 30, 0, disp)
17
--and display
18
 
19
--then we need make buttons and print labels of buttons
20
makebutton(6,60,20,20,17,13619151)
21
textout(9,63,0,"7")
22
makebutton(6,90,20,20,14,13619151)
23
textout(9,93,0,"4")
24
makebutton(6,120,20,20,11,13619151)
25
textout(9,123,0,"1")
26
makebutton(6,150,50,20,10,13619151)
27
textout(9,153,0,"0")
28
 
29
 
30
makebutton(36,60,20,20,18,13619151)
31
textout(39,63,0,"8")
32
makebutton(36,90,20,20,15,13619151)
33
textout(39,93,0,"5")
34
makebutton(36,120,20,20,12,13619151)
35
textout(39,123,0,"2")
36
 
37
 
38
makebutton(66,60,20,20,19,13619151)
39
textout(69,63,0,"9")
40
makebutton(66,90,20,20,16,13619151)
41
textout(69,93,0,"6")
42
makebutton(66,120,20,20,13,13619151)
43
textout(69,123,0,"3")
44
 
45
 
46
makebutton(96,60,20,20,20,13619151)
47
textout(99,63,0,"*")
48
makebutton(96,90,20,20,21,13619151)
49
textout(99,93,0,"/")
50
makebutton(96,120,20,20,22,13619151)
51
textout(99,123,0,"-")
52
makebutton(96,150,20,20,23,13619151)
53
textout(99,153,0,"+")
54
 
55
makebutton(126,60,20,20,30,13619151)
56
textout(129,63,0,"C")
57
makebutton(126,90,20,20,31,13619151)
58
textout(129,93,0,"CE")
59
makebutton(126,120,20,50,32,13619151)
60
textout(129,123,0,"=")
61
 
62
paintend()
63
--and finish redraw
64
end
65
 
66
 
67
--main loop
68
while 1==1 do --loop until exit
69
	event=waitevent() --check the event
70
	if event==1 then drawwin() end --redraw needed
71
	if event==2 then key=getkey()  end --get keyboard scancode
72
	if event==3 then button=getbutton() --button pressed
73
		if button==1 then sysexit() end --close button
74
		if button==10 then disp=disp*10 end --numerical buttons - 0
75
		if button==11 then disp=disp*10+1 end --1
76
		if button==12 then disp=disp*10+2 end --2
77
		if button==13 then disp=disp*10+3 end --3
78
		if button==14 then disp=disp*10+4 end
79
		if button==15 then disp=disp*10+5 end
80
		if button==16 then disp=disp*10+6 end
81
		if button==17 then disp=disp*10+7 end
82
		if button==18 then disp=disp*10+8 end
83
		if button==19 then disp=disp*10+9 end --and 9
84
		if button==30 then disp=0
85
		stack=0 end  -- C button - clear disp and memory
86
		if button==31 then disp=0 end -- CE button - clear disp
87
		if button==20 then will=1
88
		stack=disp
89
		disp=0
90
		end -- next is multiple
91
		if button==21 then will=2
92
		stack=disp
93
		disp=0
94
		end --next is divison
95
		if button==22 then will=3
96
		stack=disp
97
		disp=0
98
		end -- next is substraction
99
		if button==23 then will=4
100
		stack=disp
101
		disp=0
102
		end -- next is addition
103
		if button==32 then --evalute
104
		if will==1 then disp=stack*disp end
105
		if will==2 then disp=stack/disp end
106
		if will==3 then disp=stack-disp end
107
		if will==4 then disp=stack+disp end
108
		end
109
		drawwin() --redraw... we need it after pressing buttons
110
	end
111
end