+ 4
Evaluating a math expression in Java.
I wanted to see how a programmer can evaluate a math expression in Java, such as this: input: 10^6 8+5+6 152.53-6*(5) output: 1000000 19 122.53 However, I wanted to avoid too many if or switch statements, and avoid putting spaces between the values and the operands. How can I do such?
6 Antworten
+ 3
@Timon Paßlick
Because I made a code in which was basic calculator, yet I got complaints and downvotes based on if and switch statements, plus putting spaces between the operands and the numbers. Especially considering the fact that you could only evaluate 2 numbers at a time, I felt pretty badly about the code.
https://code.sololearn.com/c6onX1jZFliG/#java
If and/or switch statements are a given, but I don't want the whole program to just be switch after switch after switch after switch, proving to be a useless calculator.
Although I don't think there's much wrong with it, I would like to learn how to do more advanced coding, and learning how to evaluate a math expression is part of it.
+ 2
Why do you have fear of if and switch?
+ 2
Sheldon Duncan Advanced coding is also with if and switch, no idea why they downvoted.
+ 1
Operator precedence is important.
4 + 5 * 10 = 54, and not 90
0
I think the original poster is talking about parsing a mathematical expression, and then evaluating it. You normally use a tree for for this.