G
G
goshan_p2015-04-01 21:04:10
Java
goshan_p, 2015-04-01 21:04:10

How to implement Boolean function evaluation in Java?

You need to write a so-called logical calculator. Input: A string like " A&(B|C<=>A) " Can contain all possible operators From Negation to Pierce's Arrow and Schaeffer's Stroke. Output: Truth table. I'm not familiar with complex structures like Trees yet. Suggest an algorithm for calculating such expressions. There are several options for splitting a string and calculating in my head, but they all seem to be suboptimal. Tell me how you would do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2015-04-01
@goshan_p

Polish notation , syntax tree and parsing will help you .
I would do so.
I go through the input string from left to right, breaking it into lexemes (tokens, symbols). The splitting rule is as follows: if the character under the caret is a letter, it is an operand, otherwise we try to recognize the operator.
Next, having received a list of tokens, I would go through it in order to build a tree of operations (Polish notation, don't forget!). This step will give me a list of operations and operands. The tree will always be binary, even if there is a unary operation, it is still only one node of the tree.
Next, I would make a table of operand values ​​and calculate the value of the resulting tree of operations from it.
This repository can help you:
https://github.com/FrankStain/c-script
And more often this code:
https://github.com/FrankStain/c-script/blob/master...
This is Object Pascal, but don't shy away from it. Read, smoke.
Feel free to comment and I'll update my answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question