Advertisement
Guest User

calctest

a guest
Feb 18th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. os.loadAPI("button")
  2. m = peripheral.wrap ("top")
  3. m.clear()
  4.  
  5. function centerText(text)
  6.   w, h = m.getSize()
  7.   m.setCursorPos(math.floor(w/2-#text/2+.5), h/2+.5)
  8.   m.write(text)
  9.   m.scroll(1)
  10.  
  11. end
  12. m.setCursorBlink(false)
  13. centerText("Hello World!")
  14. os.sleep(.15)
  15. m.clear()
  16. button.heading("Calculatron 9000")
  17. m.setCursorBlink(true)
  18. usenmst1 = true
  19. cng = 1
  20. cng2 = 1
  21. numset2= {}
  22. numset1= {}
  23. for i = 1,100 do
  24.   numset1[i] = ""
  25.   numset2[i] = ""
  26. end
  27. oppcanhap = false
  28. shouldclear = false
  29.  
  30.  
  31. function numPad()
  32.   button.setTable("C",clear,12,15,22,24)
  33.   button.setTable("1", one, 2,5,10,12)
  34.   button.setTable("2", two, 7,10,10,12)
  35.   button.setTable("3", three, 12,15,10,12)
  36.   button.setTable("4", four, 2,5,14,16)
  37.   button.setTable("5", five, 7,10,14,16)
  38.   button.setTable("6", six, 12,15,14,16)
  39.   button.setTable("7", seven, 2,5,18,20)
  40.   button.setTable("8", eight, 7,10,18,20)
  41.   button.setTable("9", nine, 12,15,18,20)
  42.   button.setTable("0", zero, 7,10,22,24)
  43.   button.setTable("+", add, 17,20,10,12)
  44.   button.setTable("-", sub, 17,20,14,16)
  45.   button.setTable("*", multi, 17,20,18,20)
  46.   button.setTable("/",div,17,20,22,24)
  47.   button.setTable("=",equal,2,5,22,24)
  48.   button.setTable("exit", exit, 25,29,22,24)
  49.   button.setTable("crash", crash , 24,29,18,20)
  50.  
  51.   button.screen()
  52. end
  53.  
  54. function getClick()
  55.   event,side,x,y=os.pullEvent("monitor_touch")
  56.   button.checkxy(x,y)
  57. end
  58. function clear()
  59.   button.flash("C")
  60.   clearline()
  61. end
  62. function clearline()
  63. oppcanhap=false
  64. usenmst1= true
  65. shouldclear=false
  66.   i = 0
  67.   for i = 0,30 do
  68.   i= i+1
  69.    m.setCursorPos(i,5)
  70.     m.write(" ")
  71.     cng = 1
  72.   end
  73.   for i = 1,100 do
  74.     numset1[i]=""
  75.     numset2[i]=""
  76.   end
  77.  m.setCursorPos(4,5)
  78.  cng = 1
  79.  cng2 = 1
  80. end
  81. function emptyLine()
  82.   for i = 0,30 do
  83.     i = i+1
  84.     m.setCursorPos(i,5)
  85.     m.write(" ")
  86.     cng = 1
  87.     cng2 = 1
  88.   end
  89. end
  90. function exit()
  91.   button.toggleButton("exit")
  92.   os.reboot()
  93. end
  94. function setNum(nm)
  95.   button.flash(nm)
  96.   if shouldclear==true then
  97.     clearline()
  98.   end
  99.   if usenmst1 == true then
  100.     numset1[cng] =nm
  101.     m.setCursorPos(5+cng,5)
  102.     cng = cng+1
  103.     print(usenmst1)
  104.     m.write(nm)
  105.     oppcanhap=true
  106.   elseif usenmst1==false then
  107.     numset2[cng2]=nm
  108.     m.setCursorPos(5+cng2,5)
  109.     cng2=cng2+1
  110.     print(usenmst1)
  111.     m.write(nm)
  112.   end
  113. end  
  114. function one()
  115.   setNum("1")  
  116. end
  117. function two()
  118.    setNum("2")    
  119. end
  120. function three()
  121.   setNum("3")
  122. end
  123. function four()
  124.   setNum("4")  
  125. end
  126. function five()
  127.   setNum("5")
  128. end
  129. function six()
  130.    setNum("6")
  131. end
  132. function seven()
  133.   setNum("7")
  134. end
  135. function eight()
  136.   setNum("8")  
  137. end
  138. function nine()
  139.   setNum("9")
  140. end
  141. function zero()
  142.   setNum("10")
  143. end
  144. function equal()
  145. button.flash("=")
  146.     if opptype == "add" then
  147.         nst2 = table.concat(numset2)
  148.   nst1 = table.concat(numset1)
  149.         opt = nst1 + nst2
  150.         printOut(opt)
  151.     print(nst1)
  152.  print(nst2)
  153.     elseif opptype == "subtract" then
  154.   nst2 = table.concat(numset2)
  155.   nst1 = table.concat(numset1)
  156.   opt = nst1-nst2
  157.   printOut(opt)
  158.     elseif opptype == "multiply" then
  159.   nst2 = table.concat(numset2)
  160.   nst1 = table.concat(numset1)
  161.   opt = nst1*nst2
  162.   printOut(opt)
  163.     elseif opptype == "divide" then
  164.   nst2 = table.concat(numset2)
  165.   nst1 = table.concat(numset1)
  166.   opt = nst1/nst1
  167.   printOut(opt)
  168.     else
  169.   clearLine()
  170.   print(Err)
  171.   sleep(2)
  172.   clearLine()
  173.     end
  174.  shouldclear=true
  175. end
  176. function printOut(opt)
  177. clearline()
  178. m.write(opt)
  179. end
  180. function add()
  181.   if oppcanhap == true then
  182.    button.flash("+")
  183.   opptype = "add"
  184.   usenmst1=false
  185.   emptyLine()
  186.   end
  187. end
  188. function sub()
  189.   if oppcanhap==true then
  190.     button.flash("-")
  191.     opptype = "subtract"
  192.     usenmst1=false
  193.     emptyLine()
  194.   end
  195. end
  196. function multi()
  197.   if oppcanhap==true then
  198.     button.flash("*")
  199.     opptype="multiply"
  200.     usenmst1=false
  201.     emptyLine()
  202.   end
  203. end
  204. function div()
  205.   if oppcanhap==true then
  206.     button.flash("/")
  207.     opptype="divide"
  208.     usenmst1= false
  209.     emptyLine()
  210.   end
  211. end
  212. numPad()
  213. m.setCursorPos(5,5)
  214.  
  215. while true do
  216.  getClick()
  217. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement