mad1231999

Untitled

Jan 26th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[Smart Calculator by Cranium]]--
  2.  
  3. local tX, tY = term.getSize()
  4. local calc = {}
  5.  
  6. --equation states
  7. calc.mode = false
  8. calc.inverse = false
  9. calc.hyp = false
  10. calc.sqrt = false
  11. calc.exp = false
  12. calc.asin = false
  13. calc.sin = false
  14. calc.sinh = false
  15. calc.atan = false
  16. calc.tan = false
  17. calc.tanh = false
  18. calc.acos = false
  19. calc.cos = false
  20. calc.cosh = false
  21. calc.log = false
  22. calc.pos = false
  23.  
  24. -- characters
  25. local charList = {
  26.     ["0"] = {
  27.         ".-.",
  28.         "| |",
  29.         "'-'"
  30.     },
  31.     ["1"] = {
  32.         " . ",
  33.         "'| ",
  34.         "---"
  35.     },
  36.     ["2"] = {
  37.         ".-.",
  38.         ".-'",
  39.         "'- "
  40.     },
  41.     ["3"] = {
  42.         " -.",
  43.         " -|",
  44.         " -'"
  45.     },
  46.     ["4"] = {
  47.         ". .",
  48.         "'-|",
  49.         "  '"
  50.     },
  51.     ["5"] = {
  52.         ".- ",
  53.         "'-.",
  54.         " -'"
  55.     },
  56.     ["6"] = {
  57.         ".-.",
  58.         "|-.",
  59.         "'-'"
  60.     },
  61.     ["7"] = {
  62.         ".-.",
  63.         "  |",
  64.         "  '"
  65.     },
  66.     ["8"] = {
  67.         ".-.",
  68.         "|-|",
  69.         "'-'"
  70.     },
  71.     ["9"] = {
  72.         ".-.",
  73.         "'-|",
  74.         " -'"
  75.     },
  76.     ["."] = {
  77.         "   ",
  78.         "   ",
  79.         " . "
  80.     },
  81.     ["-"] = {
  82.         "   ",
  83.         " --",
  84.         "   "
  85.     },
  86. }
  87. -- lay out the button labels
  88. local labels = {
  89.     ' + ', ' 1 ', ' 2 ', ' 3 ', '<--', 'sin', 'x^y', 'DEG', 'OFF',
  90.     ' - ', ' 4 ', ' 5 ', ' 6 ', 'CLR', 'cos', 'srt', 'RAD', '   ',
  91.     ' x ', ' 7 ', ' 8 ', ' 9 ', '   ', 'tan', 'Pi ', 'inv', '   ',
  92.     ' / ', ' 0 ', '-/+', ' . ', ' = ', 'log', 'exp', 'hyp', '   '
  93. }
  94.  
  95.  
  96. -- generate the objects
  97. local function objGen()
  98.     local _objects = {}
  99.     local width = 9
  100.     for i=1, #labels do
  101.         table.insert(_objects, {
  102.             x = (((i - 1)%width + 1)*5) - 1;
  103.             y = (math.ceil(i/width) * 3) + 4;
  104.             label = labels[i];
  105.             -- make operators different colors
  106.             color =
  107.             i == 1 and colors.blue or
  108.             i == 5 and colors.red or
  109.             i == 6 and colors.yellow or
  110.             i == 7 and colors.orange or
  111.             i == 8 and colors.white or
  112.             i == 9 and colors.red or
  113.             i == 10 and colors.blue or
  114.             i == 14 and colors.red or
  115.             i == 15 and colors.yellow or
  116.             i == 16 and colors.orange or
  117.             i == 17 and colors.white or
  118.             i == 18 and colors.white or
  119.             i == 19 and colors.blue or
  120.             i == 24 and colors.yellow or
  121.             i == 25 and colors.orange or
  122.             i == 26 and colors.white or
  123.             i == 27 and colors.white or
  124.             i == 28 and colors.blue or
  125.             i == 30 and colors.red or
  126.             i == 32 and colors.white or
  127.             i == 33 and colors.yellow or
  128.             i == 34 and colors.orange or
  129.             i == 35 and colors.white or
  130.             i == 36 and colors.white or
  131.             colors.lightGray;
  132.             -- controls the highlight colors for operators
  133.             back =
  134.             i == 6 and
  135.                 calc.sin == true and colors.red or
  136.                 calc.asin == true and colors.red or
  137.                 calc.sinh == true and colors.red or
  138.             i == 8 and calc.mode == "deg" and colors.blue or
  139.             i == 15 and
  140.                 calc.cos == true and colors.red or
  141.                 calc.acos == true and colors.red or
  142.                 calc.cosh == true and colors.red or
  143.             i == 16 and calc.sqrt == true and colors.lightBlue or
  144.             i == 17 and calc.mode == "rad" and colors.blue or
  145.             i == 24 and
  146.                 calc.tan == true and colors.red or
  147.                 calc.atan == true and colors.red or
  148.                 calc.tanh == true and colors.red or
  149.             i == 26 and calc.inverse == true and colors.blue or
  150.             i == 30 and calc.pos == true and colors.white or
  151.             i == 33 and calc.log == true and colors.red or
  152.             i == 34 and calc.exp == true and colors.lightBlue or
  153.             i == 35 and calc.hyp == true and colors.blue or
  154.             colors.black;
  155.         })
  156.     end
  157. return _objects
  158. end
  159.  
  160. local function draw()
  161.     term.setBackgroundColor(colors.black)
  162.     term.clear()
  163.     local objects = objGen()
  164.     for i=1, #objects do
  165.         local obj = objects[i]
  166.         term.setTextColor(colors.gray)
  167.         term.setBackgroundColor(colors.black)
  168.         --draw the grid
  169.         for num, line in pairs{'+---+','|   |','+---+'} do
  170.             term.setCursorPos(obj.x, obj.y + num - 1)
  171.             write(line)
  172.         end
  173.         --draw the button text and colors
  174.         term.setCursorPos(obj.x+1, obj.y+1)
  175.         term.setTextColor(obj.color)
  176.         term.setBackgroundColor(obj.back)
  177.         write(obj.label)
  178.     end
  179. end
  180. --draw border and screen
  181. local function display()
  182.     term.setBackgroundColor(colors.black)
  183.     term.setTextColor(colors.gray)
  184.     term.setCursorPos(2,1)
  185.     write("+"..string.rep("-", tX - 4).."+")
  186.     for i = 2, tY - 1 do
  187.         term.setCursorPos(2,i)
  188.         write("|")
  189.         term.setCursorPos(tX - 1,i)
  190.         write("|")
  191.     end
  192.     term.setCursorPos(2,tY)
  193.     write("+"..string.rep("-", tX - 4).."+")
  194.     term.setBackgroundColor(colors.lightGray)
  195.     for i = 2, 6 do
  196.         term.setCursorPos(4,i)
  197.         write(string.rep(" ", tX - 6))
  198.     end
  199. end
  200. --run the equation passed by the user.
  201. local function calculate(eq)
  202.     if table.concat(eq) == "()" then
  203.         eq = {"0"}
  204.     elseif table.concat(eq) == "(.)" then
  205.         eq = {"0"}
  206.     end
  207.     local sExpr = table.concat(eq)
  208.     local fnMath, sErr = loadstring("return "..sExpr)
  209.     if not fnMath then
  210.         return "ERROR! Check syntax!"
  211.     end
  212.     --setfenv(fnMath, math)
  213.     local bSucc, vRes = pcall(fnMath)
  214.     if not bSucc then
  215.         return "ERROR! Check syntax!"
  216.     else
  217.         return vRes
  218.     end
  219. end
  220.  
  221. -- function loop
  222. local equation = {"(", ")"}
  223. local result = "0"
  224. while true do
  225.     local rLen = 0
  226.     draw()
  227.     display()
  228.     term.setBackgroundColor(colors.lightGray)
  229.     term.setTextColor(colors.white)
  230.     term.setCursorPos(4,2)
  231.     --write the equation
  232.     write(table.concat(equation))
  233.     --write the result
  234.     if tonumber(result) ~= inf then
  235.         if string.len(result) >= 15 then
  236.             term.setCursorPos(5, 4)
  237.             term.setTextColor(colors.black)
  238.             write("= ")
  239.             for num in string.gmatch(result, ".") do
  240.                 rLen = rLen + 1
  241.                 local pX,pY = term.getCursorPos()
  242.                 if pX >= 4 and pX <= 48 then
  243.                     term.setCursorPos(rLen + 5, 4)
  244.                     write(num)
  245.                 else
  246.                     term.setCursorPos(rLen + 5 - 48, 5)
  247.                     write(num)
  248.                 end
  249.             end
  250.         else
  251.             for num in string.gmatch(result, ".") do
  252.                 rLen = rLen + 1
  253.                 for i = 1, #charList[num] do
  254.                     term.setTextColor(colors.black)
  255.                     term.setCursorPos((rLen * 3) + 1, i + 3)
  256.                     write(charList[num][i])
  257.                 end
  258.             end
  259.         end
  260.     else
  261.         term.setCursorPos(5, 4)
  262.         term.setTextColor(colors.black)
  263.         write("= INFINITY")
  264.     end
  265.     local events = {os.pullEvent()}
  266.     --mouse click filter
  267.     if events[1] == "mouse_click" and events[2] == 1 then
  268.         if events[3] >= 44 and events[3] <= 48 then
  269.             if events[4] >= 7 and events[4] <= 9 then
  270.                 os.shutdown()
  271.             end
  272.         elseif events[3] >= 39 and events[3] <= 43 then
  273.             if events[4] >= 7 and events[4] <= 9 then
  274.                 if calc.mode == false then
  275.                     table.remove(equation, 1)
  276.                     calc.mode = "deg"
  277.                     table.insert(equation, 1, "math.deg(")
  278.                 elseif calc.mode == "deg" then
  279.                     table.remove(equation, 1)
  280.                     calc.mode = false
  281.                 elseif calc.mode == "rad" then
  282.                     table.remove(equation, 1)
  283.                     calc.mode = "deg"
  284.                     table.insert(equation, 1, "math.deg(")
  285.                 end
  286.             elseif events[4] >= 10 and events[4] <= 12 then
  287.                 if calc.mode == false then
  288.                     table.remove(equation, 1)
  289.                     calc.mode = "rad"
  290.                     table.insert(equation, 1, "math.rad(")
  291.                 elseif calc.mode == "rad" then
  292.                     table.remove(equation, 1)
  293.                     calc.mode = false
  294.                 elseif calc.mode == "deg" then
  295.                     table.remove(equation, 1)
  296.                     calc.mode = "rad"
  297.                     table.insert(equation, 1, "math.rad(")
  298.                 end
  299.             elseif events[4] >= 13 and events[4] <= 15 then
  300.                 if calc.inverse == true and calc.hyp == false then
  301.                     calc.inverse = false
  302.                 elseif calc.inverse == false and calc.hyp == true then
  303.                     calc.inverse = true
  304.                     calc.hyp = false
  305.                 elseif calc.inverse == false and calc.hyp == false then
  306.                     calc.inverse = true
  307.                 end
  308.             elseif events[4] >= 16 and events[4] <= 18 then
  309.                 if calc.hyp == true and calc.inverse == false then
  310.                     calc.hyp = false
  311.                 elseif calc.hyp == false and calc.inverse == true then
  312.                     calc.hyp = true
  313.                     calc.inverse = false
  314.                 elseif calc.hyp == false and calc.inverse == false then
  315.                     calc.hyp = true
  316.                 end
  317.             end
  318.         elseif events[3] >= 34 and events[3] <= 38 then
  319.             if events[4] >= 7 and events[4] <= 9 then
  320.                 table.insert(equation, #equation, "^")
  321.             elseif events[4] >= 10 and events[4] <= 12 then
  322.                 if calc.sqrt == false then
  323.                     table.insert(equation, #equation, "math.sqrt(")
  324.                     calc.sqrt = true
  325.                 elseif calc.sqrt == true then
  326.                     table.insert(equation, #equation, ")")
  327.                     calc.sqrt = false
  328.                 end
  329.             elseif events[4] >= 13 and events[4] <= 15 then
  330.                 table.insert(equation, #equation, "math.pi")
  331.             elseif events[4] >= 16 and events[4] <= 18 then
  332.                 if calc.exp == false then
  333.                     table.insert(equation, #equation, "math.exp(")
  334.                     calc.exp = true
  335.                 elseif calc.exp == true then
  336.                     table.insert(equation, #equation, ")")
  337.                     calc.exp = false
  338.                 end
  339.             end
  340.         elseif events[3] >= 29 and events[3] <= 33 then
  341.             if events[4] >= 7 and events[4] <= 9 then
  342.                 if calc.inverse == true and calc.asin == false then
  343.                     table.insert(equation, #equation, "math.asin(")
  344.                     calc.asin = true
  345.                 elseif calc.inverse == false and calc.hyp == false and calc.sin == false then
  346.                     table.insert(equation, #equation, "math.sin(")
  347.                     calc.sin = true
  348.                 elseif calc.hyp == true and calc.sinh == false then
  349.                     table.insert(equation, #equation, "math.sinh(")
  350.                     calc.sinh = true
  351.                 elseif calc.asin == true then
  352.                     table.insert(equation, #equation, ")")
  353.                     calc.asin = false
  354.                 elseif calc.sin == true then
  355.                     table.insert(equation, #equation, ")")
  356.                     calc.sin = false
  357.                 elseif calc.sinh == true then
  358.                     table.insert(equation, #equation, ")")
  359.                     calc.sinh = false
  360.                 end
  361.             elseif events[4] >= 10 and events[4] <= 12 then
  362.                 if calc.inverse == true and calc.acos == false then
  363.                     table.insert(equation, #equation, "math.acos(")
  364.                     calc.acos = true
  365.                 elseif calc.inverse == false and calc.hyp == false and calc.cos == false then
  366.                     table.insert(equation, #equation, "math.cos(")
  367.                     calc.cos = true
  368.                 elseif calc.hyp == true and calc.cosh == false then
  369.                     table.insert(equation, #equation, "math.cosh(")
  370.                     calc.cosh = true
  371.                 elseif calc.acos == true then
  372.                     table.insert(equation, #equation, ")")
  373.                     calc.acos = false
  374.                 elseif calc.cos == true then
  375.                     table.insert(equation, #equation, ")")
  376.                     calc.cos = false
  377.                 elseif calc.cosh == true then
  378.                     table.insert(equation, #equation, ")")
  379.                     calc.cosh = false
  380.                 end
  381.             elseif events[4] >= 13 and events[4] <= 15 then
  382.                 if calc.inverse == true and calc.atan == false then
  383.                     table.insert(equation, #equation, "math.atan(")
  384.                     calc.atan = true
  385.                 elseif calc.inverse == false and calc.hyp == false and calc.tan == false then
  386.                     table.insert(equation, #equation, "math.tan(")
  387.                     calc.tan = true
  388.                 elseif calc.hyp == true and calc.tanh == false then
  389.                     table.insert(equation, #equation, "math.tanh(")
  390.                     calc.tanh = true
  391.                 elseif calc.atan == true then
  392.                     table.insert(equation, #equation, ")")
  393.                     calc.atan = false
  394.                 elseif calc.tan == true then
  395.                     table.insert(equation, #equation, ")")
  396.                     calc.tan = false
  397.                 elseif calc.tanh == true then
  398.                     table.insert(equation, #equation, ")")
  399.                     calc.tanh = false
  400.                 end
  401.             elseif events[4] >= 16 and events[4] <= 18 then
  402.                 if calc.log == false then
  403.                     table.insert(equation, #equation, "math.log10(")
  404.                     calc.log = true
  405.                 elseif calc.log == true then
  406.                     table.insert(equation, ")")
  407.                     calc.log = false
  408.                 end
  409.             end
  410.         -- backspace, clear, equals
  411.         elseif events[3] >= 24 and events[3] <= 28 then
  412.             if events[4] >= 7 and events[4] <= 9 then
  413.                 if table.concat(equation) ~= "()" then
  414.                     table.remove(equation, #equation - 1)
  415.                 end
  416.             elseif events[4] >= 10 and events[4] <= 12 then
  417.                 calc.mode = false
  418.                 calc.inverse = false
  419.                 calc.hyp = false
  420.                 calc.sqrt = false
  421.                 calc.exp = false
  422.                 calc.asin = false
  423.                 calc.sin = false
  424.                 calc.sinh = false
  425.                 calc.atan = false
  426.                 calc.tan = false
  427.                 calc.tanh = false
  428.                 calc.acos = false
  429.                 calc.cos = false
  430.                 calc.cosh = false
  431.                 calc.log = false
  432.                 calc.pos = false
  433.                 equation = {"(", ")"}
  434.                 result = "0"
  435.             elseif events[4] >= 16 and events[4] <= 18 then
  436.                 if equation[#equation-1] == "+" or
  437.                     equation[#equation-1] == "-" or
  438.                     equation[#equation-1] == "*" or
  439.                     equation[#equation-1] == "/" then
  440.                     table.insert(equation, #equation, "0")
  441.                 elseif equation[#equation-1] == "^" then
  442.                     table.insert(equation, #equation, "1")
  443.                 end
  444.                 for i, v in pairs(calc) do
  445.                     if calc[i] == true then
  446.                         table.insert(equation, #equation, ")")
  447.                         calc[i] = false
  448.                     end
  449.                 end
  450.                 result = tostring(calculate(equation))
  451.             end
  452.         -- 3, 6, 9, decimal
  453.         elseif events[3] >= 19 and events[3] <= 23 then
  454.             if events[4] >= 7 and events[4] <= 9 then
  455.                 table.insert(equation, #equation, "3")
  456.             elseif events[4] >= 10 and events[4] <= 12 then
  457.                 table.insert(equation, #equation, "6")
  458.             elseif events[4] >= 13 and events[4] <= 15 then
  459.                 table.insert(equation, #equation, "9")
  460.             elseif events[4] >= 16 and events[4] <= 18 then
  461.                 table.insert(equation, #equation, ".")
  462.             end
  463.         -- 2, 5, 8, positive/negative
  464.         elseif events[3] >= 14 and events[3] <= 18 then
  465.             if events[4] >= 7 and events[4] <= 9 then
  466.                 table.insert(equation, #equation, "2")
  467.             elseif events[4] >= 10 and events[4] <= 12 then
  468.                 table.insert(equation, #equation, "5")
  469.             elseif events[4] >= 13 and events[4] <= 15 then
  470.                 table.insert(equation, #equation, "8")
  471.             elseif events[4] >= 16 and events[4] <= 18 then
  472.                 if calc.pos == false then
  473.                     table.insert(equation, #equation, "(-")
  474.                     calc.pos = true
  475.                 elseif calc.pos == true then
  476.                     table.insert(equation, #equation, ")")
  477.                     calc.pos = false
  478.                 end
  479.             end
  480.         -- 1, 4, 7, 0
  481.         elseif events[3] >= 9 and events[3] <= 13 then
  482.             if events[4] >= 7 and events[4] <= 9 then
  483.                 table.insert(equation, #equation, "1")
  484.             elseif events[4] >= 10 and events[4] <= 12 then
  485.                 table.insert(equation, #equation, "4")
  486.             elseif events[4] >= 13 and events[4] <= 15 then
  487.                 table.insert(equation, #equation, "7")
  488.             elseif events[4] >= 16 and events[4] <= 18 then
  489.                 table.insert(equation, #equation, "0")
  490.             end
  491.         -- add, subtract, multiply, divide
  492.         elseif events[3] >= 4 and events[3] <= 8 then
  493.             if events[4] >= 7 and events[4] <= 9 then
  494.                 table.insert(equation, #equation, "+")
  495.             elseif events[4] >= 10 and events[4] <= 12 then
  496.                 table.insert(equation, #equation, "-")
  497.             elseif events[4] >= 13 and events[4] <= 15 then
  498.                 table.insert(equation, #equation, "*")
  499.             elseif events[4] >= 16 and events[4] <= 18 then
  500.                 table.insert(equation, #equation, "/")
  501.             end
  502.         end
  503.     -- filter for keyboard presses
  504.     elseif events[1] == "key" then
  505.         if events[2] == 79 then
  506.             table.insert(equation, #equation, "1")
  507.         elseif events[2] == 80 then
  508.             table.insert(equation, #equation, "2")
  509.         elseif events[2] == 81 then
  510.             table.insert(equation, #equation, "3")
  511.         elseif events[2] == 75 then
  512.             table.insert(equation, #equation, "4")
  513.         elseif events[2] == 76 then
  514.             table.insert(equation, #equation, "5")
  515.         elseif events[2] == 77 then
  516.             table.insert(equation, #equation, "6")
  517.         elseif events[2] == 71 then
  518.             table.insert(equation, #equation, "7")
  519.         elseif events[2] == 72 then
  520.             table.insert(equation, #equation, "8")
  521.         elseif events[2] == 73 then
  522.             table.insert(equation, #equation, "9")
  523.         elseif events[2] == 82 then
  524.             table.insert(equation, #equation, "0")
  525.         elseif events[2] == 83 then
  526.             table.insert(equation, #equation, ".")
  527.         elseif events[2] == 78 then
  528.             table.insert(equation, #equation, "+")
  529.         elseif events[2] == 74 then
  530.             table.insert(equation, #equation, "-")
  531.         elseif events[2] == 55 then
  532.             table.insert(equation, #equation, "*")
  533.         elseif events[2] == 181 then
  534.             table.insert(equation, #equation, "/")
  535.         elseif events[2] == 14 then
  536.             if table.concat(equation) ~= "()" then
  537.                 table.remove(equation, #equation - 1)
  538.             end
  539.         elseif events[2] == 28 or events[2] == 156 then
  540.             if equation[#equation-1] == "+" or
  541.                 equation[#equation-1] == "-" or
  542.                 equation[#equation-1] == "*" or
  543.                 equation[#equation-1] == "/" then
  544.                 table.insert(equation, #equation, "0")
  545.             elseif equation[#equation-1] == "^" then
  546.                 table.insert(equation, #equation, "1")
  547.             end
  548.             for i, v in pairs(calc) do
  549.                 if calc[i] == true then
  550.                     table.insert(equation, #equation, ")")
  551.                     calc[i] = false
  552.                 end
  553.             end
  554.             result = tostring(calculate(equation))
  555.         end
  556.     end
  557. end
Advertisement
Add Comment
Please, Sign In to add comment