Advertisement
CaptainSpaceCat

Calculator OOP

May 27th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.80 KB | None | 0 0
  1. w, h = term.getSize()
  2. local stringW = ""
  3. for i = 1, w - 2 do
  4.   stringW = stringW .. " "
  5. end
  6. function initializeCalc()
  7.   term.setBackgroundColor(colors.green)
  8.   term.clear()
  9.   term.setCursorPos(w / 2 - 4, 1)
  10.   term.write("Calculator")
  11.   term.setCursorPos(w / 2 - 9, 2)
  12.   term.write("+  -  *  /  **  ^  %")
  13.   term.setCursorPos(2, 4)
  14.   term.setBackgroundColor(colors.lime)
  15.   term.write(stringW)
  16.   term.setCursorPos(1, h)
  17.   term.setBackgroundColor(colors.blue)
  18.   term.write("Graph")
  19.   term.setCursorPos(w - 3, h)
  20.   term.setBackgroundColor(colors.red)
  21.   term.write("Back")
  22. end
  23.  
  24. local emphasis = {}
  25.   emphasis["+"] = true
  26.   emphasis["-"] = true
  27.   emphasis["/"] = true
  28.   emphasis["*"] = true
  29.   emphasis["^"] = true
  30.   emphasis["%"] = true
  31.   emphasis["("] = true
  32.   emphasis[")"] = true
  33.   emphasis["="] = true
  34.   emphasis["<"] = true
  35.   emphasis[">"] = true
  36.   emphasis["!"] = true
  37.  
  38. local letters = {}
  39.   letters["a"] = true
  40.   letters["b"] = true
  41.   letters["c"] = true
  42.   letters["d"] = true
  43.   letters["e"] = true
  44.   letters["f"] = true
  45.   letters["g"] = true
  46.   letters["h"] = true
  47.   letters["i"] = true
  48.   letters["j"] = true
  49.   letters["k"] = true
  50.   letters["l"] = true
  51.   letters["m"] = true
  52.   letters["n"] = true
  53.   letters["o"] = true
  54.   letters["p"] = true
  55.   letters["q"] = true
  56.   letters["r"] = true
  57.   letters["s"] = true
  58.   letters["t"] = true
  59.   letters["u"] = true
  60.   letters["v"] = true
  61.   letters["w"] = true
  62.   letters["x"] = true
  63.   letters["y"] = true
  64.   letters["z"] = true
  65.  
  66. local dontuse = {}
  67.   dontuse["~"] = true
  68.   dontuse["@"] = true
  69.   dontuse["`"] = true
  70.   dontuse["$"] = true
  71.   dontuse["#"] = true
  72.   dontuse["&"] = true
  73.   dontuse[";"] = true
  74.   dontuse[":"] = true
  75.   dontuse["\""] = true
  76.   dontuse["'"] = true
  77.   dontuse["_"] = true
  78.   dontuse["?"] = true
  79.   dontuse[","] = true
  80.   dontuse["|"] = true
  81.   dontuse["\\"] = true
  82.   dontuse["["] = true
  83.   dontuse["]"] = true
  84.   dontuse["{"] = true
  85.   dontuse["}"] = true
  86.  
  87. local printtype = nil
  88. local globalvars = {}
  89. local calcdone = false
  90. while not calcdone do
  91.   local gInput = nil
  92.   local fInput = nil
  93.   local calculate = nil
  94.   initializeCalc()
  95.   local currentY = 1
  96.   local input = {}
  97.   input[1] = ""
  98.   local inputDone = false
  99.   while not inputDone do
  100.     pressed = nil
  101.     term.setCursorPos(2, #input + 4)
  102.     term.setBackgroundColor(colors.green)
  103.     term.write(stringW)
  104.     term.setBackgroundColor(colors.lime)
  105.     for i = 1, #input do
  106.       term.setCursorPos(2, i + 3)
  107.       term.write(stringW)
  108.       term.setCursorPos(2, i + 3)
  109.       for n = 1, #input[i] do
  110.         letter = string.sub(input[i], n, n)
  111.         if emphasis[letter] == true then
  112.           term.setTextColor(colors.yellow)
  113.         elseif letters[letter] == true then
  114.           term.setTextColor(colors.pink)
  115.         else
  116.           term.setTextColor(colors.white)
  117.         end
  118.         term.write(letter)
  119.       end
  120.     end
  121.     pressed = {os.pullEvent()}   -----------------------start---V
  122.     if pressed[1] == "mouse_click" and pressed[2] == 1 and pressed[3] >= w - 3 and pressed[3] <= w and pressed[4] == h then
  123.       calcdone = true
  124.       break
  125.     elseif pressed[1] == "mouse_click" and pressed[2] == 1 and pressed[3] >= 1 and pressed[3] <= 5 and pressed[4] == h then
  126.       while true do
  127.         local graphPosX = 0
  128.         local graphPosY = 0
  129.         w, h = term.getSize()
  130.         term.setBackgroundColor(colors.blue)
  131.         term.clear()
  132.         term.setCursorPos(w/2 - 4, 1)
  133.         term.write("Grapher")
  134.         term.setCursorPos(1, 3)
  135.         term.write("y=")
  136.         term.setCursorPos(w - 3, h)
  137.         term.setBackgroundColor(colors.blue)
  138.         term.write("Back")
  139.         pressed = {os.pullEvent()} -------start graph --v
  140.         gInput = nil
  141.         if pressed[1] == "mouse_click" and pressed[2] == 1 and pressed[4] == 3 then
  142.           term.setCursorPos(3, 3)
  143.           gInput = read()
  144.         elseif pressed[1] == "mouse_click" and pressed[2] == 1 and pressed[3] >= w - 3 and pressed[3] <= w and pressed[4] == h then
  145.           initializeCalc()
  146.           break
  147.         end
  148.         if gInput then
  149.           oTemp = fs.open("_runGraph", "w")
  150.           oTemp.writeLine("graphtab = {}")
  151.           oTemp.writeLine("for x = -100, 100 do")
  152.           oTemp.writeLine("  graphtab[x] = " .. tostring(gInput))
  153.           oTemp.writeLine("end")
  154.           oTemp.flush()
  155.           oTemp.close()
  156.           os.loadAPI("_runGraph")
  157.           local donegraphing = false
  158.           while not donegraphing do
  159.             term.setBackgroundColor(colors.black)
  160.             term.clear()
  161.             for i = graphPosX, w - graphPosX do
  162.               paintutils.drawLine(i - graphPosX, h - _runGraph.graphtab[i] + 1 + graphPosY, i + 1 - graphPosX, h - _runGraph.graphtab[i + 1] + 1 + graphPosY, colors.green)
  163.               term.setCursorPos(1, 1)
  164.             end
  165.             --[[for i, v in pairs(_runGraph.graphtab) do
  166.               paintutils.drawPixel(i, h - v + 1, colors.blue)
  167.               term.setCursorPos(1, 1)
  168.             end]]--
  169.             shell.run("delete _runGraph")
  170.             pressed = {os.pullEvent()}
  171.             if pressed[1] == "key" then
  172.               if pressed[2] == 200 then  --up
  173.                 graphPosY = graphPosY - 1
  174.               elseif pressed[2] == 208 then  --down
  175.                 graphPosY = graphPosY + 1
  176.               elseif pressed[2] == 203 then  --left
  177.                 graphPosX = graphPosX + 1
  178.               elseif pressed[2] == 205 then --right
  179.                 graphPosX = graphPosX - 1
  180.               end
  181.             end
  182.           end
  183.         end
  184.       end
  185.     end
  186.     if #input[currentY] == w - 2 then
  187.       currentY = currentY + 1
  188.       input[currentY] = ""
  189.     end
  190.     if pressed[1] == "char" and not dontuse[pressed[2]] then
  191.       input[currentY] = input[currentY] .. pressed[2]
  192.     elseif pressed[1] == "key" then
  193.       if pressed[2] == 14 then --delete
  194.         if input[currentY] == "" and #input ~= 1 then
  195.           input[currentY] = nil
  196.           currentY = currentY - 1
  197.         end
  198.         input[currentY] = string.sub(input[currentY], 0, #input[currentY] - 1)
  199.       elseif pressed[2] == 28 then --enter
  200.         inputDone = true
  201.       end
  202.     end
  203.   end
  204.   if calcdone then break end
  205.   local fInput = ""
  206.   for i = 1, #input do
  207.     fInput = fInput .. input[i]
  208.   end
  209.   local var = nil
  210.   local varnum = nil
  211.   var = string.find(fInput, "=")
  212.   if var then
  213.     printtype = "var"
  214.     var = nil
  215.     varnum = 0
  216.     for i, v in pairs(letters) do
  217.       local stringstart = 0
  218.       while string.find(fInput, i, stringstart) do
  219.         var = string.find(fInput, i, stringstart)
  220.         if var then
  221.           stringstart = var + 1
  222.           varnum = varnum + 1
  223.         end
  224.       end
  225.     end
  226.     for i = 1, #tostring(fInput) do
  227.       for k, e in pairs(letters) do
  228.         if string.sub(fInput, i, i) == k then
  229.           var = i
  230.           break
  231.         end
  232.       end
  233.       if var then
  234.         break
  235.       end
  236.     end
  237.     varnum = string.sub(fInput, var - 1, varnum)
  238.     --term.setBackgroundColor(colors.blue)
  239.     --print(varnum)
  240.     globalvars[#globalvars + 1] = varnum
  241.     --fInput = string.sub(#tostring(fInput), 1, var - 1) .. " local " .. string.sub(tostring(fInput), var, #tostring(fInput))
  242.   else
  243.     fInput = "answer = " .. tostring(fInput)
  244.     printtype = "math"
  245.   end
  246.   answer = nil
  247.   calculate = nil
  248.   calculate = loadstring(fInput)
  249.   calculate()
  250.   term.setBackgroundColor(colors.black)
  251.   term.setTextColor(colors.white)
  252.   if printtype == "math" then
  253.     term.setCursorPos(w / 2 - #tostring(answer) / 2, h - 1)
  254.     term.write(answer)
  255.   elseif printtype == "var" then
  256.     term.setCursorPos(w / 2 - #tostring(varnum) / 2, h - 1)
  257.     term.write(fInput)
  258.   end
  259.   sleep(2)
  260. end
  261.  
  262. local vnil = nil
  263. for i, v in pairs(globalvars) do
  264.   --print(v .. " = nil")
  265.   vnil = loadstring(v .. " = nil")
  266.   vnil()
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement