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