Trying to get started on an idea for a CS course

Hello, I'm trying to make a program that generates truth tables to assist me in my CS course. I know I could easily find one online but I want to make one myself. For anyone who doesn't know, truth tables are generated from propositional statements like this:

(P and Q) or (not P and R).
- The user will enter something like this when the program starts.

My program should be able to generate all possible combinations of True and False values for P, Q, and R. Then, it will evaluate the entire expression and give a True or False value. I want the program to be able to recognize the precedence of operators and parentheses. Afterwards, the program will display a table with all combinations and the final truth value for each. The biggest challenge for me will be parsing the text and getting the program to decipher exactly what the user meant it to be.

I have a general idea how to do it. Perhaps I should use vectors to hold all the combinations of truth values? What is the best method for parsing the text? If you have any more general ideas of how the program should work I would appreciate that very much.
Last edited on
Do you know postfix notation? You may want to use it to evaluate the expression. http://en.wikipedia.org/wiki/Reverse_Polish_notation

A way from converting from infix to postfix http://en.wikipedia.org/wiki/Shunting-yard_algorithm
A recursive descent parser for this would be much simpler, and would only require two or three functions...

A std::bitset might help you also when iterating through values for your inputs...
Well, depends on what you mean by precedence. (A stupid question, I know.)
If you want the expression
P or Q and R
to be automatically transformed into, say
(P or (Q and R))
then recursive descent may not be that easy. In such case, you should transform to postfix notation. If you only need to be able to handle parenthesis, with all connectives at equal precedence, then using recursive descent will be much simpler.

Regards
Last edited on
... what you mean by precedence ... then recursive descent may not be that easy

LOL, what do you mean by that?

Recursive descent is specifically useful for handling precedence properly.
Duoas wrote:
LOL, what do you mean by that?

Recursive descent is specifically useful for handling precedence properly.

To be honest my understanding of recursive descent was obviously very shallow. It seems that you are right.

My sincere apologies to the poster.
Topic archived. No new replies allowed.