9
1
Your mission, if you choose to accept it, is to construct a simple truth evaluator for the following logical operators:
----------------------------------------------------------------------------------
Logical Name | Gate Name | Symbol | Symbol Name | Truth Table
----------------------------------------------------------------------------------
Identity | is | | (none) | 10
Negation | not | ~ | tilde | 01
Conjunction | and | & | ampersand | 1000
Disjunction | or | | | pipe | 1110
Negative Conjunction | nand | ^ | caret | 0111
Joint Denial | nor | v | "vee" | 0001
Exclusive Disjunction | xor | x | "ecks" | 0110
Equivalence | equals/xnor | = | equals | 1001
Implication | implies | > | greater than | 1011
Truth tables are in the following order:
- 1 1
- 1 0
- 0 1
- 0 0
Input will come as a simple string of 0, 1, and the symbol. You can either accept input as a parameter or read it from the user on stdin. Here are some sample input/output pairs:
Input: 1
Output: 1
Input: ~1
Output: 0
Input: 0|1
Output: 1
Input: 1>0
Output: 0
The unary operator (negation) will always appear before the boolean value, while the binary operators will always appear between the two boolean values. You can assume that all input will be valid. Strings are regular ASCII strings.
If you prefer, you can use T and F rather than 1 and 0. -6 to your character count if you support both.
This is code-golf: shortest code in any language wins!
3
I believe
– FireFly – 2013-10-07T15:23:42.327^
's symbol name should say caret.3@FireFly Haha, you're right. Too close to lunch! Thanks. – asteri – 2013-10-07T15:25:20.080