# Calculator Concept Idea

Hello,

I thought of a concept idea of a working calculator in roblox studio. I want to hear what you guys think!

``````local text = script.Parent.Text
local possibleArithmitic = {"+", "-", "*", "/"}
local possibleNumbers = {"1","2","3","4","5","6","7","8","9","0"}
local symbol
local Numbers = {}

local function solve()
table.clear(Numbers)
symbol = ""

text = script.Parent.Text
for i,v in pairs(possibleArithmitic) do
if string.find(text.Text, v) then
symbol = v
local splitNumber = string.split(text.Text, v)
for i,v in pairs(splitNumber) do
table.insert(Numbers, v)
end
end
end
-- I think I could utilize a for loop here but I don't know how!
if symbol == "+" then
return Numbers[1] +  Numbers[2]
end
if symbol == "-" then
return Numbers[1] -  Numbers[2]
end
if symbol == "*" then
return Numbers[1] *  Numbers[2]
end
if symbol == "/" then
return Numbers[1] /  Numbers[2]
end
end

script.Parent.Equals.MouseButton1Up:Connect(function()
text.Text = solve()
end)

script.Parent.Clear.MouseButton1Up:Connect(function()
text.Text = ""
end)
``````

Basically this can only use one algorithmic term(+,-,*,/). To be able to di multiple terms, I need to figure out how to utilize a for loop on line 22-34.

Please let me know if you know how to make it!

A for loop is pretty simple, itâ€™s basically:

``````for wait(1) do
-- code
end
``````

You can also do a while loop to check if a condition is met but thatâ€™s not what you want Iâ€™d assume.

Also this should be in Scripting Support^

Yes, thank you but I need a sort of for loop to check the code without me having to type out each term. ie.

``````if symbol == "Algorithmic term" then
return Numbers[x] *  Numbers[x]
end
``````

Dont you mean

``````for task.wait(1) do
-- code
end
``````

Combining multiple terms is a very difficult process. Albeit, Iâ€™ll walk you through the most simple process I could think of for solving this problem

â€“CODE EDITSâ€“

First off, youâ€™ll need to make the `symbol` variable an array, like this:

``````symbol = {}
``````

Then, in the for loop you already have you have to input each arithmetic operation in order in the array, by using a similar process to which you are using to store the numbers.

Now comes the hard part.

Because of the Order of Operations, multiplication and division always come before addition and subtraction, so executing the math operation is tricky.

â€“FIRST ITERATIONâ€“

First, iterate through the arithmetic table.

For each * or / sign, execute the arithmetic for the two values involved in that specific operation

To get these values, refer to the arithmeticâ€™s operation index in the array and compare that to the numbers array. For instance, if you have a * sign on index 1, you know the two numbers in the numbers array are on index 1 and 2, and for a * sign on index 2, the 2 numbers involved are on index 2 and 3

When you calculate the value, assign it to the value of the first of the two indices involved in the number array and remove the second index. Make sure you also remove the arithetic operator used in the symbol array.

If you encounter a + or - sign, simply skip and move on to the next element in the array.

Once you iterate through the arithmetic array once, there should ONLY be + and - signs remaining, and the numbers array should have fewer indices.

â€“SECOND ITERATIONâ€“

Finally, iterate through a second time, this time getting the sum and difference for every value and storing it in a similar way to the previous iteration.

Once you are finished with all processes, the numbers array should be condensed to a single value at index 1, of which you would return.

There you have it! Let me know if you need any clarification.

1 Like

Couldnâ€™t I put the term and the position of the term to figure this out?

``````local Symbols= {
"+" = 5 --Demonstration, I know this wouldn't work for real
}

for i,v in pairs(Symbols) do
if v == "+" then
table.insert(Symbols, v)
v.weight = i --With this information I could loop through the table and figure out the operators
--position and based on that I could use PEMDAS to calculate
end
end
``````

Also I would have to loop though the string multiple times to make sure I got all the terms

Edit: The code works above with strictly one +, -, *, or /