Martmists

Calculator v3 beta

Apr 30th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.96 KB | None | 0 0
  1. shell.run("bg")
  2. term.clear()
  3. --button.clearTable()
  4. shell.run("monitor","monitor_0","clear")
  5. term.clear()
  6. os.loadAPI("button")
  7. term.setCursorPos(1,1)
  8. m = peripheral.wrap("monitor_0")
  9. m.clear()
  10. num1 = -1
  11. num2 = -1
  12. misc = " "
  13.  
  14. function fillTable()
  15.   button.setTable("1",one,1,3,1,3)
  16.   button.setTable("2",two,5,7,1,3)
  17.   button.setTable("3",three,9,11,1,3)
  18.   button.setTable("4",four,1,3,5,7)
  19.   button.setTable("5",five,5,7,5,7)
  20.   button.setTable("6",six,9,11,5,7)
  21.   button.setTable("7",seven,1,3,9,11)
  22.   button.setTable("8",eight,5,7,9,11)
  23.   button.setTable("9",nine,9,11,9,11)
  24.   button.setTable("0",zero,13,15,9,11)
  25.   button.setTable("/",divide,13,15,1,3)
  26.   button.setTable("*",multiply,13,15,5,7)
  27.   button.setTable("+",add,17,19,1,3)
  28.   button.setTable("-",subtract,17,19,5,7)
  29.   button.setTable("=",calc,17,19,9,11)
  30.   button.setTable("Ans",ans,21,25,1,3)
  31.   button.setTable("^",power,21,23,5,7)
  32.   button.setTable("C",clear,21,23,9,11)
  33.   button.setTable(" ",none,1000,1000,1000,1000)
  34.   button.setTable("(-)",toglneg,27,31,1,3)
  35.   button.screen()
  36. end
  37.  
  38. neg = false
  39. function toglneg()
  40.   if neg then
  41.     neg = false
  42.   else
  43.     neg = true
  44.   end
  45. end
  46.  
  47. function getClick()
  48.   event,side,x,y = os.pullEvent("monitor_touch")
  49.   button.checkxy(x,y)
  50. end
  51.  
  52. function one()
  53.   button.flash("1")
  54.   if misc == " " then
  55.     if num1 == -1 then
  56.       num1 = 1
  57.     else
  58.       num1 = num1 * 10
  59.       sleep(0.1)
  60.       if neg then
  61.         num1 = num1 - 1
  62.       else
  63.     num1 = num1 + 1
  64.       end
  65.     end
  66.   else
  67.     if num2 == -1 then
  68.       num2 = 1
  69.     else
  70.       num2 = num2 * 10
  71.       sleep(0.1)
  72.       num2 = num2 + 1
  73.     end
  74.   end
  75. end
  76.  
  77. function two()
  78.   button.flash("2")
  79.   if misc == " " then
  80.     if num1 == -1 then
  81.       num1 = 2
  82.     else
  83.       num1 = num1 * 10
  84.       sleep(0.1)
  85.       num1 = num1 + 2
  86.     end
  87.   else
  88.     if num2 == -1 then
  89.       num2 = 2
  90.     else
  91.       num2 = num2 * 10
  92.       sleep(0.1)
  93.       num2 = num2 + 2
  94.     end
  95.   end
  96. end
  97.  
  98. function three()
  99.   button.flash("3")
  100.   if misc == " " then
  101.     if num1 == -1 then
  102.       num1 = 3
  103.     else
  104.     num1 = num1 * 10
  105.     sleep(0.1)
  106.     num1 = num1 + 3
  107.     end
  108.   else
  109.     if num2 == -1 then
  110.       num2 = 3
  111.     else
  112.       num2 = num2 * 10
  113.       sleep(0.1)
  114.       num2 = num2 + 3
  115.     end
  116.   end
  117. end  
  118.  
  119. function four()
  120.   button.flash("4")
  121.   if misc == " " then
  122.     if num1 == -1 then
  123.       num1 = 4
  124.     else
  125.     num1 = num1 * 10
  126.     sleep(0.1)
  127.     num1 = num1 + 4
  128.     end
  129.   else
  130.     if num2 == -1 then
  131.       num2 = 4
  132.     else
  133.       num2 = num2 * 10
  134.       sleep(0.1)
  135.       num2 = num2 + 4
  136.     end
  137.   end
  138. end  
  139.  
  140. function five()
  141.   button.flash("5")
  142.   if misc == " " then
  143.     if num1 == -1 then
  144.       num1 = 5
  145.     else
  146.     num1 = num1 * 10
  147.     sleep(0.1)
  148.     num1 = num1 + 5
  149.     end
  150.   else
  151.     if num2 == -1 then
  152.       num2 = 5
  153.     else
  154.       num2 = num2 * 10
  155.       sleep(0.1)
  156.       num2 = num2 + 5
  157.     end
  158.   end
  159. end  
  160.  
  161. function six()
  162.   button.flash("6")
  163.   if misc == " " then
  164.     if num1 == -1 then
  165.       num1 = 6
  166.     else
  167.     num1 = num1 * 10
  168.     sleep(0.1)
  169.     num1 = num1 + 6
  170.     end
  171.   else
  172.     if num2 == -1 then
  173.       num2 = 6
  174.     else
  175.       num2 = num2 * 10
  176.       sleep(0.1)
  177.       num2 = num2 + 6
  178.     end
  179.   end
  180. end  
  181.  
  182. function seven()
  183.   button.flash("7")
  184.   if misc == " " then
  185.     if num1 == -1 then
  186.       num1 = 7
  187.     else
  188.     num1 = num1 * 10
  189.     sleep(0.1)
  190.     num1 = num1 + 7
  191.     end
  192.   else
  193.     if num2 == -1 then
  194.       num2 = 7
  195.     else
  196.       num2 = num2 * 10
  197.       sleep(0.1)
  198.       num2 = num2 + 7
  199.     end
  200.   end
  201. end  
  202.  
  203. function eight()
  204.   button.flash("8")
  205.   if misc == " " then
  206.     if num1 == -1 then
  207.       num1 = 8
  208.     else
  209.     num1 = num1 * 10
  210.     sleep(0.1)
  211.     num1 = num1 + 8
  212.     end
  213.   else
  214.     if num2 == -1 then
  215.       num2 = 8
  216.     else
  217.       num2 = num2 * 10
  218.       sleep(0.1)
  219.       num2 = num2 + 8
  220.     end
  221.   end
  222. end  
  223.  
  224. function nine()
  225.   button.flash("9")
  226.   if misc == " " then
  227.     if num1 == -1 then
  228.       num1 = 9
  229.     else
  230.     num1 = num1 * 10
  231.     sleep(0.1)
  232.     num1 = num1 + 9
  233.     end
  234.   else
  235.     if num2 == -1 then
  236.       num2 = 9
  237.     else
  238.       num2 = num2 * 10
  239.       sleep(0.1)
  240.       num2 = num2 + 9
  241.     end
  242.   end
  243. end  
  244.  
  245. function zero()
  246.   button.flash("0")
  247.   if misc == " " then
  248.     if num1 == -1 then
  249.       num1 = 0
  250.     else
  251.     num1 = num1 * 10
  252.     sleep(0.1)
  253.     num1 = num1 + 0
  254.     end
  255.   else
  256.     if num2 == -1 then
  257.       num2 = 0
  258.     else
  259.       num2 = num2 * 10
  260.       sleep(0.1)
  261.       num2 = num2 + 0
  262.     end
  263.   end
  264. end  
  265.  
  266.  
  267.  
  268. function add()
  269.   if misc == " " then
  270.     button.toggleButton("+")
  271.     misc = "+"
  272.   elseif misc == "+" then
  273.     button.toggleButton("+")
  274.     misc = " "
  275.   else
  276.     button.toggleButton("+")
  277.     button.toggleButton(misc)
  278.     misc = "+"
  279.   end
  280. end
  281.  
  282. function subtract()
  283.   if misc == " " then
  284.     button.toggleButton("-")
  285.     misc = "-"
  286.   elseif misc == "-" then
  287.     button.toggleButton("-")
  288.     misc = " "
  289.   else
  290.     button.toggleButton("-")
  291.     button.toggleButton(misc)
  292.     misc = "-"
  293.   end
  294. end
  295.  
  296. function divide()
  297.   if misc == " " then
  298.     button.toggleButton("/")
  299.     misc = "/"
  300.   elseif misc == "/" then
  301.     button.toggleButton("/")
  302.     misc = " "
  303.   else
  304.     button.toggleButton("/")
  305.     button.toggleButton(misc)
  306.     misc = "/"
  307.   end
  308. end
  309.  
  310. function multiply()
  311.   if misc == " " then
  312.     button.toggleButton("*")
  313.     misc = "*"
  314.   elseif misc == "*" then
  315.     button.toggleButton("*")
  316.     misc = " "
  317.   else
  318.     button.toggleButton("*")
  319.     button.toggleButton(misc)
  320.     misc = "*"
  321.   end
  322. end
  323.  
  324. function power()
  325.   if misc == " " then
  326.     button.toggleButton("^")
  327.     misc = "^"
  328.   elseif misc == "^" then
  329.     button.toggleButton("^")
  330.     misc = " "
  331.   else
  332.     button.toggleButton("^")
  333.     button.toggleButton(misc)
  334.     misc = "^"
  335.   end
  336. end
  337.  
  338. function none()
  339.  
  340. end
  341.  
  342. function calc()
  343.   button.toggleButton(misc)
  344.   button.flash("=")
  345.   if misc == " " then
  346.     print("Invalid.")
  347.     m.setCursorPos(27,9)
  348.     m.write("Invalid.")
  349.   else
  350.     if misc == "/" then
  351.       ans = math.abs(num1/num2)
  352.     elseif misc == "*" then
  353.       ans = math.abs(num1*num2)
  354.     elseif misc == "+" then
  355.       ans = math.abs(num1+num2)
  356.     elseif misc == "-" then
  357.       ans = math.abs(num1-num2)
  358.     else
  359.       ans = math.abs(num1^num2)
  360.     end
  361.  m.setCursorPos(27,9)
  362.  m.write(ans)
  363.   end
  364. misc = " "
  365. sleep(0.5)
  366. end
  367.  
  368. function clear()
  369.   button.flash("C")
  370.   shell.run("reboot")
  371. end  
  372.  
  373. function ans()
  374.   if misc == " " then
  375.     num1 = ans
  376.   else
  377.     num2 = ans
  378.   end
  379. end  
  380. fillTable()
  381. button.heading("")
  382. button.label(1,1,"")
  383. ans = " "
  384. --m.setCursorPos(24,1)
  385.  
  386. while true do
  387.   getClick()
  388.   term.clear()
  389.   term.setCursorPos(1,1)
  390.   print(num1)
  391.   print(misc)
  392.   print(num2)
  393.   print("=")
  394.   print(ans)
  395.   m.setCursorPos(27,1)
  396.   m.write(num1)
  397.   m.setCursorPos(27,3)
  398.   m.write(misc)
  399.   m.setCursorPos(27,5)
  400.   m.write(num2)
  401.   m.setCursorPos(27,7)
  402.   m.write("=")
  403.   --m.setCursorPos(27,9)
  404.   --m.write(ans)
  405. end
Advertisement
Add Comment
Please, Sign In to add comment