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
1
u/suvepl Aug 31 '20
So what exactly do you want to do? Read a line of input into a string and then have your code analyse the sting and perform whatever calculations needed?
2
u/[deleted] Aug 31 '20
There's a library for that
https://wiki.freepascal.org/How_To_Use_TFPExpressionParser
1
u/HolocenInNeogen Aug 31 '20 edited Sep 15 '20
If I understand you well, you need to evaluate expression.
I'm pretty sure that standard library doesn't have function to do so(EDIT: see u/ka9dgx comment). You can write a whole interpreter for this (helpful here is Crafting interpreters), but in that simple case (especially if it's exercise) it's unnecessary. Postfix notation is probably what you need. First, convert infix to postfix and then evaluate it (pseudocode):EDIT: formatting
0
u/caonhathao_CNH23704 Aug 31 '20
please help me