Calculator using strings

so @edozune and @Super_pro322222 i am sorry to say but both of these methods are not what I want you see I am making a calculator! and it shouldnt use loadstring cause its bad backdoors and stuff anyways the equation is not always 10/10/10+10 it can be different most of the times

so if you’re making a calculator then just replace 10/10/10 with input1/input2/input3 + input4

inputs should be tonumber(string)

i think you still dont get it
so another example

lets say a player inputted 10+10-10
now if i do string.split(“10+10-10”,"+") it would return {“10 10-10”} see thats the problem
roblox would just error unless… wait a second HOLD UP

This isn’t something that can be done easily. You will need to learn to write a parser, and it’s not something that can be done in an afternoon. Do a search for arithmetic parser.

yea i know its complex as hell but rn i am trying to make a simpler one using tables ill make sure to keep all of you updated!

well I made this and it sucks at math more than a 3rd grader

local subtab = {}
local addtab = {}
local function solve(input)
	local sub = string.split(input,"-")
	for i,v in pairs(sub) do
		if not string.find(v,"+") then
			table.insert(subtab,v)
		else
			local add = string.split(v,"+")
			for _,a in pairs(add) do
				table.insert(addtab,a)
			end
		end
	end
	
	local num = 0
	for _,a in pairs(addtab) do
		num += a
	end
	for _,s in pairs(subtab) do
		num -= s
	end
	print(num)
end

solve("5-10-10")

well it works now

local r = loadstring("5-10-10")
r()

:+1:
yup it surely does

What if you’ve actually gotten the numbers and operations as strings?

Like, make a table for all possible number and operation values:

numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
operations = {"+", "-", "*", "/", "^", "%", "sqr", "log"}

Then make it so that it’ll find if the string has any of that in there, it’ll find the first and add it onto an expressed equation. Just an idea.

that is a fine idea and would try that in the future but rn ama stick to da 2 lines of code that makes everything work : ) (yea i gave up)

I’ll try to do this on my own research, would it be okay to PM you when I found a solution? Maybe you can use it for your calculator.

yea sure i dont see why not! and its not like necessary for you to do search you know?

Yeah, but you wanted to do it without using a loadstring and I know it’s possible. So, I just wanted to kind of fulfill the initial request, which was doing it without the loadstring.

1 Like

hmm that is right and thank you very much!

1 Like

@Qinrir, JUST FOUND A WAY TO DO IT WITHOUT LOADSTRING.

So, using a module called: Luaxp, it can help with making a string a mathmatical function, and it’ll automatically return as a string back.

Put the module in ReplicatedStorage and put this code inside of your script, modify it to add more conditions and valla!

local parser = require(game.ReplicatedStorage:WaitForChild("MainModule"))
local expression

TextBox = script.Parent.TextBox

if TextBox then


	for buttons, button in ipairs(script.Parent:GetChildren()) do

		if button:IsA("TextButton") then

			if button.Name == "=" then
				
				button.MouseButton1Click:Connect(function()
					
				
					expression = parser.evaluate(TextBox.Text)
					wait()
					TextBox.Text = expression
				end)
				
			elseif button.Name == "C" then
				
				button.MouseButton1Click:Connect(function()
					
					TextBox.Text = ""
				end)
				
			elseif button.Name == "CE" then
				
				button.MouseButton1Click:Connect(function()
					
					TextBox.Text = TextBox.Text:gsub(".?$","")
				end)
				
				-- Going to assume you have more buttons like ^, sqr, log, etc. Like how the if statements are set up, make the button name be the symbol, and then put the symbol in as a text.
			else
				
				button.MouseButton1Click:Connect(function()
					
					TextBox.Text ..= button.Text
				end)
			end
		end
	end
end
2 Likes

WAIT WHAT! I need to try this! ASAP

it works! I am just… surprised! Time to make a plugin or something out of it and yea still thank you so much for the help this really helps!

1 Like

hey so how do i do race to power? for somereason it does addition and % also doesnt work for me

You can look into the module and see how some of the functions are made. I know they made something that does percent and power, it’s just in a more complicated way. You could give Birdelther a PM and ask about the module and it’s functions to remove the hassle. If you don’t want to though, I can check inside of the module myself and see if theres a power or a percentage. You can probably do something like pow for getting powers.

Like, within your script you can have a button named pow but the text saying ^. Then making another elseif button.Name == "pow" then, something like this:

elseif button.Name == "pow" then

TextBox.Text ..= "pow("
end

--[[ you can try to find a way for putting the end parenthesis inside of the function. 
like if the button has been pressed once and it just says ^, it will put pow( in there. 
when it gets clicked again and it's named "end power" or a bool to express that it's active, 
then it'll change the function from adding "pow(" to ")" to finish the expression.]]

I was thinking of this example when looking through the post again, I'm making a gui calculator, how to convert string to operators? - #16 by blokav

He did “asin(1)*2” as a string, so that would mean if we just did something like "pow(10, 4)"it in like that it’d work as intended.

1 Like

yea but I insist Ill go check through that

2 Likes

Since it’s a standalone parser you can’t really use the math library functions in the inputs, so you would need to use the built-in operators as @edozune explained.

The parser requires you to use ‘pow(x, y)’ instead of ‘x ^ y’ because the ‘^’ symbol is used for the bitwise ‘xor’ operation.

In this thread someone had a similar problem with wanting to use ^ for exponents. I think I gave them a solution for it.

If you need it the original source and documentation can be found here: