Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ShadOS Calculator by PaymentOption --
- VERSION = "Alpha 1.0"
- ----------------------------------------
- -- VARS --
- sExpression = ""
- nAnswer = nil
- screenWidth, screenHeight = term.getSize()
- selection = 1
- ----------
- -- Helper Functions --
- function cPrint(height, string)
- local xPos = screenWidth/2 - string.len(string)/2
- term.setCursorPos(xPos, height); term.write(string)
- end
- function rPrint(height, string)
- local xPos = screenWidth - string.len(string)
- term.setCursorPos(xPos, height); term.write(string)
- end
- function clear() term.clear(); term.setCursorPos(1,1) end
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- ----------------------
- -- Application related functions --
- function printLogo()
- term.setCursorPos(2, 2)
- print(" -_-/ ,, |\\ ,-||-, -_-/ ")
- print(" (_ / || _ \\\\ ('||| ) (_ / ")
- print(" (_ --_ ||/\\\\ < \\, / \\\\ (( |||--)) (_ --_ ")
- print(" --_ ) || || /-|| || || (( |||--)) --_ ) ")
- print(" _/ )) || || (( || || || ( / | ) _/ )) ")
- print(" (_-_- \\\\ |/ \\/\\\\ \\\\/ -____- (_-_- ")
- print(" _/ ")
- print(" ,- _~. ,, ")
- print(" (' /| _ || ")
- print(" (( || < \\, || _-_ ")
- print(" (( || /-|| || || ")
- print(" ( / | (( || || || ")
- print(" -____- \\/\\\\ \\\\ \\\\,/ ")
- end
- function printBorder() --Print Logo then border!
- local w,h = term.getSize()
- write(" ".. string.rep('*', w-2).."\n")
- for i=1, h-2 do write(" "..'*'..string.rep(" ", w-4)..'*'.."\n") end
- write(" ".. string.rep('*', w-2))
- cPrint(2, "ShadOS Calculator")
- rPrint(17, "Version: "..VERSION.."*")
- if bGetInput == true then cPrint(5, "Format: Num1 OPERATOR Num2") end
- end
- function getInput()
- local nFirstNum = 0;
- local nSecondNum = 0;
- local cOperator = '';
- local tExpression = {}
- local sTempExpression = "" -- This will be used to grab the expression via read()
- bGetInput = true
- while true do
- clear()
- printBorder()
- term.setCursorPos(3,17); term.write("Write 'exit' to exit")
- if nAnswer ~= nil then term.setCursorPos(3, 3); term.write("ans: "..nAnswer..string.rep(" ", screenWidth-string.len("ans: "..nAnswer.." "))) end
- term.write("*"..string.rep(" ", screenWidth-4).."*")
- term.setCursorPos(5, 7); term.write("Expression: ")
- sTempExpression = tostring(read())
- if sTempExpression == "exit" then bGetInput = false; break
- else
- tExpression = split(sTempExpression, " ")
- if tExpression[1] == "ans"then
- if nAnswer ~= nil then tExpression[1] = nAnswer
- else cPrint(8, "Error: No value held by prior answer"); sleep(1.3) end
- elseif tExpression[2] == "ans" then
- if nAnswer ~= nil then tExpression[1] = nAnswer
- else cPrint(8, "Error: No value held by prior answer"); sleep(1.3) end
- end
- nFirstNum = tExpression[1]
- cOperator = tExpression[2]
- nSecondNum = tExpression[3]
- -- Long ass operator checking.... --
- if cOperator == '+' then nAnswer = nFirstNum+nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3)
- elseif cOpeartor == '-' then nAnswer = nFirstNum-nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3)
- elseif cOperator == '*' then nAnswer = nFirstNum*nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3)
- elseif cOperator == '/' then
- if nSecondNum == 0 then cPrint(8, "Cannot divide by 0"); sleep(1.3)
- else nAnswer = nFirstNum/nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3) end
- elseif cOperator == '%' then nAnswer = nFirstNum%nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3)
- else cPrint(8, "Invalid expression: Operator unknown."); sleep(1.3) end
- end
- end
- end
- function printMenu()
- if selection == 1 then cPrint(6, "[ Custom Expression ]")
- else cPrint(6, " Custom Expression ") end
- if selection == 2 then cPrint(7, "[ Exit ]")
- else cPrint(7, " Exit ") end
- end
- -----------------------------------
- clear()
- printLogo()
- sleep(2)
- while true do
- clear()
- printBorder()
- printMenu()
- event, key = os.pullEvent("key")
- if key == 200 and selection > 1 then selection = selection-1
- elseif key == 208 and selection < 2 then selection = selection+1
- elseif key == 28 and selection == 1 then getInput()
- elseif key == 28 and selection == 2 then clear(); break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement