Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Calculator
- local mainMenu = {
- "Calculations",
- "View History",
- "Shutdown"
- }
- local mathMenu = {
- "Addition",
- "Subtraction",
- "Multiplication",
- "Division",
- "Power",
- "Square Root",
- "Back"
- }
- local history = {}
- local function drawMenu(title, options, selected)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.blue)
- print("=== " .. title .. " ===")
- term.setTextColor(colors.white)
- print("Use UP/DOWN arrows and Enter to select.\n")
- for i, option in ipairs(options) do
- if i == selected then
- term.setTextColor(colors.yellow)
- print("> " .. option)
- term.setTextColor(colors.white)
- else
- print(" " .. option)
- end
- end
- end
- local function readNumber(prompt)
- while true do
- write(prompt)
- local input = read()
- local num = tonumber(input)
- if num then return num end
- term.setTextColor(colors.red)
- print("Invalid input, please enter a number.")
- term.setTextColor(colors.white)
- end
- end
- local function showHistory()
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.blue)
- print("=== Calculation History ===")
- term.setTextColor(colors.white)
- if #history == 0 then
- print("No calculations yet.")
- else
- for i, entry in ipairs(history) do
- term.setTextColor(colors.green)
- print(i .. ". " .. entry)
- end
- end
- term.setTextColor(colors.white)
- print("\nPress any key to return...")
- os.pullEvent("key")
- end
- local function mathOperations()
- local selected = 1
- drawMenu("Math Operations", mathMenu, selected)
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selected = (selected - 2) % #mathMenu + 1
- elseif key == keys.down then
- selected = (selected % #mathMenu) + 1
- elseif key == keys.enter then
- local op = mathMenu[selected]
- if op == "Back" then
- return
- elseif op == "Square Root" then
- local num = readNumber("Enter number: ")
- if num < 0 then
- term.setTextColor(colors.red)
- print("Error: Cannot take square root of a negative number.")
- else
- local result = math.sqrt(num)
- local output = string.format("√%.2f = %.4f", num, result)
- term.setTextColor(colors.green)
- print("Result: " .. output)
- table.insert(history, output)
- end
- else
- local a = readNumber("Enter first number: ")
- local b = readNumber("Enter second number: ")
- local result, output
- if op == "Addition" then
- result = a + b
- output = string.format("%.2f + %.2f = %.2f", a, b, result)
- elseif op == "Subtraction" then
- result = a - b
- output = string.format("%.2f - %.2f = %.2f", a, b, result)
- elseif op == "Multiplication" then
- result = a * b
- output = string.format("%.2f * %.2f = %.2f", a, b, result)
- elseif op == "Division" then
- if b == 0 then
- term.setTextColor(colors.red)
- print("Error: Division by zero.")
- else
- result = a / b
- output = string.format("%.2f / %.2f = %.2f", a, b, result)
- end
- elseif op == "Power" then
- result = a ^ b
- output = string.format("%.2f ^ %.2f = %.2f", a, b, result)
- end
- if output then
- term.setTextColor(colors.green)
- print("Result: " .. output)
- table.insert(history, output)
- end
- end
- term.setTextColor(colors.white)
- print("\nPress any key to return...")
- os.pullEvent("key")
- drawMenu("Math Operations", mathMenu, selected)
- end
- drawMenu("Math Operations", mathMenu, selected)
- end
- end
- local function main()
- local selected = 1
- drawMenu("Calculator", mainMenu, selected)
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selected = (selected - 2) % #mainMenu + 1
- elseif key == keys.down then
- selected = (selected % #mainMenu) + 1
- elseif key == keys.enter then
- local choice = mainMenu[selected]
- if choice == "Calculations" then
- mathOperations()
- elseif choice == "View History" then
- showHistory()
- elseif choice == "Shutdown" then
- term.setTextColor(colors.red)
- print("\nNeobox OS Alpha shutting down. Goodbye!")
- sleep(1.5)
- os.shutdown()
- end
- drawMenu("Calculator", mainMenu, selected)
- end
- drawMenu("Calculator", mainMenu, selected)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement