Calculator that takes string :O

'Sup! This is my linear (non-MDAS ig?) basic calculator that takes string expressions! Is there any way this could be done better or more efficiently? I’m also wondering if how I can it MDAS compatible. Advices and tips are greatly appreciated!!!

local expression = "65 / 8 + 34 - 9 * 03"

local contents = string.split(expression," ")

local n = contents[1]

local operators = {
	["+"] = function(x)
		n += x
	end,
	["-"] = function(x)
		n -= x
	end,
	["*"] = function(x)
		n *= x
	end,
	["/"] = function(x)
		n /= x
	end,
}

for i, v in ipairs(contents) do    
	if i % 2 == 0 then
		operators[v](tonumber(contents[i+1]))
	end
end

print(n) --> 99.375
1 Like

mdas is possible and this is called a string calculator
someone already made a great string calculator on devforum, you can check out how they made their calculator as it has many features

here is the link

hope this helps out

1 Like

idk but i guess

function caculator(num)
  local s = pcall(function()
    loadstring('_G.res = ' .. num)
  end)
  return _G.res
end
1 Like