Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- time to remake the strange do all math algo.
- str = "8*3 is my favorite number, but it's not as cool as 3*42. 827 Is a good one too."
- function isnumber(x)
- local numbers = {"1","2","3","4","5","6","7","8","9","0","."}
- for i=1,#numbers do
- if tonumber(x) == tonumber(numbers[i]) then
- return true
- end
- end
- return false
- end
- function isoperator(x)
- local operators = {"*","-","+","/"}
- for i=1,#operators do
- if x == operator[i] then
- return {true,x}
- end
- end
- return {false,nil}
- end
- function calculate(x,sign,y)
- if sign == "+" then
- local sum = tonumber(x)+tonumber(y)
- return sum
- elseif sign == "-" then
- local sum = tonumber(x)-tonumber(y)
- return sum
- elseif sign == "/" then
- local sum = tonumber(x)/tonumber(y)
- return sum
- elseif sign == "*" then
- local sum = tonumber(x)*tonumber(y)
- return sum
- end
- end
- local sub1
- local sub2
- local sub3
- local op
- for i=1,#str do
- if isnumber(string.sub(str,i,i)) == true and sub1 == nil and op == nil then
- print('hi')
- sub1 = i
- elseif isnumber(string.sub(str,i,i)) == false and sub1 == true and isoperator(string.sub(str,i,i))[1] == true and op == nil then
- print('hi again')
- op = isoperator(string.sub(str,i,i))[2]
- sub3 = i
- elseif op ~= nil and isnumber(string.sub(str,i,i)) == false then
- sub2 = i
- local number1 = string.sub(sub1,sub3-1)
- local number2 = string.sub(sub3+1,sub2)
- local math = calculate(number1,op,number2)
- local idiff = #str
- str = string.sub(str,1,sub1-1)..math..(string.sub(str,sub3+1))
- idiff = idiff - #str
- i = i - idiff
- else
- --sub1 = nil
- --sub2 = nil
- --op = nil
- end
- end
- print(str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement