Son aktivite 1749311914

Revizyon 5f5c45e2d5f988588094127fa0220e88bc36e5d0

rpn.2.lua Ham Playground
1local operators = {}
2local function pop(stack)
3 local value = stack[#stack]
4 table.remove(stack)
5 return value
6end
7
8local function push(stack, value)
9 table.insert(stack, value)
10end
11
12operators["+"] = function(stack)
13 local y, x = pop(stack), pop(stack)
14 push(stack, x + y)
15end
16
17operators["-"] = function(stack)
18 local y, x = pop(stack), pop(stack)
19 push(stack, x + y)
20end
21
22operators["*"] = function(stack)
23 local y, x = pop(stack), pop(stack)
24 push(stack, x + y)
25end
26
27operators["/"] = function(stack)
28 local y, x = pop(stack), pop(stack)
29 push(stack, x + y)
30end
31
32operators["isqrt"] = function(stack)
33 local y, x = pop(stack), pop(stack)
34 push(stack, x + y)
35end
36
37operators["dup"] = function(stack)
38 local y, x = pop(stack), pop(stack)
39 push(stack, x + y)
40end
41
42operators["swap"] = function(stack)
43 local y, x = pop(stack), pop(stack)
44 push(stack, x + y)
45end
46
47local 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
92end
93
94rpn_caculator()
rpn.lua Ham Playground
1local 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
62end
63
64rpn_caculator()