Truth table: Difference between revisions
Content added Content deleted
(Added Algol 68) |
|||
Line 19: | Line 19: | ||
* [http://www.google.co.uk/search?q=truth+table&hl=en&client=firefox-a&hs=Om7&rls=org.mozilla:en-GB:official&prmd=imvns&tbm=isch&tbo=u&source=univ&sa=X&ei=C0uuTtjuH4Wt8gOF4dmYCw&ved=0CDUQsAQ&biw=941&bih=931&sei=%20Jk-uTuKKD4Sg8QOFkPGcCw some "truth table" examples from Google]. |
* [http://www.google.co.uk/search?q=truth+table&hl=en&client=firefox-a&hs=Om7&rls=org.mozilla:en-GB:official&prmd=imvns&tbm=isch&tbo=u&source=univ&sa=X&ei=C0uuTtjuH4Wt8gOF4dmYCw&ved=0CDUQsAQ&biw=941&bih=931&sei=%20Jk-uTuKKD4Sg8QOFkPGcCw some "truth table" examples from Google]. |
||
<br><br> |
<br><br> |
||
=={{header|ALGOL 68}}== |
|||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
|||
Uses the Algol 68G specific evaluate procedure to evaluate the Boolean expressions. The expressions must therefore be infix and valid Algol 68 boolean expressions. |
|||
<lang algol68># prints the truth table of a boolean expression composed of the 26 lowercase variables a..z, # |
|||
# the boolean operators AND, OR, XOR and NOT and the literal values TRUE and FALSE # |
|||
# The evaluation is done with the Algol 68G evaluate function which is an extension # |
|||
PROC print truth table = ( STRING expr )VOID: |
|||
BEGIN |
|||
# recursively prints the truth table # |
|||
PROC print line = ( INT v )VOID: |
|||
IF v > UPB bv |
|||
THEN |
|||
# at the end of the variables - print the line # |
|||
FOR e TO UPB bv DO |
|||
IF used[ e ] THEN print( ( " ", bv[ e ], " " ) ) FI |
|||
OD; |
|||
print( ( " ", evaluate( expr ), newline ) ) |
|||
ELIF used[ v ] |
|||
THEN |
|||
# have another variable # |
|||
bv[ v ] := TRUE; |
|||
print line( v + 1 ); |
|||
bv[ v ] := FALSE; |
|||
print line( v + 1 ) |
|||
ELSE |
|||
# this variable is not used # |
|||
print line( v + 1 ) |
|||
FI # print line # ; |
|||
# returns the name of the variable number # |
|||
PROC variable name = ( INT number )CHAR: REPR ( number + ( ABS "a" - 1 ) ); |
|||
# the 26 boolean variables # |
|||
BOOL a := FALSE, b := FALSE, c := FALSE, d := FALSE, e := FALSE, f := FALSE; |
|||
BOOL g := FALSE, h := FALSE, i := FALSE, j := FALSE, k := FALSE, l := FALSE; |
|||
BOOL m := FALSE, n := FALSE, o := FALSE, p := FALSE, q := FALSE, r := FALSE; |
|||
BOOL s := FALSE, t := FALSE, u := FALSE, v := FALSE, w := FALSE, x := FALSE; |
|||
BOOL y := FALSE, z := FALSE; |
|||
# table of the variables allowng access by number # |
|||
[]REF BOOL bv = ( a, b, c, d, e, f, g, h, i, j, k, l, m |
|||
, n, o, p, q, r, s, t, u, v, w, x, y, z |
|||
); |
|||
[ 26 ]BOOL used; |
|||
BOOL at least one variable := FALSE; |
|||
# determine which variables are used in the expression # |
|||
FOR v TO UPB bv DO |
|||
used[ v ] := char in string( variable name( v ), NIL, expr ); |
|||
IF used[ v ]THEN at least one variable := TRUE FI |
|||
OD; |
|||
# print truth table headings # |
|||
print( ( expr, ":", newline ) ); |
|||
FOR v TO UPB bv DO |
|||
IF used[ v ] THEN print( ( " ", variable name( v ), " " ) ) FI |
|||
OD; |
|||
print( ( " value", newline ) ); |
|||
FOR v TO UPB bv DO |
|||
IF used[ v ] THEN print( ( " - " ) ) FI |
|||
OD; |
|||
print( ( " -----", newline ) ); |
|||
# evaluate the expression for each cobination of variables # |
|||
IF at least one variable |
|||
THEN |
|||
# the expression does not consist of literals only # |
|||
FOR v TO UPB bv DO bv[ v ] := FALSE OD; |
|||
print line( LWB bv ) |
|||
ELSE |
|||
# the expression is constant # |
|||
print( ( " ", evaluate( expr ), newline ) ) |
|||
FI |
|||
END # print truth table # ; |
|||
# print truth tables from the user's expressions # |
|||
print( ( "Please enter Boolean expressions using variables a, b, c, ..., z,", newline ) ); |
|||
print( ( "operators AND, OR, NOT and XOR and literals TRUE and FALSE", newline ) ); |
|||
print( ( "(Note operators and TRUE/FALSE must be uppercase and variables must be lower case)", newline ) ); |
|||
print( ( "Enter a blank line to quit", newline ) ); |
|||
WHILE |
|||
STRING expr; |
|||
print( ( "expression> " ) ); |
|||
read( ( expr, newline ) ); |
|||
expr /= "" |
|||
DO |
|||
print truth table( expr ) |
|||
OD</lang> |
|||
{{out}} |
|||
<pre> |
|||
Please enter Boolean expressions using variables a, b, c, ..., z, |
|||
operators AND, OR, NOT and XOR and literals TRUE and FALSE |
|||
(Note operators and TRUE/FALSE must be uppercase and variables must be lower case) |
|||
Enter a blank line to quit |
|||
expression> a OR b |
|||
a OR b: |
|||
a b value |
|||
- - ----- |
|||
T T T |
|||
T F T |
|||
F T T |
|||
F F F |
|||
expression> a AND ( b OR f ) |
|||
a AND ( b OR f ): |
|||
a b f value |
|||
- - - ----- |
|||
T T T T |
|||
T T F T |
|||
T F T T |
|||
T F F F |
|||
F T T F |
|||
F T F F |
|||
F F T F |
|||
F F F F |
|||
expression> ( NOT a ) OR ( b AND c ) |
|||
( NOT a ) OR ( b AND c ): |
|||
a b c value |
|||
- - - ----- |
|||
T T T T |
|||
T T F F |
|||
T F T F |
|||
T F F F |
|||
F T T T |
|||
F T F T |
|||
F F T T |
|||
F F F T |
|||
expression> |
|||
</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |