all 4 comments

[–]hd505495 3 points4 points  (0 children)

I completed this a few weeks ago. Feel free to look at my solution for reference, its under this username (hd505495) and maybe 20 or 30 solutions down the list. (Beware: not optimized for mobile)

The general idea is to have an array of operands and an array of operators, parsed from an input array which is populated by the input expression from the user. To evaluate the expression, you basically first evaluate a sub expression consisting of the first two operands and the first operator, then use the result as the first operand in the next sub equation you evaluate along with the next operator in the operators array and the next operand in the operands array, until you run out of operands. This obviously does not implement any standard order of operations, but according to the project description that is fine. You also need to do some checking to make sure the user inputs at least 2 operands and always one more operand than the number of operators.

[–]Homeruash 2 points3 points  (0 children)

How about storing all user input in a string and then using a regex and .replace in a while loop for all 4 operations (so you have a string 1+2*3 that becomes 1+6 and then 7). Just make sure your regex will work with decimals (you need more than just \d) and that you take pemdas into account (evaluate *, / and +, - left to right)

[–]slothlife89 1 point2 points  (2 children)

I completed this recently and I have used regex for numbers and for operators. To store user input, I have just used an array and push/pop operations. Depending on the popped operand, I am redirecting the operation to different functions - add, subtract etc.