Math in a string into an answer

Okay, Here is a simple example:
 
UserInput = “1+1”
 
print(UserInput)

Now how do I make it print out the answer of that as simply as possible? I tried print(UserInput*1) that only popped up an error. Neither can I do print(tonumber(UserInput)) since the + is not a number.

Corona doesn’t support lua equivalent of an eval function and that’s due to Apple contraints (it’s a huge opening for worms and viruses, and takes a lot of the control over updates and changes to the app)

Luckily enough there are several ways you can calculate mathematical expressions and they are not hard to implement.

We’ve developed a game called mathemagica which is based on such an algorithm, sadly I cannot share the actual code because it’s not my IP to share but I can certainly explain how to do it:

  • The human readable notation of math expressions is called “infix”. It basically means that the operator is placed between (hence “in”) two operands. In your case “+” is between the 1s.
  • a different more computer/programming friendly notation is called “postfix” or Polish Reverse Notation (RPN). As the name suggests in this notation the operator is placed after two operands, in your case “1 1 +”. You can read here a bit about the convertion between the two notations:
  • We used the Shunting Yard algorithm to convert an “infix” expression to postfix. We did not find an open source implementation in lua but you can find on google for different languages.
  • The nice thing about postfix notation is that it embedded within it the precedence of each operator so in essence you never need brackets in this notation like you do in “infix” for example “( 1 + 1 ) * 2” converts to “1 1 + 2 *”. its easy to see how starting from the left it’s very easy to build up the solution in a stack. You can find several implementation on google for what’s commonly referred to as “RPN calculators”. But it’s really quite basic. The harder part (no a biggy either) is implementing the shunting yard right… The idea here is to split the input string by spaces and then just run over the items in a loop, each number you meet you push onto a stack, each operator you meet you pop out the two last numbers, do the math and push back the result to the stack, and so on. You end up with the answer in the first element of the stack

This worked really well for us. if you check out the our game Mathemagica enter the practice mode and choose some higher level question types. We have actually managed to calculate some complicated expressions including decimals and even fractions, I think it’s quite nice (just don’t do the marathon because it takes a while till you get to actual math expressions.

And if you feel like it a rate would never hurt :slight_smile:

Hope this helped…

Corona doesn’t support lua equivalent of an eval function and that’s due to Apple contraints (it’s a huge opening for worms and viruses, and takes a lot of the control over updates and changes to the app)

Luckily enough there are several ways you can calculate mathematical expressions and they are not hard to implement.

We’ve developed a game called mathemagica which is based on such an algorithm, sadly I cannot share the actual code because it’s not my IP to share but I can certainly explain how to do it:

  • The human readable notation of math expressions is called “infix”. It basically means that the operator is placed between (hence “in”) two operands. In your case “+” is between the 1s.
  • a different more computer/programming friendly notation is called “postfix” or Polish Reverse Notation (RPN). As the name suggests in this notation the operator is placed after two operands, in your case “1 1 +”. You can read here a bit about the convertion between the two notations:
  • We used the Shunting Yard algorithm to convert an “infix” expression to postfix. We did not find an open source implementation in lua but you can find on google for different languages.
  • The nice thing about postfix notation is that it embedded within it the precedence of each operator so in essence you never need brackets in this notation like you do in “infix” for example “( 1 + 1 ) * 2” converts to “1 1 + 2 *”. its easy to see how starting from the left it’s very easy to build up the solution in a stack. You can find several implementation on google for what’s commonly referred to as “RPN calculators”. But it’s really quite basic. The harder part (no a biggy either) is implementing the shunting yard right… The idea here is to split the input string by spaces and then just run over the items in a loop, each number you meet you push onto a stack, each operator you meet you pop out the two last numbers, do the math and push back the result to the stack, and so on. You end up with the answer in the first element of the stack

This worked really well for us. if you check out the our game Mathemagica enter the practice mode and choose some higher level question types. We have actually managed to calculate some complicated expressions including decimals and even fractions, I think it’s quite nice (just don’t do the marathon because it takes a while till you get to actual math expressions.

And if you feel like it a rate would never hurt :slight_smile:

Hope this helped…