Truth Tables are Helpful
Learn to use truth tables to understand logic and control flow in your programs
Size 2.2 kB - File type text/x-csharpFile contents
/*Comp 170 Standard C# Header *Assignment name: Implementing Truth Tables with Boolean Logic *LUC id and Name whonig Dr. W L Honig *Date 9Sep14 * *Get more practice with Booleans. *The main uses the facade design pattern to get all user input. It includes a sentinel loop *so that the user can give as many inputs they wish * *Questions: None * * */ using System; namespace TruthTables { class MainClass { public static void Main (string[] args) { bool a; bool b; bool result; string inputLine; string[] inputValues; const string SENTINEL_VALUE = "STOP"; Console.WriteLine ("Welcome to my truth table tester"); Console.WriteLine ("Enter two Boolean values as True or False or STOP to quit"); inputLine = Console.ReadLine(); inputLine = inputLine.Trim(); while ( inputLine != SENTINEL_VALUE ) { inputValues = inputLine.Split( ' ' ); a = bool.Parse( inputValues[0] ); b = bool.Parse ( inputValues[1] ); result = TruthTable1110( a, b); Console.WriteLine ( "Truth table applied to " + a + " and " + b + " computes " + result ); Console.WriteLine ("Enter two Boolean values as True or False or STOP to quit"); inputLine = Console.ReadLine(); inputLine = inputLine.Trim(); } Console.WriteLine ("Thank you for using the truth table program. What logic function was that?"); } /* TO DO: Your job is to add two more functions for some other truth tables * Pick any two *other than* AND OR NOT * Implement a method for each of your two to replace TruthTable1110 below (change the name) * Update the truth table with the ones your implement * * Be sure to test your program by modifying the facade main above * You can test your methods one at a time or both at once. */ public static bool TruthTable1110 (bool a, bool b) { /* This function implements a NAND gate according to this truth table * * A B Result * F F T * T F T * F T T * T T F * */ //if ( a && b ) return false; //else return true; return (!a && !b) || (a && !b) || (!a && b); } } }