Truth table: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Java}}: A little more info)
Line 128: Line 128:


=={{header|Ruby}}==
=={{header|Ruby}}==
Uses <code>eval</code>, so blindly trusts the user's input:
<lang ruby>loop do
<lang ruby>loop do
print "\ninput a boolean expression (e.g. 'a & b'): "
print "\ninput a boolean expression (e.g. 'a & b'): "

Revision as of 15:31, 31 October 2011

Truth table is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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

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

J

Implementation:

<lang j>truthTable=:3 :0

 assert. -. 1 e. 'data expr names table' e.&;: y
 names=. ~. (#~ _1 <: nc) ;:expr=. y
 data=. #:i.2^#names
 (names)=. |:data
 (' ',;:inv names,<expr),(1+#@>names,<expr)":data,.".expr

)</lang>

The argument is expected to be a valid boolean J sentence which, among other things, does not use any of the words used within the implementation (but any single-character name is valid).

Example use:

<lang j> truthTable '-.b'

b -.b
0   1
1   0
  truthTable 'a*b'
a b a*b
0 0   0
0 1   0
1 0   0
1 1   1
  truthTable 'a+.b'
a b a+.b
0 0    0
0 1    1
1 0    1
1 1    1
  truthTable '(a*bc)+.d'
a bc d (a*bc)+.d
0  0 0         0
0  0 1         1
0  1 0         0
0  1 1         1
1  0 0         0
1  0 1         1
1  1 0         1
1  1 1         1</lang>

Java

Works with: Java version 1.5+

This example would require a system of pages that would be moderately complicated to set up and follow (or a really huge page that would also be hard to follow) since there is no eval in Java, so you can find information about it here. There is a link to an executable jar file with the required source files there. The program shows the expression and the truth table in a window. The expression must use prefix notation, single characters for input names (numerals, lowercase letters, and uppercase letters are the easiest to read), and the outputs can be shown as 1/0 or T/F. There is also a "Check" button which will make sure that the operators have enough operands. The window looks something like this:

Python

This accepts correctly formatted Python boolean expressions. <lang python>from itertools import product

while True:

   bexp = input('\nBoolean expression: ')
   bexp = bexp.strip()
   if not bexp:
       print("\nThank you")
       break
   code = compile(bexp, '<string>', 'eval')
   names = code.co_names
   print('\n' + ' '.join(names), ':', bexp)
   for values in product(range(2), repeat=len(names)):
       env = dict(zip(names, values))
       print(' '.join(str(v) for v in values), ':', eval(code, env))

</lang>

Sample output
Boolean expression: A ^ B

A B : A ^ B
0 0 : 0
0 1 : 1
1 0 : 1
1 1 : 0

Boolean expression: S | ( T ^ U )

S T U : S | ( T ^ U )
0 0 0 : 0
0 0 1 : 1
0 1 0 : 1
0 1 1 : 0
1 0 0 : 1
1 0 1 : 1
1 1 0 : 1
1 1 1 : 1

Boolean expression: A ^ (B ^ (C ^ D))

A B C D : A ^ (B ^ (C ^ D))
0 0 0 0 : 0
0 0 0 1 : 1
0 0 1 0 : 1
0 0 1 1 : 0
0 1 0 0 : 1
0 1 0 1 : 0
0 1 1 0 : 0
0 1 1 1 : 1
1 0 0 0 : 1
1 0 0 1 : 0
1 0 1 0 : 0
1 0 1 1 : 1
1 1 0 0 : 0
1 1 0 1 : 1
1 1 1 0 : 1
1 1 1 1 : 0

Boolean expression: 

Thank you

Ruby

Uses eval, so blindly trusts the user's input: <lang ruby>loop do

 print "\ninput a boolean expression (e.g. 'a & b'): "
 expr = gets.strip.downcase
 break if expr.empty?
 vars = expr.scan(/\p{Alpha}+/)
 if vars.empty?
   puts "no variables detected in your boolean expression"
   next
 end
 vars.each {|v| print "#{v}\t"}
 puts "| #{expr}"
 prefix = []
 suffix = []
 vars.each do |v|
   prefix << "[false, true].each do |#{v}|"
   suffix << "end"
 end
 body = vars.inject("puts ") {|str, v| str + "#{v}.to_s + '\t' + "} 
 body += '"| " + eval(expr).to_s'
 eval (prefix + [body] + suffix).join("\n")

end</lang>

Example

input a boolean expression (e.g. 'a & b'): !a
a       | !a
false   | true
true    | false

input a boolean expression (e.g. 'a & b'): a|!b
a       b       | a|!b
false   false   | true
false   true    | false
true    false   | true
true    true    | true

input a boolean expression (e.g. 'a & b'): ((a^b)^c)^d
a       b       c       d       | ((a^b)^c)^d
false   false   false   false   | false
false   false   false   true    | true
false   false   true    false   | true
false   false   true    true    | false
false   true    false   false   | true
false   true    false   true    | false
false   true    true    false   | false
false   true    true    true    | true
true    false   false   false   | true
true    false   false   true    | false
true    false   true    false   | false
true    false   true    true    | true
true    true    false   false   | false
true    true    false   true    | true
true    true    true    false   | true
true    true    true    true    | false