Advertisement
guitarplayer616

Calculator Backup

May 28th, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.08 KB | None | 0 0
  1. local w, h = term.getSize()
  2. term.setBackgroundColor(colors.blue)
  3. term.clear()
  4. local memory = nil
  5. local events = {}
  6. local col = nil
  7. local ascii = nil
  8. local arithCount = 0
  9. local ans = nil
  10. local string = ""
  11. local equation = ""
  12. local solve = nil
  13. local solved = nil
  14. local numpad = {".",0,1,2,3,4,5,6,7,8,9}
  15. local arithmetic = {"+","-","x","/"}
  16. local keys = {}                    
  17.   keys["MRC"] = {2,6}
  18.   keys["M-"] = {8,6}
  19.   keys["M+"] = {14,6}
  20.   keys["/"] = {21,6}
  21.   keys[7] = {2,9}
  22.   keys[8] = {8,9}
  23.   keys[9] = {14,9}
  24.   keys["x"] = {21,9}
  25.   keys[4] = {2,12}
  26.   keys[5] = {8,12}
  27.   keys[6] = {14,12}
  28.   keys["-"] = {21,12}
  29.   keys[1] = {2,15}
  30.   keys[2] = {8,15}
  31.   keys[3] = {14,15}
  32.   keys["+"] = {21,15}
  33.   keys["ON/C"] = {2,18}
  34.   keys[0] = {8,18}
  35.   keys["."] = {14,18}
  36.   keys["="] = {21,18}
  37. local numbers = {}
  38.   numbers[1] = {
  39. "   / |",
  40. "   | |",
  41. "   |_|"
  42. }
  43.   numbers[2] = {
  44. " |_  )",
  45. "  / / ",
  46. " /___|"
  47. }
  48.   numbers[3] = {
  49. " |__ /",
  50. "  |_ \\",
  51. " |___/"
  52. }
  53.  
  54.  
  55. function input()
  56. local tArgs = {"get","XAktVaSD","startup"}
  57. if #tArgs < 2 then
  58.     return
  59. end
  60.  
  61. local function get(paste)
  62.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode( paste ))
  63.     if response then
  64.         local sResponse = response.readAll()
  65.         response.close()
  66.         return sResponse
  67.     else
  68.         printError( "Failed." )
  69.     end
  70. end
  71.  
  72. local sCommand = tArgs[1]
  73. if sCommand == "put" then
  74.     local sFile = tArgs[2]
  75.     local sPath = shell.resolve( sFile )
  76.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  77.         print( "No such file" )
  78.         return
  79.     end
  80.    
  81.     local sName = fs.getName( sPath )
  82.     local file = fs.open( sPath, "r" )
  83.     local sText = file.readAll()
  84.     file.close()
  85.    
  86.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  87.     local response = http.post(
  88.         "http://pastebin.com/api/api_post.php",
  89.         "api_option=paste&"..
  90.         "api_dev_key="..key.."&"..
  91.         "api_paste_format=lua&"..
  92.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  93.         "api_paste_code="..textutils.urlEncode(sText)
  94.     )
  95.        
  96.     if response then
  97.        
  98.         local sResponse = response.readAll()
  99.         response.close()
  100.                
  101.         local sCode = string.match( sResponse, "[^/]+$" )
  102.     else
  103.         print( "Failed." )
  104.     end
  105.    
  106. elseif sCommand == "get" then
  107.     if #tArgs < 3 then
  108.         printUsage()
  109.         return
  110.     end
  111.  
  112.     local sCode = tArgs[2]
  113.     local sFile = tArgs[3]
  114.     local sPath = shell.resolve( sFile )
  115.     if fs.exists( sPath ) then
  116.         return
  117.     end
  118.    
  119.     local res = get(sCode)
  120.     if res then        
  121.         local file = fs.open( sPath, "w" )
  122.         file.write( res )
  123.         file.close()
  124.        
  125.     end
  126. elseif sCommand == "run" then
  127.     local sCode = tArgs[2]
  128.  
  129.     local res = get(sCode)
  130.     if res then
  131.         local func, err = loadstring(res)
  132.         if not func then
  133.             printError( err )
  134.             return
  135.         end
  136.         setfenv(func, getfenv())
  137.         local success, msg = pcall(func, unpack(tArgs, 3))
  138.         if not success then
  139.             printError( msg )
  140.         end
  141.     end
  142. else
  143.     printUsage()
  144.     return
  145. end
  146. end
  147.  
  148. input()
  149.  
  150. paintutils.drawFilledBox(2,2,25,4,colors.green)
  151.  
  152. for i=6,18,3 do
  153.   if i==6 then
  154.     col = colors.red
  155.   else
  156.     col = colors.white
  157.   end
  158.   paintutils.drawFilledBox(2,i,25,i+1,col)
  159.   paintutils.drawLine(7,i,7,i+1,colors.blue)
  160.   paintutils.drawLine(13,i,13,i+1,colors.blue)
  161.   paintutils.drawFilledBox(19,i,20,i+1,colors.blue)
  162.   term.setBackgroundColor(colors.blue)
  163. end
  164.  
  165. for i=6,18,3 do
  166.   paintutils.drawFilledBox(21,i,25,i+1,colors.red)
  167. end
  168.  
  169. paintutils.drawFilledBox(2,18,6,19,colors.red)
  170.  
  171. function uWords(string, x, y, txtcol, bakcol)
  172.   if txtcol then
  173.     term.setTextColor(txtcol)
  174.   end
  175.   if bakcol then
  176.     term.setBackgroundColor(bakcol)
  177.   end
  178.   if x and not tonumber(x) then
  179.     if string.sub(x, 0, 1) == "r" then
  180.       if x == "right" or x == "r" then
  181.         term.setCursorPos(w - #string + 1, y)
  182.       elseif string.sub(x, 3, 3) == "-" or string.sub(x, 7, 7) == "-" then
  183.         offset = tonumber(string.sub(x, 5, 5)) or tonumber(string.sub(x, 9, 9))
  184.         term.setCursorPos(w - #string + 1 - offset, y)
  185.       end
  186.     end
  187.   else
  188.     term.setCursorPos(x, y)
  189.   end
  190.   term.write(string)
  191. end
  192.  
  193. for i,v in pairs(keys) do
  194.   local pos = {v}
  195.   local colT = nil
  196.   local colB = nil
  197.   term.setTextColor(colors.white)
  198.   term.setBackgroundColor(colors.black)
  199.   if tonumber(pos[1][1]) == 21 or tonumber(pos[1][2]) == 6 or (tonumber(pos[1][1]) == 2 and tonumber(pos[1][2]) == 18) then
  200.     colT = colors.white
  201.     colB = colors.red
  202.   else
  203.     colT = colors.blue
  204.     colB = colors.white
  205.   end
  206.   uWords(tostring(i),pos[1][1]+1,pos[1][2],colT,colB)
  207. end
  208.  
  209. uWords("0","r - 2",3,colors.gray,colors.green)
  210.  
  211. function clearScreen(t)
  212.   uWords(string.rep(" ",22),"r - 2",3,colors.gray,colors.green)
  213.   string = ""
  214.   if not t then
  215.     sleep(0.1)
  216.   end
  217. end
  218.  
  219. function clear(t)
  220.   clearScreen(t)
  221.   equation = ""
  222.   solved = false
  223.   arithCount = 0
  224. end
  225.  
  226. function resolve(clear)
  227.   if clear then
  228.     clearScreen()
  229.   else
  230.     clearScreen(t)
  231.   end
  232.   -- for example if the equation = 5 + 5 - 3 + then it would be turned into just 5 + 5 - 3
  233.   for _,arith in ipairs(arithmetic) do
  234.     if equation:sub(equation:len()) == arith then
  235.       equation = table.concat{equation:sub(1,equation:len()-1),"",equation:sub(equation:len()+1)}
  236.     end
  237.   end
  238.   equation = equation:gsub("x","*")
  239.   solve = loadstring("x = "..equation)
  240.   solve();ans = x
  241.   uWords(tostring(ans),"r - 2",3,colors.gray,colors.green)
  242.   if clear then
  243.     solved = true
  244.     arithCount = 0
  245.   end
  246. end
  247.  
  248. while true do
  249.   events = {os.pullEventRaw()}
  250.   if events[1] == "terminate" then
  251.     while true do
  252.       uWords("Exit,y/n?","r - 2",3,colors.gray,colors.green)
  253.       local p = {os.pullEventRaw()}
  254.       if p[1] == "terminate" or (p[1] == "key" and p[2] == 28) or p[2] == "y" then
  255.         term.setTextColor(colors.white)
  256.         term.setBackgroundColor(colors.black)
  257.         term.clear()
  258.         term.setCursorPos(1,1)
  259.         error()
  260.       elseif p[2]=="n" then
  261.         clear()
  262.         uWords("0","r - 2",3,colors.gray,colors.green)
  263.         break
  264.       end
  265.     end
  266.   end
  267.   for i,v in pairs(keys) do
  268.     if events[1] == "mouse_click" and events[3] >= v[1] and events[3] <= (v[1] + 4) and
  269.     events[4] >= v[2] and events[4] <= (v[2] + 1) then
  270.    
  271.       for _,num in ipairs(numpad) do
  272.         if i == num then
  273.           if solved then
  274.             clear(1)
  275.           end
  276.           if arithCount >= 1 then
  277.             uWords(string.rep(" ",22),"r - 2",3,colors.gray,colors.green)
  278.           end
  279.           string = string..tostring(i)
  280.           equation = equation..tostring(i)
  281.           uWords(string,"r - 2",3,colors.gray,colors.green)
  282.         end
  283.       end
  284.       for _,arith in ipairs(arithmetic) do
  285.         if i==arith then
  286.           if solved then
  287.             clear(1)
  288.             equation = equation..ans
  289.           end
  290.           if arithCount >= 1 then
  291.             resolve()
  292.           else
  293.             clearScreen()
  294.             uWords(i,"r - 2",3,colors.gray,colors.green)
  295.           end
  296.           equation = equation..tostring(i)
  297.           arithCount = arithCount + 1
  298.           end
  299.         end
  300.       if i=="=" then
  301.         resolve(clear)
  302.       end
  303.       if i=="ON/C" then
  304.         clear()
  305.         uWords("0","r - 2",3,colors.gray,colors.green)
  306.       end
  307.       if i=="M+" then
  308.         memory = tostring(ans)
  309.       end
  310.       if i=="M-" then
  311.         memory = nil
  312.       end
  313.       if i=="MRC" then
  314.         if memory then
  315.           clearScreen(t)
  316.           uWords(memory,"r - 2",3,colors.gray,colors.green)
  317.         end
  318.       end
  319.     end
  320.     term.setCursorPos(1,h-4)
  321.     --[[for i=1,#events do
  322.       term.setTextColor(colors.white)
  323.       term.setBackgroundColor(colors.blue)
  324.       write(" ")
  325.       term.setBackgroundColor(colors.green)
  326.       write(events[i])
  327.       local x,y = term.getCursorPos()
  328.       write(string.rep(" ",w-x))
  329.       write("\n")
  330.     end]]--
  331.   end
  332. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement