rpn.2.lua
· 2.0 KiB · Lua
Raw
Playground
local operators = {}
local function pop(stack)
local value = stack[#stack]
table.remove(stack)
return value
end
local function push(stack, value)
table.insert(stack, value)
end
operators["+"] = function(stack)
local y, x = pop(stack), pop(stack)
push(stack, x + y)
end
operators["-"] = function(stack)
local y, x = pop(stack), pop(stack)
push(stack, x + y)
end
operators["*"] = function(stack)
local y, x = pop(stack), pop(stack)
push(stack, x + y)
end
operators["/"] = function(stack)
local y, x = pop(stack), pop(stack)
push(stack, x + y)
end
operators["isqrt"] = function(stack)
local y, x = pop(stack), pop(stack)
push(stack, x + y)
end
operators["dup"] = function(stack)
local y, x = pop(stack), pop(stack)
push(stack, x + y)
end
operators["swap"] = function(stack)
local y, x = pop(stack), pop(stack)
push(stack, x + y)
end
local function rpn_caculator()
local char = io.read(1)
local acc, op = nil, {}
local data_stack = {}
local function push_accumulator()
if acc then
table.insert(data_stack, acc)
acc = nil
end
table.insert(op, char)
end
local function shift_digit()
acc = acc and (acc * 10 + tonumber(char)) or tonumber(char)
end
local function eval()
if #op ~= 0 then
local op_to_eval = table.concat(op)
op = {}
if operators[op_to_eval] then
operators[op_to_eval](data_stack)
end
elseif acc then
table.insert(data_stack, acc)
acc = nil
end
io.write("> ")
for _, data in ipairs(data_stack) do
io.write(data) io.write(" ")
end
io.write("\n")
end
while char do
if char:match("%s") then
eval()
elseif char:match("%d") then
shift_digit()
else
push_accumulator()
end
char = io.read(1)
end
end
rpn_caculator()
| 1 | local operators = {} |
| 2 | local function pop(stack) |
| 3 | local value = stack[#stack] |
| 4 | table.remove(stack) |
| 5 | return value |
| 6 | end |
| 7 | |
| 8 | local function push(stack, value) |
| 9 | table.insert(stack, value) |
| 10 | end |
| 11 | |
| 12 | operators["+"] = function(stack) |
| 13 | local y, x = pop(stack), pop(stack) |
| 14 | push(stack, x + y) |
| 15 | end |
| 16 | |
| 17 | operators["-"] = function(stack) |
| 18 | local y, x = pop(stack), pop(stack) |
| 19 | push(stack, x + y) |
| 20 | end |
| 21 | |
| 22 | operators["*"] = function(stack) |
| 23 | local y, x = pop(stack), pop(stack) |
| 24 | push(stack, x + y) |
| 25 | end |
| 26 | |
| 27 | operators["/"] = function(stack) |
| 28 | local y, x = pop(stack), pop(stack) |
| 29 | push(stack, x + y) |
| 30 | end |
| 31 | |
| 32 | operators["isqrt"] = function(stack) |
| 33 | local y, x = pop(stack), pop(stack) |
| 34 | push(stack, x + y) |
| 35 | end |
| 36 | |
| 37 | operators["dup"] = function(stack) |
| 38 | local y, x = pop(stack), pop(stack) |
| 39 | push(stack, x + y) |
| 40 | end |
| 41 | |
| 42 | operators["swap"] = function(stack) |
| 43 | local y, x = pop(stack), pop(stack) |
| 44 | push(stack, x + y) |
| 45 | end |
| 46 | |
| 47 | local function rpn_caculator() |
| 48 | local char = io.read(1) |
| 49 | local acc, op = nil, {} |
| 50 | local data_stack = {} |
| 51 | |
| 52 | local function push_accumulator() |
| 53 | if acc then |
| 54 | table.insert(data_stack, acc) |
| 55 | acc = nil |
| 56 | end |
| 57 | table.insert(op, char) |
| 58 | end |
| 59 | |
| 60 | local function shift_digit() |
| 61 | acc = acc and (acc * 10 + tonumber(char)) or tonumber(char) |
| 62 | end |
| 63 | |
| 64 | local function eval() |
| 65 | if #op ~= 0 then |
| 66 | local op_to_eval = table.concat(op) |
| 67 | op = {} |
| 68 | if operators[op_to_eval] then |
| 69 | operators[op_to_eval](data_stack) |
| 70 | end |
| 71 | elseif acc then |
| 72 | table.insert(data_stack, acc) |
| 73 | acc = nil |
| 74 | end |
| 75 | io.write("> ") |
| 76 | for _, data in ipairs(data_stack) do |
| 77 | io.write(data) io.write(" ") |
| 78 | end |
| 79 | io.write("\n") |
| 80 | end |
| 81 | |
| 82 | while char do |
| 83 | if char:match("%s") then |
| 84 | eval() |
| 85 | elseif char:match("%d") then |
| 86 | shift_digit() |
| 87 | else |
| 88 | push_accumulator() |
| 89 | end |
| 90 | char = io.read(1) |
| 91 | end |
| 92 | end |
| 93 | |
| 94 | rpn_caculator() |
rpn.lua
· 2.6 KiB · Lua
Raw
Playground
local function rpn_caculator()
local char = io.read(1)
local acc, op = nil, {}
local data_stack = {}
while char do
if char:match("%s") then
if #op ~= 0 then
local op_to_eval = table.concat(op)
op = {}
if op_to_eval == "+" then
local y, x = data_stack[#data_stack], data_stack[#data_stack - 1]
table.remove(data_stack) table.remove(data_stack)
table.insert(data_stack, x + y)
elseif op_to_eval == "*" then
local y, x = data_stack[#data_stack], data_stack[#data_stack - 1]
table.remove(data_stack) table.remove(data_stack)
table.insert(data_stack, x * y)
elseif op_to_eval == "/" then
local y, x = data_stack[#data_stack], data_stack[#data_stack - 1]
table.remove(data_stack) table.remove(data_stack)
table.insert(data_stack, x / y)
elseif op_to_eval == "-" then
local y, x = data_stack[#data_stack], data_stack[#data_stack - 1]
table.remove(data_stack) table.remove(data_stack)
table.insert(data_stack, x - y)
elseif op_to_eval == "isqrt" then
local x = data_stack[#data_stack]
table.remove(data_stack)
table.insert(data_stack, x)
elseif op_to_eval == "dup" then
local x = data_stack[#data_stack]
table.remove(data_stack)
table.insert(data_stack, x)
table.insert(data_stack, x)
elseif op_to_eval == "swap" then
local y, x = data_stack[#data_stack], data_stack[#data_stack - 1]
table.remove(data_stack) table.remove(data_stack)
table.insert(data_stack, y)
table.insert(data_stack, x)
end
elseif acc then
table.insert(data_stack, acc)
acc = nil
end
io.write("> ")
for _, data in ipairs(data_stack) do
io.write(data) io.write(" ")
end
io.write("\n")
elseif char:match("%d") then
acc = acc and (acc * 10 + tonumber(char)) or tonumber(char)
else
if acc then
table.insert(data_stack, acc)
acc = nil
end
table.insert(op, char)
end
char = io.read(1)
end
end
rpn_caculator()
| 1 | local function rpn_caculator() |
| 2 | local char = io.read(1) |
| 3 | local acc, op = nil, {} |
| 4 | local data_stack = {} |
| 5 | while char do |
| 6 | if char:match("%s") then |
| 7 | if #op ~= 0 then |
| 8 | local op_to_eval = table.concat(op) |
| 9 | op = {} |
| 10 | if op_to_eval == "+" then |
| 11 | local y, x = data_stack[#data_stack], data_stack[#data_stack - 1] |
| 12 | table.remove(data_stack) table.remove(data_stack) |
| 13 | table.insert(data_stack, x + y) |
| 14 | elseif op_to_eval == "*" then |
| 15 | local y, x = data_stack[#data_stack], data_stack[#data_stack - 1] |
| 16 | table.remove(data_stack) table.remove(data_stack) |
| 17 | table.insert(data_stack, x * y) |
| 18 | elseif op_to_eval == "/" then |
| 19 | local y, x = data_stack[#data_stack], data_stack[#data_stack - 1] |
| 20 | table.remove(data_stack) table.remove(data_stack) |
| 21 | table.insert(data_stack, x / y) |
| 22 | elseif op_to_eval == "-" then |
| 23 | local y, x = data_stack[#data_stack], data_stack[#data_stack - 1] |
| 24 | table.remove(data_stack) table.remove(data_stack) |
| 25 | table.insert(data_stack, x - y) |
| 26 | elseif op_to_eval == "isqrt" then |
| 27 | local x = data_stack[#data_stack] |
| 28 | table.remove(data_stack) |
| 29 | table.insert(data_stack, x) |
| 30 | elseif op_to_eval == "dup" then |
| 31 | local x = data_stack[#data_stack] |
| 32 | table.remove(data_stack) |
| 33 | table.insert(data_stack, x) |
| 34 | table.insert(data_stack, x) |
| 35 | elseif op_to_eval == "swap" then |
| 36 | local y, x = data_stack[#data_stack], data_stack[#data_stack - 1] |
| 37 | table.remove(data_stack) table.remove(data_stack) |
| 38 | table.insert(data_stack, y) |
| 39 | table.insert(data_stack, x) |
| 40 | end |
| 41 | elseif acc then |
| 42 | table.insert(data_stack, acc) |
| 43 | acc = nil |
| 44 | end |
| 45 | io.write("> ") |
| 46 | for _, data in ipairs(data_stack) do |
| 47 | io.write(data) io.write(" ") |
| 48 | end |
| 49 | io.write("\n") |
| 50 | elseif char:match("%d") then |
| 51 | acc = acc and (acc * 10 + tonumber(char)) or tonumber(char) |
| 52 | else |
| 53 | if acc then |
| 54 | table.insert(data_stack, acc) |
| 55 | acc = nil |
| 56 | end |
| 57 | table.insert(op, char) |
| 58 | end |
| 59 | |
| 60 | char = io.read(1) |
| 61 | end |
| 62 | end |
| 63 | |
| 64 | rpn_caculator() |