Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- w, h = term.getSize()
- term.setBackgroundColor(colors.green)
- term.clear()
- term.setCursorPos(w/2 - 4, 1)
- term.write("Calculator")
- term.setCursorPos(1, 2)
- term.write(" + - * / ** ^ % ")
- term.setCursorPos(1, 4)
- term.write("Input: ")
- term.setCursorPos(8, 4)
- local input = read()
- local tArgs = {}
- local count = 1
- for i in string.gmatch(input, "%S+") do
- tArgs[count] = i
- count = count + 1
- end
- local nums = {}
- local operands = {}
- local result = nil
- for i, v in pairs(tArgs) do
- if tonumber(v) and v ~= "-" then
- nums[i] = tonumber(v)
- operands[i] = false
- else
- nums[i] = false
- operands[i] = v
- end
- end
- local initial = true
- for i = 1, #tArgs do
- if nums[i] == false then
- if initial then
- result = nums[i - 1]
- initial = false
- end
- if operands[i] == "+" then
- result = result + nums[i + 1]
- elseif operands[i] == "-" then
- result = result - nums[i + 1]
- elseif operands[i] == "*" then
- result = result * nums[i + 1]
- elseif operands[i] == "/" then
- result = result / nums[i + 1]
- elseif operands[i] == "**" or operands[i] == "^" then
- result = result ^ nums[i + 1]
- elseif operands[i] == "%" then
- result = result % nums[i + 1]
- else
- print("ERROR: Unsupported operation")
- result = nil
- end
- end
- end
- print(result)
- sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement