How do you run a mathematical equation when the operator is a variable?

So I am making a calculator and I have set it up so that the operator is one of four different operations. But i can not seem to figure out how to execute the equation while using a variable.

function execute_eqation()
    one = string1.Text
    two = string2.Text
    output.Text = one..op..two.." = "..one(tostring(op))two
    end
equals.MouseButton1Down:connect(execute_eqation)

maybe something like this

this code does not support multiplication because
4 + 6 * 2 = 16
but the code below will give the result 20 because it does not order the actions correctly but that is outside the scope of this question

local operations = {}
local actions = {}
local number = ""

operations["+"] = function(a, b)
    return a + b
end

operations["-"] = function(a, b)
    return a - b
end

one.Activated:Connect(function()
    number = number .. 1
end)

two.Activated:Connect(function()
    number = number .. 2
end)

plus.Activated:Connect(function()
    if number == "" then return end
    table.insert(actions, tonumber(number))
    number = ""

    table.insert(actions, "+")
end)

subtract.Activated:Connect(function()
    if number == "" then return end
    table.insert(actions, tonumber(number))
    number = ""

    table.insert(actions, "-")
end)

equals.Activated:Connect(function()
    if number == "" then return end
    table.insert(actions, tonumber(number))
    number = ""

    local value = 0
    local operation = "+"
    for i, action in ipairs(actions) do
        if operations[action] == nil then
            value = operations[operation](value, action)
        else
            operation = action
        end
    end
    actions = {}

    print(value)
end)
3 Likes

What you’re specifically asking for is not possible.

However, you can choose what to do based on what op is.

function execute_eqation()
    one = string1.Text
    two = string2.Text
    local result
    if op == "+" then
    	result = one + two
    elseif op == "-" then
    	result = one - two
    end -- etc. with all the other operators
    output.Text = one..op..two.." = " .. result
end

equals.MouseButton1Down:connect(execute_eqation)

Another way is to choose a function that represents your operator and then run it with your numbers.

local operators = {
	["+"] = function(one, two) return one + two end,
	["-"] = function(one, two) return one - two end,
	["*"] = function(one, two) return one * two end,
	["/"] = function(one, two) return one / two end,
	["asdf"] = function(one, two) return one * two * math.pi end, -- and anything else you can imagine
}

function execute_eqation()
    one = string1.Text
    two = string2.Text
    output.Text = one..op..two.." = " .. operators[op](one, two)
end

equals.MouseButton1Down:connect(execute_eqation)

This code assumes op is a string (and there was no need for tostring) and that it can’t be anything that’s not in the operators table.
edit: you may have to do one = tonumber(string1.Text) for example