Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Simple Calculator Designed by NDFJay for AnthonyD98, all rights are reserved for AnthonyD98 ]]--
- local w,h = term.getSize()
- local n = 17
- local padY = 7
- local equation = {"(", ")"}
- term.clear()
- local function center(text, line)
- term.setCursorPos(math.ceil(w/2)-string.len(text)/2, line)
- print(text)
- end
- local function calculate(eq)
- if table.concat(eq) == "()" then
- eq = {"0"}
- elseif table.concat(eq) == "(.)" then
- eq = {"0"}
- end
- local sExpr = table.concat(eq)
- local fnMath, sErr = loadstring("return "..sExpr)
- if not fnMath then
- return "ERROR 1: "..sErr
- end
- setfenv(fnMath, math)
- local bSucc, vRes = pcall(fnMath)
- if not bSucc then
- return "ERROR 2: "..vRes
- else
- return vRes
- end
- end
- local function draw()
- center(string.rep("#",47),1)
- center("#"..string.rep(" ",45).."#",2)
- center("#"..string.rep(" ",45).."#",3)
- center("#"..string.rep(" ",45).."#",4)
- center(string.rep("#",47),5)
- term.setCursorPos(5,2)
- write(table.concat(equation))
- term.setCursorPos(1, math.floor(h-1))
- print('Press "Q" to exit')
- end
- local pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] "
- }
- local function selected()
- if n == 1 then
- pad ={
- " [>/<] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 2 then
- pad ={
- " [ / ] [>*<] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 3 then
- pad ={
- " [ / ] [ * ] [>-<] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 4 then
- pad ={
- " [ / ] [ * ] [ - ] [>+<]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 5 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [>7<] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 6 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [>8<] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 7 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [>9<] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 8 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [>.<]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 9 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [>4<] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 10 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [>5<] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 11 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [>6<] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 12 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [> <]";
- " [ 1 ] [ 2 ] [ 3 ] [>=<]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 13 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [>1<] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 14 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [>2<] [ 3 ] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 15 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [>3<] [ = ]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 16 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [> <]";
- " [ 1 ] [ 2 ] [ 3 ] [>=<]";
- " [ 0 ] [ Clear ]"
- }
- elseif n == 17 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ > 0 < ] [ Clear ]"
- }
- elseif n == 18 then
- pad ={
- " [ / ] [ * ] [ - ] [ + ]";
- " [ 7 ] [ 8 ] [ 9 ] [ . ]";
- " [ 4 ] [ 5 ] [ 6 ] [ ]";
- " [ 1 ] [ 2 ] [ 3 ] [ = ]";
- " [ 0 ] [>Clear<]"
- }
- end
- draw()
- for i,v in ipairs(pad) do
- center(v,padY+i)
- end
- end
- term.clear()
- selected()
- while true do
- local event, key = os.pullEvent("key")
- selected()
- if key == keys.right then
- n = n+1
- if n >= 18 then n = 18 end
- selected()
- elseif key == keys.left then
- n = n-1
- if n <= 1 then n = 1 end
- selected()
- elseif key == keys.down then
- n = n+4
- if n >= 18 then n = 18 end
- selected()
- elseif key == keys.up then
- n = n-4
- if n <= 1 then n = 1 end
- selected()
- elseif key == keys.enter then
- if n == 1 then
- table.insert(equation, #equation, "/")
- selected()
- elseif n == 2 then
- table.insert(equation, #equation, "*")
- selected()
- elseif n == 3 then
- table.insert(equation, #equation, "-")
- selected()
- elseif n == 4 then
- table.insert(equation, #equation, "+")
- selected()
- elseif n == 5 then
- table.insert(equation, #equation, "7")
- selected()
- elseif n == 6 then
- table.insert(equation, #equation, "8")
- selected()
- elseif n == 7 then
- table.insert(equation, #equation, "9")
- selected()
- elseif n == 8 then
- table.insert(equation, #equation, ".")
- selected()
- elseif n == 9 then
- table.insert(equation, #equation, "4")
- selected()
- elseif n == 10 then
- table.insert(equation, #equation, "5")
- selected()
- elseif n == 11 then
- table.insert(equation, #equation, "6")
- selected()
- elseif n == 12 then
- selected()
- term.setCursorPos(5,4)
- print(calculate(equation))
- elseif n == 16 then
- selected()
- term.setCursorPos(5,4)
- print(calculate(equation))
- elseif n == 13 then
- table.insert(equation, #equation, "1")
- selected()
- elseif n == 14 then
- table.insert(equation, #equation, "2")
- selected()
- elseif n == 15 then
- table.insert(equation, #equation, "3")
- selected()
- elseif n == 17 then
- table.insert(equation, #equation, "0")
- selected()
- elseif n == 18 then
- if table.concat(equation) ~= "()" then
- table.remove(equation, #equation - 1)
- end
- selected()
- end
- elseif key == 2 or key == 79 then
- table.insert(equation, #equation, "1")
- selected()
- elseif key == 3 or key == 80 then
- table.insert(equation, #equation, "2")
- selected()
- elseif key == 4 or key == 81 then
- table.insert(equation, #equation, "3")
- selected()
- elseif key == 5 or key == 75 then
- table.insert(equation, #equation, "4")
- selected()
- elseif key == 6 or key == 76 then
- table.insert(equation, #equation, "5")
- selected()
- elseif key == 7 or key == 77 then
- table.insert(equation, #equation, "6")
- selected()
- elseif key == 8 or key == 71 then
- table.insert(equation, #equation, "7")
- selected()
- elseif key == 9 or key == 72 then
- table.insert(equation, #equation, "8")
- selected()
- elseif key == 10 or key == 73 then
- table.insert(equation, #equation, "9")
- selected()
- elseif key == 11 or key == 82 then
- table.insert(equation, #equation, "0")
- selected()
- elseif key == 52 or key == 83 then
- table.insert(equation, #equation, ".")
- selected()
- elseif key == 53 or key == 181 then
- table.insert(equation, #equation, "/")
- selected()
- elseif key == 53 or key == 181 then
- table.insert(equation, #equation, "/")
- selected()
- elseif key == 55 then
- table.insert(equation, #equation, "*")
- selected()
- elseif key == 74 then
- table.insert(equation, #equation, "-")
- selected()
- elseif key == 78 then
- table.insert(equation, #equation, "+")
- selected()
- elseif key == 14 then
- if table.concat(equation) ~= "()" then
- table.remove(equation, #equation - 1)
- end
- selected()
- elseif key == keys.q then
- term.clear()
- term.setCursorPos(1,1)
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement