-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_calculator_v2.lua
More file actions
105 lines (89 loc) · 2.36 KB
/
Copy pathstring_calculator_v2.lua
File metadata and controls
105 lines (89 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
--Personal implementation of the shunting yard algorithm
local gsub = string.gsub
local find = string.find
local sub = string.sub
local getmath = {
["^"] = function (a,b) return a^b end,
["/"] = function (a,b) return a/b end,
["*"] = function (a,b) return a*b end,
["%"] = function (a,b) return a%b end,
["+"] = function (a,b) return a+b end,
["-"] = function (a,b) return a-b end
}
local symbol={
["^"]=1,
["/"]=2,
["*"]=2,
["%"]=2,
["+"]=3,
["-"]=3,
[")"]=4,
["("]=4
}
local function expr(val)
if find(val, "[0-9]") == nil then
return val
end
val = gsub(val," ","")
local symbolstack = {}
local numberstack = {}
local term=""
local closed
local len_number, len_symbol = 0,0
local len_val = #val
for i=1,len_val do
local temp_term = sub(val,i,i)
if symbol[temp_term] == nil or ((temp_term == "-" or temp_term == "+") and (symbol[sub(val,i-1,i-1)] or i-1 == 0) and sub(val,i-1,i-1) ~= ")") then
if symbol[sub(val,i+1,i+1)] or i == len_val then
len_number = len_number + 1
numberstack[len_number] = term .. temp_term
term = ""
else
term = term .. temp_term
end
else
if symbolstack[len_symbol] and symbol[symbolstack[len_symbol]] <= symbol[temp_term] and temp_term ~= "(" and temp_term ~= ")" then
len_number = len_number + 1
numberstack[len_number] = symbolstack[len_symbol]
symbolstack[len_symbol] = temp_term
else
len_symbol = len_symbol + 1
symbolstack[len_symbol] = temp_term
end
if temp_term == ")" then
closed = true
end
end
if closed then
len_symbol = len_symbol - 1
while symbolstack[len_symbol] ~= "(" and len_symbol > 0 do
len_number = len_number + 1
numberstack[len_number] = symbolstack[len_symbol]
len_symbol = len_symbol - 1
end
if len_symbol == 0 then
error("Brackets are unlinked.")
end
closed = false
len_symbol = len_symbol - 1
end
end
for i = len_symbol, 1, -1 do
len_number = len_number + 1
numberstack[len_number] = symbolstack[i]
end
local tempstack = {}
local len_temp = 0
for i = 1, len_number do
if symbol[numberstack[i]] then
tempstack[len_temp - 1] = getmath[numberstack[i]](tempstack[len_temp - 1], tempstack[len_temp])
len_temp = len_temp - 1
else
len_temp = len_temp + 1
tempstack[len_temp] = numberstack[i]
end
end
if tempstack[1] * 2 then
return tonumber(tempstack[1])
end
end