Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted
Comments
Sergey Alexandrovich Kryukov 18-Nov-15 16:59pm    
You did not start your explanation from the very beginning. Are you parsing strings with some code? What is the expression language? What are you parsing it into and why?
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
Oliver S. Neven 18-Nov-15 17:17pm    
Sorry for being unclear.
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? :)
Sergey Alexandrovich Kryukov 18-Nov-15 17:38pm    
Fundamentally wrong question. You don't "need" anything, you need only what leads you to your own goals.

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
PIEBALDconsult 18-Nov-15 18:50pm    
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
Patrice T 18-Nov-15 19:00pm    
Edsger W. Dijkstra: a real great guy

There is no solution/answer to your question.
Quote:
I could paste the code, but I see no point since I've just explained basically the whole program.
This is where you are wrong. You gave us a vague general speak about expression compilation that can apply to almost any program or technique used to do it.
Quote:
- Now here is my question: Am I going about this wrong, have I forgotten something that could simplify and increase efficiency?
When it comes to efficiency and simplicity, the only thing of interest is your exact method or program (that you didn't provide).
Quote:
I've read about infix, prefix and postfix, but I don't know how this would programmatically.
Since understanding infix, prefix and postfix is a prerequisite to programming an expression compiler, there is even doubt about what you can have really done.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900