ID:154004
 
I am now taking a Discrete mathmatics course and thought it would be an interesting project to make a program that generates and compares truth tables for various logical statements. I haven't decided what language to do this in yet so I'm not interested in syntax just yet, only the approach to solving the problem.

Here is an example for those of you that have no idea what I am talking about.

There are 5 basic logical operators (that I've learned anyway):

~ - Negation - it is essentially equivalent to using the '!' operator.
| - Or - it is actually supposed to be a downwards arrowhead but it doesn't matter.
& - And - supposed to be an upwards arrowhead
-> - Conditional - means "if then"
<-> - Can't recall the name but it is essentially the same as ==

Lets use the variables p and q.

p & q

p   q  (p & q)
T T T
T F F
F T F
F F F

p | q

p q (p | q)
T T T
T F T
F T T
F F F

p -> q

p q (p -> q)
T T T
T F F
F T T
F F T


That represents all the possible situations for each statement (T being it evaluates as true, F being it evaluates as false.)

The use of this is that you can compare logical statements to see if they are equivalent, thus allowing you to reduce statements to simpler forms (sometimes).

Back to the task at hand. I have several problems to overcome. The overlying problem is that I need to seperate each piece of a statement so I can evaluate it bit by bit without repetition. It seems I can take the harder route and decipher order or operations or take the easier route and force the user to use excessive parenthesis (which makes it harder to use and easier to make mistakes).

Order of operations:
()
~
&, |
->, <->

This could be a possible statement (those parenthesis are needed because the "or" and "and" are ambiguous without them):

p | (q &~r) -> r

p   q   r   ~r   (q&~r)   p|(q&~r)   p|(q&~r)->r
T T T F F T T
T T F T T T T
T F T F F T T
T F F T F T T
F T T F F F F
F T F T T T T
F F T F F F F
F F F T F F T


(p | (q & (~r))) -> r

This is what the statement would look like that the program would need. I was thinking of grabbing the entire statement, every single parenthesis, and then the variables (all the while checking for redundancy) then organize them based upon their length. That seems pretty inneficient, does anyone else have very much experience in this area, or tricks that I could use? I'm only two days into the class but I would prefer to right the program while it will have a use, instead of after the class.