Wednesday, May 13, 2009

Lab 9 Operator

Lab 9 Use the reference code as a hint. Your program will be quite different. Ask the user for 2 values. Accept string values from the user. Define two boolean variables. Set the boolean variables according to what is entered. For example, “T”, “t”, “True”, etc. should be regarded as TRUE. After accepting and converting the user input to boolean, report both the input values and the result of the logical operators (&, &&, |, ||, ^ and !) on the entered values. Put all this in a loop and accept ‘q’ for value 1 as the signal to quit. Print an error message and resume looping if an Invalid value is entered.

// Fig. 6.18: LogicalOperators.cs
// Logical operators.
using System;

public class LogicalOperators
{
public static void Main( string[] args )
{
Boolean value1=true, value2=true; // get two values from users.
string string1, string2="true"; //string from input
Boolean string1ok = true, string2ok = true;
do
{
Console.WriteLine("Please enter the first boolean value, press q to quit!");
string1=Convert.ToString(Console.ReadLine());
if (string1 == "T" || string1 == "True" || string1 == "t" || string1 == "true")
{
value1 = true;
}
else
if (string1 == "f" || string1 == "False" || string1 == "F" || string1 == "false")
{
value1 = false;
}
else string1ok = false;
if (string1 != "q" && string1 != "Q")
{
Console.WriteLine("Please enter the second boolean value");
string2 = Convert.ToString(Console.ReadLine());
}
if (string2 == "T" || string2 == "True" || string2 == "t" || string2 == "true")
{
value2 = true;
}
else
if (string2 == "f" || string2 == "False" || string2 == "F" || string2 == "false")
{
value2 = false;
}
else string2ok = false;
if ((string1ok == true) && (string2ok == true))
{
// create truth table for && (conditional AND) operator
Console.WriteLine("First String is '{0}'\n", string1);
Console.WriteLine("Second String is '{0}'\n", string2);
Console.WriteLine("{0}\n{1}\n",
"Conditional AND (&&)", (value1 && value2));


// create truth table for || (conditional OR) operator
Console.WriteLine("{0}\n{1}\n",
"Conditional or (||)", (value1 || value2));

// create truth table for & (boolean logical AND) operator
Console.WriteLine("{0}\n{1}\n",
"Boolean Logical And (&)", (value1 & value2));

// create truth table for | (boolean logical inclusive OR) operator
Console.WriteLine("{0}\n{1}\n",
"Boolean Logical inclusive or (|)", (value1 | value2));


// create truth table for ^ (boolean logical exclusive OR) operator
Console.WriteLine("{0}\n{1}\n",
"Boolean Logical exclusive or (^)", (value1 ^ value2));


// create truth table for ! (logical negation) operator
value1 = !value1;
value2 = !value2;
Console.WriteLine("{0}\n{1}\n{2}",
"Boolean Logical Negation (!)", value1, value2);
} //end if
if (string1 != "q" && string1 != "Q")
if (string1ok == false || string2ok == false)
Console.WriteLine("Please enter the valid values");

} while (string1!="q" && string1!="Q"); //while loop
Console.WriteLine("Thank you!");
} // end Main
} // end class LogicalOperators

No comments:

Post a Comment