bryceio

Computercraft Calculator

Mar 7th, 2018 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.85 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1, 1)
  3.  
  4. fs.makeDir("temp")
  5.  
  6. createtemp = fs.open("/temp/calctemp", "w")
  7. createtemp.writeLine(" ")
  8. createtemp.close()
  9.  
  10. createtemp2 = fs.open("/temp/calctemp2", "w")
  11. createtemp2.writeLine(" ")
  12. createtemp2.close()
  13.  
  14. function editnum1(x)
  15.     readline = fs.open("/temp/calctemp", "r")
  16.     readnum1 = readline.readLine()
  17.     readline.close()
  18.     writenum1 = fs.open("/temp/calctemp", "w")
  19.     writenum1.write(readnum1..x)
  20.     writenum1.close()
  21. end
  22.  
  23. function editnum2(y)
  24.     readnum2 = fs.open("/temp/calctemp2", "r")
  25.     tempnum2 = readnum2.readLine()
  26.     readnum2.close()
  27.     writenum2 = fs.open("/temp/calctemp2", "w")
  28.     writenum2.write(tempnum2..y)
  29.     writenum2.close()
  30. end
  31.  
  32.     term.setTextColor(colors.yellow)
  33. print("7  8  9  +")
  34. print(" ")
  35. print("4  5  6  -")
  36. print(" ")
  37. print("1  2  3  *")
  38. print(" ")
  39. print(".  0  =  /")
  40.   decimal = "."
  41.   add = " + "
  42.   subtract = " - "
  43.   multiply = " * "
  44.   divide = " / "
  45.   finished = false
  46.   math = 0
  47. pos = 1
  48. term.setTextColor(1)
  49.     minus = "-"
  50. negative = false
  51. characters = 0
  52.  
  53. repeat
  54. char = -5
  55.  
  56.   local event, button, mouseX, mouseY = os.pullEvent("mouse_click")
  57.     if mouseX == 1 then
  58.       if mouseY == 1 then
  59.         char = 7
  60.       elseif mouseY == 3 then
  61.         char = 4
  62.       elseif mouseY == 5 then
  63.         char = 1
  64.       elseif mouseY == 7 then
  65.         char = 12
  66.       end
  67.     elseif mouseX ==  4 then
  68.       if mouseY == 1 then
  69.         char = 8
  70.       elseif mouseY == 3 then
  71.         char = 5
  72.       elseif mouseY == 5 then
  73.         char = 2
  74.       elseif mouseY == 7 then
  75.         char = 0
  76.       end
  77.     elseif mouseX == 7 then
  78.       if mouseY == 1 then
  79.         char = 9
  80.       elseif mouseY == 3 then
  81.         char = 6
  82.       elseif mouseY == 5 then
  83.         char = 3
  84.       elseif mouseY == 7 and math == 1 then
  85.         char = 14
  86.         if math == 1 then
  87.           finished = true
  88.         end
  89.       end
  90.     elseif mouseX == 10 and characters ~= 0 then
  91.       if mouseY == 1 then
  92.         if math == 0 then
  93.           math = -1
  94.         end
  95.       elseif mouseY == 3 then
  96.  --       if pos == 1 and math == 0 then
  97.  --         math = -5
  98.         if math == 0 then
  99.           math = -2
  100.         end
  101.       elseif mouseY == 5 then
  102.         if math == 0 then
  103.           math = -3
  104.         end
  105.       elseif mouseY == 7 then
  106.         if math == 0 then
  107.           math = -4
  108.         end
  109.       end
  110.     end
  111.  
  112. if char >=0 or math < 0 then
  113.     term.setCursorPos(pos, 9)
  114.     if math < 0 and math ~= -5 then
  115.             if math == -1 then
  116.                     print(add)
  117.                     maths = 1
  118.             elseif math == -2 then
  119.                     print(subtract)
  120.                     maths = 2
  121.             elseif math == -3 then
  122.                     print(multiply)
  123.                     maths = 3
  124.             elseif math == -4 then
  125.                     print(divide)
  126.                     maths = 4
  127.             end
  128.         math = 1
  129.         pos = pos + 2
  130.     elseif math == -5 then
  131.                     print("-")
  132.                     negative = true
  133.                     math = 0
  134.                     char = 13
  135.  
  136. elseif char >= 0 and char ~= 12 and char ~= 14 then
  137.     print(char)
  138.     characters = 1
  139.     if math == 0 and negative == false then
  140.         editnum1(char)
  141.     elseif math == 1 and negative == false then
  142.         editnum2(char)
  143.     elseif math == 0 and negative == true then
  144.         editnum1("-")
  145.         negative = false
  146.         finalnegative = true
  147.     end
  148.  
  149. elseif char == 12 and math == 0 then
  150.     print(decimal)
  151.     editnum1(".")
  152. elseif char == 12 and math == 1 then
  153.     print(decimal)
  154.     editnum2(".")
  155.  
  156. elseif char == 14 then
  157.     finished = true
  158.  
  159. end
  160. pos = pos + 1
  161. end
  162. until finished == true
  163.  
  164. finalread1 = fs.open("/temp/calctemp", "r")
  165. finalnum1 = finalread1.readLine()
  166. finalread1.close()
  167.  
  168. finalread2 = fs.open("/temp/calctemp2", "r")
  169. finalnum2 = finalread2.readLine()
  170. finalread2.close()
  171.  
  172. term.setCursorPos(1, 10)
  173. print("=")
  174.  
  175. if finalnum1 == nil or finalnum1 == " " then
  176.         print("Please input two numbers")
  177.         error()
  178. end
  179.  
  180. if finalnum2 == nil or finalnum2 == " " then
  181.         print("Please input two numbers.")
  182.         error()
  183. end
  184.  
  185. if maths == 4 and finalnum2*1 == 0.0 and finalnum1*1 ~= 0 then
  186.         for error = 1, 100 do
  187.             print("ERROR!!!!!"..math.random(1, 999999))
  188.             sleep(0.5)
  189.         end
  190.         error()
  191. elseif maths == 4 and finalnum2*1.0 == 0 and finalnum1*1 == 0.0 then
  192.         print("Imagine that you have zero cookies and you split them evenly among zero friends. How many cookies does each person get? See? It doesn't make sense. And Cookie Monster is sad that there are no cookies, and you are sad that you have no friends.")
  193.         error()    
  194. end
  195.  
  196.     if maths == 1 then
  197.             print(finalnum1 + finalnum2)
  198.     elseif maths == 2 then
  199.             print(finalnum1 - finalnum2)
  200.     elseif maths  == 3 then
  201.             print(finalnum1 * finalnum2)
  202.     elseif maths == 4 then
  203.             print(finalnum1 / finalnum2)
  204.     end
  205.  
  206.     if maths == 1  or maths == 2 and finalnum1*1.0 == 0 or finalnum2*1.0 == 0 then
  207.             print("That's some tough math.")
  208.     elseif maths == 3 or maths == 4 and finalnum1*1.0 == 1 or finalnum2*1.0 == 1 then
  209.             print("That's some tough math.")
  210.     end
  211.    
  212. fs.delete("/temp/calctemp")
  213. fs.delete("/temp/calctemp2")
Add Comment
Please, Sign In to add comment