A truth table is a display of the inputs to, and the output of a Boolean function organized as a table where each row gives one combination of input values and the corresponding value of the function.



Task

 Input a Boolean function from the user as a string then calculate and print a formatted truth table for the given function.
 (One can assume that the user input is correct).
 Print and show output for Boolean functions of two and three input variables, but any program should not be limited to that many variables in the function. 
 Either reverse-polish or infix notation expressions are allowed.


Related tasks

   Boolean values
   Ternary logic


See also

   Wolfram MathWorld entry on truth tables.
   some "truth table" examples from Google.


10 DEFINT A-Z: DATA "~",4,"&",3,"|",2,"^",2,"=>",1
20 DIM V(26),E(255),S(255),C(5),C$(5)
30 FOR I=1 TO 5: READ C$(I),C(I): NEXT
40 PRINT "Boolean expression evaluator"
50 PRINT "----------------------------"
60 PRINT "Operators are: ~ (not), & (and), | (or), ^ (xor), => (implies)."
70 PRINT "Variables are A-Z. Constant False and True are 0 and 1."
100 FOR I=1 TO 26: V(I)=0: NEXT
110 PRINT: LINE INPUT "Enter an expression: ";A$
120 E$="": E=0: S=0
130 FOR I=1 TO LEN(A$)
140 I$=MID$(A$,I,1)
150 IF I$<>" " THEN E$=E$+I$
160 NEXT
170 IF E$="" THEN END ELSE Y$=E$
180 IF E$="" THEN 330
190 A$=LEFT$(E$,1): A=ASC(A$) OR 32: B$=RIGHT$(E$,LEN(E$)-1)
200 IF A>=97 AND A<=122 THEN E(E)=A-33: E=E+1: E$=B$: GOTO 180
210 IF A$="0" OR A$="1" THEN E(E)=VAL(A$)+32: E=E+1: E$=B$: GOTO 180
220 IF A$="(" THEN S(S)=97: S=S+1: E$=B$: GOTO 180
225 IF A$=")" THEN E$=B$: GOTO 300
227 I=1
230 IF LEFT$(E$,LEN(C$(I)))=C$(I) THEN 250 ELSE I=I+1: IF I<6 THEN 230
240 PRINT "Parse error at: ";E$: PRINT: GOTO 100
250 A$=C$(I): E$=RIGHT$(E$,LEN(E$)-LEN(A$))
260 IF I=1 THEN S(S)=1: S=S+1: GOTO 180
270 IF S=0 THEN 290
275 IF S(S-1)<>97 AND C(S(S-1) AND 31)>=C(I) THEN 280 ELSE 290
280 S=S-1: E(E)=S(S): E=E+1: GOTO 270
290 S(S)=I: S=S+1: GOTO 180
300 IF S=0 THEN PRINT "Error: missing (!": GOTO 100
310 IF S(S-1)<>97 THEN S=S-1: E(E)=S(S): E=E+1: GOTO 300
320 S=S-1: GOTO 180
330 IF S=0 THEN 350 ELSE S=S-1
335 IF S(S)=97 THEN PRINT "Error: missing )!": GOTO 100
340 E(E)=S(S): E=E+1: GOTO 330
350 V$=""
360 FOR I=0 TO E-1
370 IF (E(I) AND 224)<>64 THEN 390
380 A$=CHR$(E(I)+1): IF INSTR(V$,A$)=0 THEN V$=V$+A$
390 NEXT
400 GOSUB 600
410 FOR I=1 TO LEN(V$): PRINT MID$(V$,I,1);" ";: NEXT
420 PRINT "| ";Y$
430 PRINT STRING$(2+2*LEN(V$)+LEN(Y$),"-")
440 FOR J=1 TO 2^LEN(V$)
450 FOR I=1 TO LEN(V$)
460 IF V(I) THEN PRINT "T "; ELSE PRINT "F ";
470 NEXT
480 PRINT "| ";: GOSUB 600: IF S(0) THEN PRINT "T" ELSE PRINT "F"
490 I=1
500 IF V(I) THEN V(I)=0: I=I+1: GOTO 500 ELSE V(I)=1
510 NEXT
520 GOTO 100
600 S=0
610 FOR I=0 TO E-1: T=E(I) AND 224: V=E(I) AND 31
620 IF T=0 THEN ON V GOTO 700,710,720,730,740
630 IF T=32 THEN S(S)=-V: S=S+1: GOTO 650
640 IF T=64 THEN S(S)=V(INSTR(V$,CHR$(V+65))): S=S+1: GOTO 650
650 NEXT
660 IF S<>1 THEN PRINT "Missing operator": GOTO 100
670 RETURN
700 IF S<1 THEN 770 ELSE S(S-1)=1-S(S-1): GOTO 650
710 IF S<2 THEN 770 ELSE S=S-1:S(S-1)=S(S-1) AND S(S): GOTO 650
720 IF S<2 THEN 770 ELSE S=S-1:S(S-1)=S(S-1) OR S(S): GOTO 650
730 IF S<2 THEN 770 ELSE S=S-1:S(S-1)=S(S-1) XOR S(S): GOTO 650
740 IF S<2 THEN 770 ELSE S=S-1
750 IF S(S-1) THEN S(S-1)=S(S) ELSE S(S-1)=-1
760 GOTO 650
770 PRINT "Missing operand": GOTO 100