Hello there! (Sorry if I have some grammatical errors, english is not my native language, but I try my best!)
I'm currently trying to write a program that compiles a string containing an arithmetic expression and solves / evaluates it. I'm not looking for code or libraries, I just want to get some more knowledge, so that I can understand the things that I'm trying to do.
I've read about infix, prefix and postfix, but I don't know how this would programmatically. How I'm going about it now is that i have a string e.g.
"7 + 4*5 - 10/2"
, at every add or subtract operation I break it up into smaller parts. In those parts I store if it either positive or negative, I also store the number, or equation if it contains stronger operators (multiplication, division and modulo). So the previous string would be converted into this array:
[+, 7] [+, 4*5] [-, 10/2]
I do this since I can scramble the sequence of parts without getting into trouble later:
[+, 7] [+, 4*5] [-, 10/2] == [+, 4*5] [+, 7] [-, 10/2]
Now I calculate each of the parts equation, so that I get:
[+, 7] [+, 20] [-, 5]
Now I can take the sum of all the parts.
[+, 7] add [+, 20] = 27; [+, 27] add [-, 5] = 22;
If I had a parenthese in the expression, I could just use the same function on the string inside them. Then paste the result of the parentheses instead.
I could paste the code, but I see no point since I've just explained basically the whole program.
- Now here is my question: Am I going about this wrong, have I forgotten something that could simplify and increase efficiency?
I really look forward for your answers!