r/dailyprogrammer • • Oct 20 '12
[10/20/2012] Challenge #105 [Intermediate] (Boolean logic calculator)
Boolean logic is something all programmers have to deal with, whether we like it or not. Why not automate the task to make it easier?
Your objective, if you choose to accept it, is to make a boolean logic calculator that can parse boolean logic statements. Given:
| = or
* = and
^ = xor
! = not
Take input of 1s and 0s (or T and F) and output the evaluation of that statement. Try not to use statement evaluators built into your language of choice, like eval. Your parser should be able to evaluate statements in parentheses as well
15
Upvotes
5
u/nagasgura 0 0 Oct 21 '12
Could you please give an example input and output?
3
u/Arknave 0 0 Oct 20 '12 edited Oct 20 '12
Python solution. Had to look up Dijkstra's shunting algorithm. This was a fun one. I'm pretty sure the method I took was overkill, but was easier to debug. What's the proper way of doing this?
5
u/Cosmologicon 2 3 Oct 20 '12
Do we need to worry about order of operations? In C it goes NOT AND OR XOR, so:
should be parsed as:
2
u/Godde Oct 21 '12
Arknave's solution puts mine to shame, I'm sort of new to this and I gave it my best. The mentioning of Dijkstra's algorithm was a lifesaver!
Java:
1
u/CptBread 0 0 Oct 21 '12
Here is my C++ solution. Complete with non-descriptive error messages...
1
u/[deleted] Oct 23 '12
C solution. I made a simple stack implementation first and used Dijkstra's Shunting algoritm
1
u/thePersonCSC 0 0 Oct 23 '12 edited Oct 23 '12
Java:
1
u/iMalevolence Oct 24 '12 edited Oct 24 '12
Java
I tried writing it in a way so I wasn't using any actual boolean operators like && or ||. I know && binds more tightly than ||, but I wasn't sure about XOR so I put it at the end.
Tested:
Returns:
1
u/altorelievo Oct 26 '12 edited Oct 26 '12
No parenthesis but, uses order of operation. If this isn't what was intended for the output give me a heads up :0 Python:
1
u/Die-Nacht 0 0 Nov 02 '12
Python. I'm know I am late but this one was hard (and was busy). Also using Shunting-yard but I made my own evaluator instead of using the built-int "and, or, etc" by adding 1's and 0's and determining that their final value was correct: