Write a code to execute the following specifications: . Specifications Input: Yo
ID: 3890144 • Letter: W
Question
Write a code to execute the following specifications:
. Specifications Input: Your program should prompt the user to enter a simple Boolean expression of the form a op b, where a and b are 1-bit operands and op is one of the following operators: & (AND), | (OR), ^ (XOR). See Section 6: Boolean Operations for more details on these operators and their desired behavior. Examples include: • 1 & 1 • 0 | 0 • 1 ^ 0 Output: Given a valid expression, your program should calculate the result and reprint the entire expression as well as its result. For example, the expressions listed above will produce the following output: • 1 & 1 = 1 • 0 | 0 = 0 • 1 ^ 0 = 1 NOTE: Your program should not print the result of an expression if any error occurs. See the next page of the assignment for details on possible input errors. See Section 5: Test Cases for more sample program runs.Error checking: Your program should print a descriptive error message under any of the conditions below. For examples of valid error messages, see Section 5: Test Cases: 1. Any of the inputs are incorrectly formatted and therefore cannot be read correctly using scanf() • Your error message should include the number of input values that were entered correctly—for example: Error: 2 values entered correctly • scanf() returns the number of values correctly read, which you can store in a variable to test later. Say you have the following line of code: nVals = scanf("%d %d %d", &v1, &v2, &v3); If the user enters: • 1 2 3 à nVals == 3 • 1 2 a à nVals == 2 ('a' is not part of a valid int) • 1.2 2 3 à nVals == 1 ('.' is not part of a valid int, but 1 is read correctly) • X1 2 3 à nVals == 0 ('X' is not part of a valid int) 2. Either (or both) of the operands cannot be represented as a single bit (0 and 1 are the only valid 1-bit values) • Your error message should print the invalid input(s)—for example: Error: first input (3) requires > 1 bit 3. The operator entered is not a valid operator • Your error message should print the invalid operator—for example: Error: invalid operator X NOTE: If the inputs are correctly formatted (i.e., scanf() can read all input values), then your program may generate multiple error messages! For example, if the user enters the expression 3 X 4, then your program should print: Error: first input (3) requires > 1 bit Error: second input (4) requires > 1 bit Error: invalid operator X If scanf() cannot read all input values, your program should only print the error message related to a formatting error. For example, if the user enters the expression: X 3 4, then your program should print: Error: 0 values entered correctly
Explanation / Answer
#include<stdio.h>
int main()
{
//variable to store return value from scanf
int nVals;
bool var1, var2,result,expr=true;
char op;
nVals = scanf("%d %c %d", &var1, &op, &var2);
switch (nVals)
{
case 1:
printf("Only 1 value read properly ");
break;
case 2:
printf("Only 2 values read properly ");
break;
case 3:
if (var1 > 1)
{
printf("var1 value cannot be more than 1 as it is one bit ");
expr = false;
}
if (var2 > 1)
{
printf("var2 value cannot be more than 1 as it is one bit ");
expr = false;
}
if ( !(op == '&' || op == '|' || op == '^') )
{
printf("operation other than & or | or ^,pls enter any one of these operations: &, | , ^ ");
expr = false;
}
break;
}
//if expression is correct , we have expr true, then it evaluates the boolean expression and print result
if (expr)
//print result
switch (op)
{
case '&':
result = var1 & var2;
printf("%d %c %d = %d ", var1, op, var2, result);
break;
case '|':
result = var1 | var2;
printf("%d %c %d = %d ", var1, op, var2, result);
break;
case '^':
result = var1 ^ var2;
printf("%d %c %d = %d ", var1, op, var2, result);
break;
default:
printf("This operation not implemented ");
break;
}
else
printf("Boolean expression is not correct ");
}
------------------------------------------------
//output 1 for invalid input
3 & 1
var1 value cannot be more than 1 as it is one bit
Boolean expression is not correct
//output2 for invalid input
1 & 3
var2 value cannot be more than 1 as it is one bit
Boolean expression is not correct
//output3 for invalid operation
1 * 1
operation other than & or | or ^,pls enter any one of these operations: &, | , ^
Boolean expression is not correct
//output4 , all invalid inputs
2 * 3
var1 value cannot be more than 1 as it is one bit
var2 value cannot be more than 1 as it is one bit
operation other than & or | or ^,pls enter any one of these operations: &, | , ^
Boolean expression is not correct
//correct input for & operation
1 & 1
1 & 1 = 1
//correct output for |
1 | 0
1 | 0 = 1
//correct output for ^
1 ^ 0
1 ^ 0 = 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.