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!

It all becomes totally unclear when you come to the point "converted into this array". I understand that it could be a pseudo-code, but it's important to understand how you really represent the "array", what is the type of array elements, and so on.

"Am I wrong?" is not a valid question, unless you explain comprehensively what you are trying to achieve. Wrong for one goal could be right for another one.

—SA

I'm trying to make a program in Java that parses a string containing an arithmetic expression e.g.

"7 + 4*5 - 10/2".

I get the parts at every 'separator' ('+' and '-' char), and when parse it as a object ([+, NaN] if positive [-, NaN] if negative) to an array.

I then go through each of the objects' numbers in the array, and calculate them using some if-else statements on the operators.

What I'm trying to achieve is to solve the string equation example from earlier using this setup. But my code is really messy, so I'm generally looking for some pointers to some articles about the same thing; that is a function that accepts a string of arithmetic form and returns the result.

Did this help, or do I need to add anything else? :)

It's possible that you misunderstand the whole concept of "parsing", I'm not 100% sure. All this talking makes little sense unless you explain in what data structures you want to parse it all, and with what purpose. Parser along does not do anything, it just massages some data. It could be used for implementation of interpreter, compiler, some runtime system driven by expression text, something...

—SA