Question
1. Introduction In this assignment, you will work with C conditional statements to implement a simple Boolean calculator program. This program will also introduce you
1. Introduction In this assignment, you will work with C conditional statements to implement a simple Boolean calculator program. This program will also introduce you to Boolean operators, which we will explore in much more detail later this semester. 2. Deliverables Submit your source file by uploading it directly to your Dropbox folder. Ensure your source file name is prog3_boolean.c. You should submit only the .c file. Failure to meet this specification will reduce your grade, as described in the program grading guidelines. 3. 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. 2 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 correctlyfor 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 operatorfor 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 4. Hints and Tips Using bitwise operators: The &, |, and ^ symbols arent operators just designed for this programtheyre valid, built-in operators in C. You can use them in an expression just as you can use arithmetic operators (+, -, *, /). You dont need to write conditional statements to basically implement the truth tables shown in Section 6. For example, assume your input variables are called in1 and in2, you know the user entered the & operator, and you want to assign the result of that operation to a variable called result. An inefficient way to handle this operation is through if statements: if (in1 == 0 || in2 == 0) result = 0; else if (in1 == 1 && in2 == 1) result = 1; The following simple statement produces the exact same result: result = in1 & in2; Unsigned values: Since both input values should only be 0 or 1, you can represent these values using unsigned integers (data type unsigned int or simply unsigned). Unsigned values are strictly non-negative (0 or positive). The format specifier used to read and print unsigned values is %u. The example code snippet below demonstrates the use of this format specifierit declares two unsigned integers, prompts for and reads their values, and then prints those values to the screen: unsigned x, y; printf("Enter two values: "); scanf("%u %u", &x, &y); printf("x = %u, y = %u ", x, y); Using if/else if vs. multiple if statements: Remember: in an if/else if statement, only one of the cases is executed, even if multiple conditions are true. For example, in the code below, only the first assignment statement (z = z + 3) will be executed, even though the first two conditions are both true: int x = 1; int y = 2; if (x == 1) z = z + 3; else if (y == 2) z = z 10; If you want both assignment statements that change z to be executed, use multiple if statements without the else: int x = 1; int y = 2; if (x == 1) z = z + 3; if (y == 2) z = z 10;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started