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
- if os.getComputerLabel() == nil then username = "New User"
- else username = os.getComputerLabel()
- sp = textutils.slowPrint
- local function cPrint(height, string)
- xPos = screenWidht/2 - string.len(string)/2
- term.setCursorPos(xPos, height)
- term.write(string)
- end
- local function rPrint(heightm 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(7, "")
- sp("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)
- for i=1, #operations do
- if operationInput == operations[i] then
- if operations[i] = 1 then operation = 1
- elseif operations[i] == 2 then operation = 2
- elseif operations[i] == 3 then operation = 3
- elseif operations[i] == 4 then operation = 4
- elseif operations[i] == 5 then operation = 5
- end
- else
- operation = 0
- end
- end
- if operation == 0 then return answer = nil
- elseif operation == 1 then return answer = firstInput+secondInput
- elseif operation == 2 then return answer = firstInput-secondInput
- elseif operation == 3 then return answer = firstInput*secondInput
- elseif operation == 4 then return answer = firstInput/secondInput
- elseif operation == 5 then return answer = firstInput%secondInput end
- end
- while true do
- local answer = 0
- term.clear()
- term.setCursorPos(1,1)
- printWelcome()
- 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
- getInput()
- if determineOperation(operationInput, 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 break
- end
- -- End menu operations
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement