+ 1
Order of operations program gone crazy
I am working on a program to accept user determined number of math expressions in parentheses and then return the solution to them obeying order of ops i.e. (3+4) (2+3*3) would yield 7 and 11. I have it working at three max operations per problem but even that has gotten messy. Anyone have a suggestion how I could automate this better? Much appreciated any help! For now I just need it to accept +,-,*, and / https://code.sololearn.com/c1X81mdwkRnu
2 Antworten
+ 3
To solve this problem I would write a tokenizer first and than analyse the tokens. The tokenizer analyses the string for important keyword and symbols like '+' or '*' and stores them into a separate Array as tokens so that you can access the tokens more easily later. Than you could build a syntax tree where you take care of all the brackets and nesting. As last step you can the perform the actual calculation. That's probably the cleanest way to do it, compilers do basically the same as their first steps to analyse code. Here are two codes of mine that made use of tokenizing:
https://code.sololearn.com/ca5Ue0VjwyH8/?ref=app
https://code.sololearn.com/cRb9jV28jHDX/?ref=app
+ 2
Check this out. Maybe this could help.
https://www.sololearn.com/Discuss/2142606/?ref=app