os.loadAPI('h2touchpanel')
local result=0
local numsave={}
local symsave={}
local count=0
local buttons = {}
buttons[1] = {name = '1', cmd = function() pass(1) end }
buttons[2] = {name = '2', cmd = function() pass(2) end }
buttons[3] = {name = '3', cmd = function() pass(3) end }
buttons[4] = {name = '4', cmd = function() pass(4) end }
buttons[5] = {name = '5', cmd = function() pass(5) end }
buttons[6] = {name = '6', cmd = function() pass(6) end }
buttons[7] = {name = '7', cmd = function() pass(7) end }
buttons[8] = {name = '8', cmd = function() pass(8) end }
buttons[9] = {name = '9', cmd = function() pass(9) end }
buttons[10] = {name = '0', cmd = function() pass(0) end }
buttons[11] = {name = '+', cmd = function() symbol('+') end }
buttons[12] = {name = '-', cmd = function() symbol('-') end }
buttons[13] = {name = '×', cmd = function() symbol('×') end }
buttons[14] = {name = '÷', cmd = function() symbol('÷') end }
buttons[15] = {name = '=', cmd = function() enter() end }
function pass(num)
numsave[#numsave+1]=num
--押した順番の通りに数字が配列numsave[]の中に保存される。
--例:) 1番目:1 2番目:2 3番目:4 ...
count=count+1
--カウントが一つ増える。
end
function symbol(name)
symsave[#symsave+1]= name
--押した順番通りの記号がsymsave[]の中に保存される。
--例:) 1番目:+ 2番目:- 3番目:÷ ...
count=count+1
--カウントが一つ増える。
end
function enter()
result=numsave[1]
--最初の数字を変数resultに入れておく。
local value
--変数valueの定義
for i=1,(count-1)/2 do
--1+1と入力したときcountは3となり、正しい計算をボタンで入力すると奇数になるので「-1」し、
--1+1は計算回数1回(count = 3)、1+1+1は計算回数2回(count = 5)となるのでそれぞれ÷2をする。
value=result
--最初の数字を変数valueに入れておく。
if symsave[i]=='+' then
result=result+numsave[i+1]
elseif symsave[i]=='-' then
result=result-numsave[i+1]
elseif symsave[i]=='×' then
result=result*numsave[i+1]
elseif symsave[i]=='÷' then
result=result/numsave[i+1]
end
print(value,symsave[i],numsave[i+1],'=',result)
--最初の数、計算記号、次の数、=、今回の計算結果が表示される。
end
print(result,' end')
result = 0
numsave = {}
symsave = {}
count = 0
end
local monitor = peripheral.wrap('top')
local option = {topPos = 1, bottomPos = 10}
local panel = h2touchpanel.makePanel(buttons, 3, 5, monitor, option)
panel:draw()
while true do
local event, btn = panel:pullButtonPushEvent()
btn:run()
panel:drawPushedButtonEffect(btn, 0.5)
end