Difference between revisions of "CSC270 GenerateTruthTable.py"
(→Python 3.X Version) |
|||
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | < | + | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 11:48, 15 January 2016 (EST) |
+ | ---- | ||
+ | <br /> | ||
+ | One can use python (or Java, or any other language) to easily generate truth tables. Programming provides a simple way to test a hypothesis, or to verify special cases in design situations. Don't hesitate to use this approach to save time and generate accurate results. | ||
+ | <br /> | ||
+ | =Python 3.X Version= | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
− | # truthtable.py | + | # truthtable.py |
# D. Thiebaut | # D. Thiebaut | ||
# how a simple python program can generate the | # how a simple python program can generate the | ||
# truth table of a boolean function | # truth table of a boolean function | ||
# | # | ||
− | # | + | # 3 functions of 3 variables are shown here: |
# _ | # _ | ||
# f = a.b + c | # f = a.b + c | ||
# _ _ _ | # _ _ _ | ||
# g = a + b + c | # g = a + b + c | ||
+ | # _ | ||
+ | # h = a + b | ||
+ | |||
def f( a, b, c ): | def f( a, b, c ): | ||
− | return ( a and not b ) or c | + | return ( a and (not b) ) or c |
def g( a, b, c ): | def g( a, b, c ): | ||
− | return not a or not b or not c | + | return (not a) or (not b) or (not c) |
+ | def h( a, b, c ): | ||
+ | return a or not b | ||
def main(): | def main(): | ||
− | print " a b c | f g " | + | print( " a b c | f g h" ) |
− | print "-----------+--------" | + | print( "-----------+---------" ) |
for a in [ 0, 1 ]: | for a in [ 0, 1 ]: | ||
for b in [ 0, 1 ]: | for b in [ 0, 1 ]: | ||
for c in [ 0, 1 ]: | for c in [ 0, 1 ]: | ||
− | print "%3d%3d%3d |%3d%3d" % | + | print( "%3d%3d%3d |%3d%3d%3d" % |
− | ( a, b, c, f( a, b, c ), g( a, b, c ) ) | + | ( a, b, c, |
+ | f( a, b, c ), | ||
+ | g( a, b, c ), | ||
+ | h( a, b, c ) ) ) | ||
− | |||
main() | main() | ||
− | |||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
The output is show below: | The output is show below: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | |||
+ | a b c | f g h | ||
+ | -----------+--------- | ||
+ | 0 0 0 | 0 1 1 | ||
+ | 0 0 1 | 1 1 1 | ||
+ | 0 1 0 | 0 1 0 | ||
+ | 0 1 1 | 1 1 0 | ||
+ | 1 0 0 | 1 1 1 | ||
+ | 1 0 1 | 1 1 1 | ||
+ | 1 1 0 | 0 1 1 | ||
+ | 1 1 1 | 1 0 1 | ||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | =Java Version= | ||
+ | <br /> | ||
+ | ::<source lang="java"> | ||
+ | class TruthTable { | ||
+ | static boolean f( boolean a, boolean b, boolean c ) { | ||
+ | return ( a & (! b) ) | c; | ||
+ | } | ||
+ | static boolean g( boolean a, boolean b, boolean c ) { | ||
+ | return !a | !b | !c; | ||
+ | } | ||
+ | |||
+ | static boolean h( boolean a, boolean b, boolean c ) { | ||
+ | return a | ! b; | ||
+ | } | ||
+ | |||
+ | static String s( boolean a ) { | ||
+ | // return ((a==true)? "T": "F" ); // use this if you want T and F output | ||
+ | return ((a==true)? "1": "0" ); | ||
+ | } | ||
− | + | public static void main( String[] args ) { | |
− | + | System.out.println( " a b c | f g h" ); | |
− | -----------+-------- | + | System.out.println( "-----------+---------" ); |
− | + | for (int a =0; a <= 1; a++ ) | |
− | + | for (int b=0; b <= 1; b++ ) | |
− | + | for (int c=0; c<=1; c++ ) { | |
− | + | boolean aa = (a==1)? true: false; | |
− | + | boolean bb = (b==1)? true: false; | |
− | + | boolean cc = (c==1)? true: false; | |
− | + | System.out.println( | |
− | + | String.format( "%3s%3s%3s |%3s%3s%3s", | |
+ | s(aa), s(bb), s(cc), | ||
+ | s(f( aa, bb, cc )), | ||
+ | s(g( aa, bb, cc )), | ||
+ | s(h( aa, bb, cc )) ) ); | ||
+ | } | ||
+ | } | ||
− | </ | + | } |
+ | </source> | ||
+ | <br /> | ||
+ | Output | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | a b c | f g h | ||
+ | -----------+--------- | ||
+ | 0 0 0 | 0 1 1 | ||
+ | 0 0 1 | 1 1 1 | ||
+ | 0 1 0 | 0 1 0 | ||
+ | 0 1 1 | 1 1 0 | ||
+ | 1 0 0 | 1 1 1 | ||
+ | 1 0 1 | 1 1 1 | ||
+ | 1 1 0 | 0 1 1 | ||
+ | 1 1 1 | 1 0 1 | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC270]][[Category:Python]][[Category:Java]] |
Latest revision as of 12:48, 15 January 2016
--D. Thiebaut (talk) 11:48, 15 January 2016 (EST)
One can use python (or Java, or any other language) to easily generate truth tables. Programming provides a simple way to test a hypothesis, or to verify special cases in design situations. Don't hesitate to use this approach to save time and generate accurate results.
Python 3.X Version
# truthtable.py # D. Thiebaut # how a simple python program can generate the # truth table of a boolean function # # 3 functions of 3 variables are shown here: # _ # f = a.b + c # _ _ _ # g = a + b + c # _ # h = a + b def f( a, b, c ): return ( a and (not b) ) or c def g( a, b, c ): return (not a) or (not b) or (not c) def h( a, b, c ): return a or not b def main(): print( " a b c | f g h" ) print( "-----------+---------" ) for a in [ 0, 1 ]: for b in [ 0, 1 ]: for c in [ 0, 1 ]: print( "%3d%3d%3d |%3d%3d%3d" % ( a, b, c, f( a, b, c ), g( a, b, c ), h( a, b, c ) ) ) main()
The output is show below:
a b c | f g h -----------+--------- 0 0 0 | 0 1 1 0 0 1 | 1 1 1 0 1 0 | 0 1 0 0 1 1 | 1 1 0 1 0 0 | 1 1 1 1 0 1 | 1 1 1 1 1 0 | 0 1 1 1 1 1 | 1 0 1
Java Version
class TruthTable { static boolean f( boolean a, boolean b, boolean c ) { return ( a & (! b) ) | c; } static boolean g( boolean a, boolean b, boolean c ) { return !a | !b | !c; } static boolean h( boolean a, boolean b, boolean c ) { return a | ! b; } static String s( boolean a ) { // return ((a==true)? "T": "F" ); // use this if you want T and F output return ((a==true)? "1": "0" ); } public static void main( String[] args ) { System.out.println( " a b c | f g h" ); System.out.println( "-----------+---------" ); for (int a =0; a <= 1; a++ ) for (int b=0; b <= 1; b++ ) for (int c=0; c<=1; c++ ) { boolean aa = (a==1)? true: false; boolean bb = (b==1)? true: false; boolean cc = (c==1)? true: false; System.out.println( String.format( "%3s%3s%3s |%3s%3s%3s", s(aa), s(bb), s(cc), s(f( aa, bb, cc )), s(g( aa, bb, cc )), s(h( aa, bb, cc )) ) ); } } }
Output
a b c | f g h -----------+--------- 0 0 0 | 0 1 1 0 0 1 | 1 1 1 0 1 0 | 0 1 0 0 1 1 | 1 1 0 1 0 0 | 1 1 1 1 0 1 | 1 1 1 1 1 0 | 0 1 1 1 1 1 | 1 0 1