Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am working on a C++ project that I need help for it.
In this project the program should get a phrase, for example:
7+2*3+4/2
and calculate it. The hard part is that it should obey precedence rules.
And If it can support power and parentheses; I will be better but it is nit nessecary.
Could anyone give me an idea or a hint?
Regards,
Posted

If you want to support precedence, you need to take the entire string and parse it, you can't do it as you go, as you won't know the precedence. So, what you want to write is a string parsing engine that works out the order of precedence as it goes, searching for * and / before applying + and -.
 
Share this answer
 
You may write your own expression parser (see, for instance [^] or you may use a tool, such flex [^]+bison[^] or ANTLR [^], for this task.
You may even embed a script language (like, for instance Lua [^]) in your application and make it evaluate the expression for you.
 
Share this answer
 
v2
you have to write an expression parser for it, implementing stack operation.
 
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