Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 3rd attempt in string algo math thing
- -- the DO ALL MATH algorithm actually works, however it thinks all decimals are numbers which will be fixed later.
- str = "10000/333333333 is probably a much larger number than 0.9999999999+0.0000000001, but I'm not really too sure. Also 23*3 is a cool number."
- function isnumber(x)
- x = tostring(x)
- local numbs = {"1","2","3","4","5","6","7","8","9","0","."}
- for i=1,#numbs do
- if numbs[i] == x then
- return true
- end
- end
- return false
- end
- function isoperator(x)
- local numbs = {"*","/","+","-"}
- for i=1,#numbs do
- if numbs[i] == x then
- return {true,numbs[i]}
- end
- end
- return {false,nil}
- end
- function calculate(x,y,z)
- if y == "+" then
- return x+z
- elseif y == "-" then
- return x-z
- elseif y == "*" then
- return x*z
- elseif y == "/" then
- return x/z
- end
- end
- local num = false
- local firstfound = false
- local sub1
- local sub2
- local sub3
- local op
- local strr = #str
- function resetvars()
- num = false
- firstfound = false
- sub1 = nil
- sub2 = nil
- sub3 = nil
- op = nil
- end
- for i=1,strr do
- local x = string.sub(str,i,i)
- if isnumber(x) == true and num == false then
- if x == "." then
- if isnumber(string.sub(str,i+1,i+1)) == true then
- sub1 = i
- firstfound = true
- num = true
- end
- else
- sub1 = i
- firstfound = true
- num = true
- end
- end
- if isnumber(x) == false and num == true and sub1 ~= nil and sub2 == nil and sub3 == nil and isoperator(x)[1] == true then
- sub2 = i
- op = string.sub(str,sub2,sub2)
- elseif isnumber(x) == false and num == true and sub1 ~= nil and sub2 == nil and sub3 == nil and isoperator(x)[1] == false then
- resetvars()
- end
- if isnumber(x) == false and num == true and sub1 ~= nil and sub2 ~= nil and sub3 == nil then
- if i-sub2 > 0 then
- if x == "." then
- if isnumber(string.sub(str,i+1,i+1)) == true then
- sub3 = i
- end
- else
- sub3 = i
- end
- end
- end
- if (num == true and sub1 ~= nil and sub2 ~= nil and sub3 ~= nil) then
- local number1 = tonumber(string.sub(str,sub1,sub2-1))
- local number2 = tonumber(string.sub(str,sub2+1,sub3-1))
- local sum = calculate(number1,op,number2)
- local k = #str
- str = string.sub(str,1,sub1-1)..tostring(sum)..string.sub(str,sub3)
- local k2 = k-#str
- i = i-k2
- strr = #str
- resetvars()
- end
- end
- print(str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement