Martmists

Calculator

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