Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local firstInput = 0
- local secondInput = 0
- local operationInput = ""
- local screenWidth, screenHeight = term.getSize()
- local username = ""
- local selection = 1
- local answer = 0
- if os.getComputerLabel() == nil then username = "New User"
- else username = os.getComputerLabel() end
- local function cPrint(height, string)
- xPos = screenWidth/2 - string.len(string)/2
- term.setCursorPos(xPos, height)
- term.write(string)
- end
- local function rPrint(height, string)
- xPos = screenWidht - string.len(string)
- term.setCursorPos(xPos, height)
- term.write(string)
- end
- function getInput()
- cPrint(6, "First number: ")
- firstInput = read()
- cPrint(7, "Operation: ")
- operationInput = read()
- cPrint(8, "Second number: ")
- secondInput = read()
- end
- function printWelcome()
- cPrint("Welcome ".. username)
- sleep(2)
- term.clear()
- term.setCursorPos(1,1)
- end
- function printMenu()
- if selection == 1 then cPrint(6, "[ Calculate ]")
- else cPrint(6, " Calculate ") end
- if selection == 2 then cPrint(7, "[ Exit ]")
- else cPrint(7, " Exit ") end
- end
- function determineOperation(operationInput, answer)
- local operations = {'+', '-', '*', '/', '%'}
- local operation = 1
- operationInput = tostring(operationInput)
- if operationInput == operations[1] then operation = 1
- elseif operationInput == operations[2] then operation = 2
- elseif operationInput == operations[3] then operation = 3
- elseif operationInput == operations[4] then operation = 4
- elseif operationInput == operations[5] then operation = 5
- else operation = 0
- end
- if operation == 0 then answer = nil
- elseif operation == 1 then answer = firstInput+secondInput
- elseif operation == 2 then answer = firstInput-secondInput
- elseif operation == 3 then answer = firstInput*secondInput
- elseif operation == 4 then answer = firstInput/secondInput
- elseif operation == 5 then answer = firstInput%secondInput
- end
- end
- printWelcome()
- while true do
- answer = 0
- term.clear()
- term.setCursorPos(1,1)
- 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
- end
- if selection == 1 and key == 28 then
- term.clear()
- term.setCursorPos(1,1)
- getInput()
- if answer == nil then
- cPrint(10, "Not a valid operation.")
- sleep(2)
- else
- cPrint(10, "The answer is " ..answer)
- sleep(2)
- end
- elseif selection == 2 and key == 28 then term.clear(); term.setCursorPos(1,1); break
- end
- -- End menu operations
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement