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 !!

Recommended Answers

All 3 Replies

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

commented: Totally irrelevant -3

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.