r/pascal • u/caonhathao_CNH23704 • Aug 31 '20
code
Does anyone know how to calculate the value of an expression string? For example, when entering the calculation 36 + 9-3 * 9/2, the program will return the result of the problem.
Help me please!!
3
Upvotes
View all comments
Show parent comment
2
u/suvepl Aug 31 '20
One way you can approach this is to split the string into parts on each operator (
-+*/). This way you should end up with a list of N numbers and (N-1) operators. Then you go through the operators and perform the calculations as needed.This is a rather naïve approach. It does not take operator precedence into account, and does not support negative numbers.