I've have a String

String s = " ~ ( A || B ) && C ";

A B C can take 0,1,x
they follow Boolean Arithmetic
w.r.t 'x' Boolean Arith is as follows

0 || x = x
1 || x = 1
x || x =1

x && 0 = 0
x && x = x
x && 1 = x

~ x = x

Now I've A = 1 B = x C = x ;
and I want to evaluate String "s: to get Output boolean value (i.e 1,0,x ) .

Currently I'm thinking of Shunting Yard Alogo to create the string from infix to posix notiation and
use each operator from stack to process the boolean expressions but this would be 100+ lines and overkill

So wanted to know if there is anyother way to implement this !!

Edited by pritaeas: Moved to Java.

2 Years
Discussion Span
Last Post by Taywin

I doubt that you will sensibly find a better approach than parsing into some kind of RPN then evaluating it from the resulting stack. 100+ lines of code sounds pretty efficient for the complexity of the syntax and operators you are dealing with.


this is a keyword in Java. Which can be used inside method or constructor of class. It(this) works as a reference to current object whose method or constructor is being invoked. this keyword can be used to refer any member of current object from within an instance method or a constructor.

For more click link this keyword

Votes + Comments
Totally irrelevant

You need to divide the process into 2 parts -- parsing and computing. I think the most difficult part to deal with and would take up the most code line is the parsing. In Shunting yard, it simply converts a string to posix notation. If you want to make it shorter, you do not need to convert it all the way to posix notation but rather use the tokens in your stack to do the computation right away. However, if you want to reuse your code in the future, convert it all the way is a good way to do. You could separate the class to deal with parsing only and return posix notation string. Then implement another class to do the computation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.