Advertisement
bryceio

ComputerCraft Calculator V2

Mar 7th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.55 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.   mathf = 0
  47.   maths = 0
  48. pos = 1
  49. term.setTextColor(1)
  50.     minus = "-"
  51. negative = false
  52. characters = 0
  53.  
  54. repeat
  55. char = -5
  56.  
  57.   local event, button, mouseX, mouseY = os.pullEvent("mouse_click")
  58.     if mouseX == 1 then
  59.       if mouseY == 1 then
  60.         char = 7
  61.       elseif mouseY == 3 then
  62.         char = 4
  63.       elseif mouseY == 5 then
  64.         char = 1
  65.       elseif mouseY == 7 then
  66.         char = 12
  67.       end
  68.     elseif mouseX ==  4 then
  69.       if mouseY == 1 then
  70.         char = 8
  71.       elseif mouseY == 3 then
  72.         char = 5
  73.       elseif mouseY == 5 then
  74.         char = 2
  75.       elseif mouseY == 7 then
  76.         char = 0
  77.       end
  78.     elseif mouseX == 7 then
  79.       if mouseY == 1 then
  80.         char = 9
  81.       elseif mouseY == 3 then
  82.         char = 6
  83.       elseif mouseY == 5 then
  84.         char = 3
  85.       elseif mouseY == 7 and mathf == 1 then
  86.         char = 14
  87.         if mathf == 1 then
  88.           finished = true
  89.         end
  90.       end
  91.     elseif mouseX == 10 and characters ~= 0 then
  92.       if mouseY == 1 then
  93.         if mathf == 0 then
  94.           mathf = -1
  95.         end
  96.       elseif mouseY == 3 then
  97.  --       if pos == 1 and math == 0 then
  98.  --         math = -5
  99.         if mathf == 0 then
  100.           mathf = -2
  101.         end
  102.       elseif mouseY == 5 then
  103.         if mathf == 0 then
  104.           mathf = -3
  105.         end
  106.       elseif mouseY == 7 then
  107.         if mathf == 0 then
  108.           mathf = -4
  109.         end
  110.       end
  111.     end
  112.  
  113. if char >=0 or mathf < 0 then
  114.     term.setCursorPos(pos, 9)
  115.     if mathf < 0 and mathf ~= -5 then
  116.             if mathf == -1 then
  117.                     print(add)
  118.                     maths = 1
  119.             elseif mathf == -2 then
  120.                     print(subtract)
  121.                     maths = 2
  122.             elseif mathf == -3 then
  123.                     print(multiply)
  124.                     maths = 3
  125.             elseif mathf == -4 then
  126.                     print(divide)
  127.                     maths = 4
  128.             end
  129.         mathf = 1
  130.         pos = pos + 2
  131.     elseif math == -5 then
  132.                     print("-")
  133.                     negative = true
  134.                     mathf = 0
  135.                     char = 13
  136.  
  137. elseif char >= 0 and char ~= 12 and char ~= 14 then
  138.     print(char)
  139.     characters = 1
  140.     if mathf == 0 and negative == false then
  141.         editnum1(char)
  142.     elseif mathf == 1 and negative == false then
  143.         editnum2(char)
  144.     elseif mathf == 0 and negative == true then
  145.         editnum1("-")
  146.         negative = false
  147.         finalnegative = true
  148.     end
  149.  
  150. elseif char == 12 and mathf == 0 then
  151.     print(decimal)
  152.     editnum1(".")
  153. elseif char == 12 and mathf == 1 then
  154.     print(decimal)
  155.     editnum2(".")
  156.  
  157. elseif char == 14 then
  158.     finished = true
  159.  
  160. end
  161. pos = pos + 1
  162. end
  163. until finished == true
  164.  
  165. finalread1 = fs.open("/temp/calctemp", "r")
  166. finalnum1 = finalread1.readLine()
  167. finalread1.close()
  168.  
  169. finalread2 = fs.open("/temp/calctemp2", "r")
  170. finalnum2 = finalread2.readLine()
  171. finalread2.close()
  172.  
  173. term.setCursorPos(1, 10)
  174. print("=")
  175.  
  176. if finalnum1 == nil or finalnum1 == " " then
  177.         print("Please input two numbers")
  178.         error()
  179. end
  180.  
  181. if finalnum2 == nil or finalnum2 == " " then
  182.         print("Please input two numbers.")
  183.         error()
  184. end
  185.  
  186. if maths == 4 and finalnum2*1 == 0 and finalnum1*1 ~= 0 then
  187.         for error = 1, 10 do
  188.             print("ERROR!!!!!"..math.random(1, 999999))
  189.             sleep(0.5)
  190.         end
  191.         error()
  192. elseif maths == 4 and finalnum2*1 == 0 and finalnum1*1 == 0 then
  193.         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.")
  194.         error()    
  195. end
  196.  
  197.     if maths == 1 then
  198.             print(finalnum1 + finalnum2)
  199.     elseif maths == 2 then
  200.             print(finalnum1 - finalnum2)
  201.     elseif maths  == 3 then
  202.             print(finalnum1 * finalnum2)
  203.     elseif maths == 4 then
  204.             print(finalnum1 / finalnum2)
  205.     end
  206.  
  207.     if maths == 1  or maths == 2 then
  208.   if finalnum1*1 == 0 or finalnum2*1 == 0 then
  209.             print("That's some tough math.")
  210.   end
  211.     elseif maths == 3 or maths == 4 then
  212.   if finalnum1*1 == 1 or finalnum2*1 == 1 then
  213.             print("That's some tough math.")
  214.   end
  215.     end
  216.    
  217. fs.delete("/temp/calctemp")
  218. fs.delete("/temp/calctemp2")
  219.  
  220. char = -24
  221. mathf = 0
  222. maths = 0
  223.  
  224.  
  225. term.setCursorPos(1, 12)
  226. print("Press enter to finish.")
  227. quit = read()
  228. shell.run("calculator")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement