Advertisement
Rukerisu

Calculator

Jun 9th, 2025 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. -- Calculator
  2.  
  3. local mainMenu = {
  4.     "Calculations",
  5.     "View History",
  6.     "Shutdown"
  7. }
  8.  
  9. local mathMenu = {
  10.     "Addition",
  11.     "Subtraction",
  12.     "Multiplication",
  13.     "Division",
  14.     "Power",
  15.     "Square Root",
  16.     "Back"
  17. }
  18.  
  19. local history = {}
  20.  
  21. local function drawMenu(title, options, selected)
  22.     term.clear()
  23.     term.setCursorPos(1,1)
  24.  
  25.     term.setTextColor(colors.blue)
  26.     print("=== " .. title .. " ===")
  27.     term.setTextColor(colors.white)
  28.     print("Use UP/DOWN arrows and Enter to select.\n")
  29.  
  30.     for i, option in ipairs(options) do
  31.         if i == selected then
  32.             term.setTextColor(colors.yellow)
  33.             print("> " .. option)
  34.             term.setTextColor(colors.white)
  35.         else
  36.             print("  " .. option)
  37.         end
  38.     end
  39. end
  40.  
  41. local function readNumber(prompt)
  42.     while true do
  43.         write(prompt)
  44.         local input = read()
  45.         local num = tonumber(input)
  46.         if num then return num end
  47.         term.setTextColor(colors.red)
  48.         print("Invalid input, please enter a number.")
  49.         term.setTextColor(colors.white)
  50.     end
  51. end
  52.  
  53. local function showHistory()
  54.     term.clear()
  55.     term.setCursorPos(1,1)
  56.     term.setTextColor(colors.blue)
  57.     print("=== Calculation History ===")
  58.     term.setTextColor(colors.white)
  59.  
  60.     if #history == 0 then
  61.         print("No calculations yet.")
  62.     else
  63.         for i, entry in ipairs(history) do
  64.             term.setTextColor(colors.green)
  65.             print(i .. ". " .. entry)
  66.         end
  67.     end
  68.  
  69.     term.setTextColor(colors.white)
  70.     print("\nPress any key to return...")
  71.     os.pullEvent("key")
  72. end
  73.  
  74. local function mathOperations()
  75.     local selected = 1
  76.     drawMenu("Math Operations", mathMenu, selected)
  77.  
  78.     while true do
  79.         local event, key = os.pullEvent("key")
  80.         if key == keys.up then
  81.             selected = (selected - 2) % #mathMenu + 1
  82.         elseif key == keys.down then
  83.             selected = (selected % #mathMenu) + 1
  84.         elseif key == keys.enter then
  85.             local op = mathMenu[selected]
  86.             if op == "Back" then
  87.                 return
  88.             elseif op == "Square Root" then
  89.                 local num = readNumber("Enter number: ")
  90.                 if num < 0 then
  91.                     term.setTextColor(colors.red)
  92.                     print("Error: Cannot take square root of a negative number.")
  93.                 else
  94.                     local result = math.sqrt(num)
  95.                     local output = string.format("√%.2f = %.4f", num, result)
  96.                     term.setTextColor(colors.green)
  97.                     print("Result: " .. output)
  98.                     table.insert(history, output)
  99.                 end
  100.             else
  101.                 local a = readNumber("Enter first number: ")
  102.                 local b = readNumber("Enter second number: ")
  103.                 local result, output
  104.  
  105.                 if op == "Addition" then
  106.                     result = a + b
  107.                     output = string.format("%.2f + %.2f = %.2f", a, b, result)
  108.                 elseif op == "Subtraction" then
  109.                     result = a - b
  110.                     output = string.format("%.2f - %.2f = %.2f", a, b, result)
  111.                 elseif op == "Multiplication" then
  112.                     result = a * b
  113.                     output = string.format("%.2f * %.2f = %.2f", a, b, result)
  114.                 elseif op == "Division" then
  115.                     if b == 0 then
  116.                         term.setTextColor(colors.red)
  117.                         print("Error: Division by zero.")
  118.                     else
  119.                         result = a / b
  120.                         output = string.format("%.2f / %.2f = %.2f", a, b, result)
  121.                     end
  122.                 elseif op == "Power" then
  123.                     result = a ^ b
  124.                     output = string.format("%.2f ^ %.2f = %.2f", a, b, result)
  125.                 end
  126.  
  127.                 if output then
  128.                     term.setTextColor(colors.green)
  129.                     print("Result: " .. output)
  130.                     table.insert(history, output)
  131.                 end
  132.             end
  133.  
  134.             term.setTextColor(colors.white)
  135.             print("\nPress any key to return...")
  136.             os.pullEvent("key")
  137.             drawMenu("Math Operations", mathMenu, selected)
  138.         end
  139.         drawMenu("Math Operations", mathMenu, selected)
  140.     end
  141. end
  142.  
  143. local function main()
  144.     local selected = 1
  145.     drawMenu("Calculator", mainMenu, selected)
  146.  
  147.     while true do
  148.         local event, key = os.pullEvent("key")
  149.         if key == keys.up then
  150.             selected = (selected - 2) % #mainMenu + 1
  151.         elseif key == keys.down then
  152.             selected = (selected % #mainMenu) + 1
  153.         elseif key == keys.enter then
  154.             local choice = mainMenu[selected]
  155.             if choice == "Calculations" then
  156.                 mathOperations()
  157.             elseif choice == "View History" then
  158.                 showHistory()
  159.             elseif choice == "Shutdown" then
  160.                 term.setTextColor(colors.red)
  161.                 print("\nNeobox OS Alpha shutting down. Goodbye!")
  162.                 sleep(1.5)
  163.                 os.shutdown()
  164.             end
  165.             drawMenu("Calculator", mainMenu, selected)
  166.         end
  167.         drawMenu("Calculator", mainMenu, selected)
  168.     end
  169. end
  170.  
  171. main()
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement