Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I have an issue, i have a string, e.g: `"!!!T&(!F)"` and i want those operations to be executed and the output should be in a variable which i will show in console.

Any idea how could i do that??
T = true ( a constant defined by me ) 
F = false ( same as T )
R = result ( the variable where i want the output)


What I have tried:

Well, i'm stuck in this point, i do not know how i should approach it. I'm waiting for suggestions, ways of solving it..
Posted
Updated 13-May-17 11:20am

1 solution

What you are trying to do is evaluate a logical expression.  In order to do this, you should first define a simple grammar for the family of expressions you want to be able to parse (your example is just one such expression), then build a parse tree from the input string, or at least convert the infix expression you have into RPN (Reverse Polish Notation).  Once you've done that, you can use a simple stack machine to execute the RPN form of the expression.

If this doesn't make any sense to you, you've bitten off more than you can chew (for now) and I suggest reading up on Dijkstra's Shunting-yard algorithm[^].  It's not that hard once you understand how it works.

/ravi
 
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