Advertisement
ndfjay

Calculator Alpha

Jan 15th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.80 KB | None | 0 0
  1. --[[ Simple Calculator Designed by NDFJay for AnthonyD98, all rights are reserved for AnthonyD98 ]]--
  2.  
  3. local w,h = term.getSize()
  4. local n = 17
  5. local padY = 7
  6. local equation = {"(", ")"}
  7. term.clear()
  8. local function center(text, line)
  9.   term.setCursorPos(math.ceil(w/2)-string.len(text)/2, line)
  10.   print(text)
  11. end
  12. local function calculate(eq)
  13.  if table.concat(eq) == "()" then
  14.   eq = {"0"}
  15.  elseif table.concat(eq) == "(.)" then
  16.   eq = {"0"}
  17.  end
  18.  local sExpr = table.concat(eq)
  19.  local fnMath, sErr = loadstring("return "..sExpr)
  20.  if not fnMath then
  21.   return "ERROR 1: "..sErr
  22.  end
  23.  setfenv(fnMath, math)
  24.  local bSucc, vRes = pcall(fnMath)
  25.  if not bSucc then
  26.   return "ERROR 2: "..vRes
  27.  else
  28.   return vRes
  29.  end
  30. end
  31. local function draw()
  32. center(string.rep("#",47),1)
  33. center("#"..string.rep(" ",45).."#",2)
  34. center("#"..string.rep(" ",45).."#",3)
  35. center("#"..string.rep(" ",45).."#",4)
  36. center(string.rep("#",47),5)
  37. term.setCursorPos(5,2)
  38. write(table.concat(equation))
  39. term.setCursorPos(1, math.floor(h-1))
  40. print('Press "Q" to exit')
  41.  
  42. end
  43. local pad ={
  44.         " [ / ] [ * ] [ - ] [ + ]";
  45.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  46.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  47.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  48.         "      [     0     ]     "
  49. }
  50.  
  51. local function selected()
  52.     if n == 1 then
  53. pad ={
  54.         " [>/<] [ * ] [ - ] [ + ]";
  55.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  56.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  57.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  58.         " [     0     ] [ Clear ]"
  59. }
  60.     elseif n == 2 then
  61. pad ={
  62.         " [ / ] [>*<] [ - ] [ + ]";
  63.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  64.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  65.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  66.         " [     0     ] [ Clear ]"
  67. }
  68.     elseif n == 3 then
  69. pad ={
  70.         " [ / ] [ * ] [>-<] [ + ]";
  71.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  72.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  73.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  74.         " [     0     ] [ Clear ]"
  75. }
  76.     elseif n == 4 then
  77. pad ={
  78.         " [ / ] [ * ] [ - ] [>+<]";
  79.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  80.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  81.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  82.         " [     0     ] [ Clear ]"
  83. }
  84.     elseif n == 5 then
  85. pad ={
  86.         " [ / ] [ * ] [ - ] [ + ]";
  87.         " [>7<] [ 8 ] [ 9 ] [ . ]";
  88.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  89.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  90.         " [     0     ] [ Clear ]"
  91. }
  92.     elseif n == 6 then
  93. pad ={
  94.         " [ / ] [ * ] [ - ] [ + ]";
  95.         " [ 7 ] [>8<] [ 9 ] [ . ]";
  96.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  97.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  98.         " [     0     ] [ Clear ]"
  99. }
  100.     elseif n == 7 then
  101. pad ={
  102.         " [ / ] [ * ] [ - ] [ + ]";
  103.         " [ 7 ] [ 8 ] [>9<] [ . ]";
  104.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  105.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  106.         " [     0     ] [ Clear ]"
  107. }
  108.     elseif n == 8 then
  109. pad ={
  110.         " [ / ] [ * ] [ - ] [ + ]";
  111.         " [ 7 ] [ 8 ] [ 9 ] [>.<]";
  112.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  113.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  114.         " [     0     ] [ Clear ]"
  115. }
  116.     elseif n == 9 then
  117. pad ={
  118.         " [ / ] [ * ] [ - ] [ + ]";
  119.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  120.         " [>4<] [ 5 ] [ 6 ] [   ]";
  121.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  122.         " [     0     ] [ Clear ]"
  123. }
  124.     elseif n == 10 then
  125. pad ={
  126.         " [ / ] [ * ] [ - ] [ + ]";
  127.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  128.         " [ 4 ] [>5<] [ 6 ] [   ]";
  129.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  130.         " [     0     ] [ Clear ]"
  131. }
  132.     elseif n == 11 then
  133. pad ={
  134.         " [ / ] [ * ] [ - ] [ + ]";
  135.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  136.         " [ 4 ] [ 5 ] [>6<] [   ]";
  137.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  138.         " [     0     ] [ Clear ]"
  139. }
  140.     elseif n == 12 then
  141. pad ={
  142.         " [ / ] [ * ] [ - ] [ + ]";
  143.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  144.         " [ 4 ] [ 5 ] [ 6 ] [> <]";
  145.         " [ 1 ] [ 2 ] [ 3 ] [>=<]";
  146.         " [     0     ] [ Clear ]"
  147. }
  148.     elseif n == 13 then
  149. pad ={
  150.         " [ / ] [ * ] [ - ] [ + ]";
  151.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  152.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  153.         " [>1<] [ 2 ] [ 3 ] [ = ]";
  154.         " [     0     ] [ Clear ]"
  155. }
  156.     elseif n == 14 then
  157. pad ={
  158.         " [ / ] [ * ] [ - ] [ + ]";
  159.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  160.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  161.         " [ 1 ] [>2<] [ 3 ] [ = ]";
  162.         " [     0     ] [ Clear ]"
  163. }
  164.     elseif n == 15 then
  165. pad ={
  166.         " [ / ] [ * ] [ - ] [ + ]";
  167.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  168.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  169.         " [ 1 ] [ 2 ] [>3<] [ = ]";
  170.         " [     0     ] [ Clear ]"
  171. }
  172.     elseif n == 16 then
  173. pad ={
  174.         " [ / ] [ * ] [ - ] [ + ]";
  175.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  176.         " [ 4 ] [ 5 ] [ 6 ] [> <]";
  177.         " [ 1 ] [ 2 ] [ 3 ] [>=<]";
  178.         " [     0     ] [ Clear ]"
  179. }
  180.     elseif n == 17 then
  181. pad ={
  182.         " [ / ] [ * ] [ - ] [ + ]";
  183.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  184.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  185.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  186.         " [ >   0   < ] [ Clear ]"
  187. }
  188.     elseif n == 18 then
  189. pad ={
  190.         " [ / ] [ * ] [ - ] [ + ]";
  191.         " [ 7 ] [ 8 ] [ 9 ] [ . ]";
  192.         " [ 4 ] [ 5 ] [ 6 ] [   ]";
  193.         " [ 1 ] [ 2 ] [ 3 ] [ = ]";
  194.         " [     0     ] [>Clear<]"
  195. }
  196.  
  197.     end
  198. draw()
  199. for i,v in ipairs(pad) do
  200.     center(v,padY+i)
  201. end
  202. end
  203. term.clear()
  204. selected()
  205. while true do
  206. local event, key = os.pullEvent("key")
  207. selected()
  208. if key == keys.right then
  209.     n = n+1
  210.     if n >= 18 then n = 18 end
  211.     selected()
  212.         elseif key == keys.left then
  213.             n = n-1
  214.             if n <= 1 then n = 1 end
  215.             selected()
  216.                 elseif key == keys.down then
  217.                     n = n+4
  218.                     if n >= 18 then n = 18 end
  219.                     selected()
  220.                         elseif key == keys.up then
  221.                             n = n-4
  222.                             if n <= 1 then n = 1 end
  223.                             selected()
  224.                                 elseif key == keys.enter then
  225.                                     if n == 1 then
  226.                                     table.insert(equation, #equation, "/")
  227.                                     selected()
  228.                                         elseif n == 2 then
  229.                                         table.insert(equation, #equation, "*")
  230.                                         selected()
  231.                                        
  232.                                         elseif n == 3 then                                     
  233.                                         table.insert(equation, #equation, "-")
  234.                                         selected()
  235.                                        
  236.                                         elseif n == 4 then                                     
  237.                                         table.insert(equation, #equation, "+")
  238.                                         selected()
  239.                                        
  240.                                         elseif n == 5 then
  241.                                         table.insert(equation, #equation, "7")
  242.                                         selected()
  243.                                        
  244.                                         elseif n == 6 then
  245.                                         table.insert(equation, #equation, "8")
  246.                                         selected()
  247.                                        
  248.                                         elseif n == 7 then
  249.                                         table.insert(equation, #equation, "9")
  250.                                         selected()
  251.                                        
  252.                                         elseif n == 8 then
  253.                                         table.insert(equation, #equation, ".")
  254.                                         selected()
  255.                                        
  256.                                         elseif n == 9 then
  257.                                         table.insert(equation, #equation, "4")
  258.                                         selected()
  259.                                        
  260.                                         elseif n == 10 then
  261.                                         table.insert(equation, #equation, "5")
  262.                                         selected()
  263.                                        
  264.                                         elseif n == 11 then
  265.                                         table.insert(equation, #equation, "6")
  266.                                         selected()
  267.                                        
  268.                                         elseif n == 12 then
  269.                                         selected()
  270.                                         term.setCursorPos(5,4)
  271.                                         print(calculate(equation))
  272.                                        
  273.                                         elseif n == 16 then
  274.                                         selected()
  275.                                         term.setCursorPos(5,4)
  276.                                         print(calculate(equation))
  277.                                        
  278.                                         elseif n == 13 then
  279.                                         table.insert(equation, #equation, "1")
  280.                                         selected()
  281.                                        
  282.                                         elseif n == 14 then
  283.                                         table.insert(equation, #equation, "2")
  284.                                         selected()
  285.                                        
  286.                                         elseif n == 15 then
  287.                                         table.insert(equation, #equation, "3")
  288.                                         selected()
  289.                                        
  290.                                         elseif n == 17 then
  291.                                         table.insert(equation, #equation, "0")
  292.                                         selected()
  293.                                        
  294.                                         elseif n == 18 then
  295.                                         if table.concat(equation) ~= "()" then
  296.                                         table.remove(equation, #equation - 1)
  297.                                         end
  298.                                         selected()
  299.                                    
  300.                                     end
  301.                                         elseif key == 2 or key == 79 then
  302.                                         table.insert(equation, #equation, "1")
  303.                                         selected()                                     
  304.                                             elseif key == 3 or key == 80 then
  305.                                             table.insert(equation, #equation, "2")
  306.                                             selected()
  307.                                                 elseif key == 4 or key == 81 then
  308.                                                 table.insert(equation, #equation, "3")
  309.                                                 selected()
  310.                                                     elseif key == 5 or key == 75 then
  311.                                                     table.insert(equation, #equation, "4")
  312.                                                     selected()
  313.                                                         elseif key == 6 or key == 76 then
  314.                                                         table.insert(equation, #equation, "5")
  315.                                                         selected()
  316.                                                             elseif key == 7 or key == 77 then
  317.                                                             table.insert(equation, #equation, "6")
  318.                                                             selected()
  319.                                                                 elseif key == 8 or key == 71 then
  320.                                                                 table.insert(equation, #equation, "7")
  321.                                                                 selected()
  322.                                                                     elseif key == 9 or key == 72 then
  323.                                                                     table.insert(equation, #equation, "8")
  324.                                                                     selected()
  325.                                                                         elseif key == 10 or key == 73 then
  326.                                                                         table.insert(equation, #equation, "9")
  327.                                                                         selected()
  328.                                                                             elseif key == 11 or key == 82 then
  329.                                                                             table.insert(equation, #equation, "0")
  330.                                                                             selected()
  331.                                                                                 elseif key == 52 or key == 83 then
  332.                                                                                 table.insert(equation, #equation, ".")
  333.                                                                                 selected()
  334.                                                                                     elseif key == 53 or key == 181 then
  335.                                                                                     table.insert(equation, #equation, "/")
  336.                                                                                     selected()
  337.                                                                                         elseif key == 53 or key == 181 then
  338.                                                                                         table.insert(equation, #equation, "/")
  339.                                                                                         selected()
  340.                                                                                             elseif key == 55 then
  341.                                                                                             table.insert(equation, #equation, "*")
  342.                                                                                             selected()
  343.                                                                                                 elseif key == 74 then
  344.                                                                                                 table.insert(equation, #equation, "-")
  345.                                                                                                 selected()
  346.                                                                                                     elseif key == 78 then
  347.                                                                                                     table.insert(equation, #equation, "+")
  348.                                                                                                     selected()
  349.                                                                                                         elseif key == 14 then
  350.                                                                                                         if table.concat(equation) ~= "()" then
  351.                                                                                                         table.remove(equation, #equation - 1)
  352.                                                                                                         end
  353.                                                                                                         selected()
  354.                                                                                                             elseif key == keys.q then
  355.                                                                                                             term.clear()
  356.                                                                                                             term.setCursorPos(1,1)
  357.                                                                                                             break
  358. end
  359.  
  360. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement