I have a project to make a logic calculator like the one described below, but it literally doesn't make "logic" sense to me. I'm sure I won't have a problem programming it once I know what the hell it is asking. Any help? THANKS!
The program in the text (see below) prompts the user for two numeric operands and an operator ( “+”, “-“, etc.) then calculates and prints the result of applying the selected operator to the two operands. Your task is to use the same sort of design to do the following:
· Prompt the user for two character variables and a logical operator.
· For example “Please enter a logical expression: T O F”. This should print “True” where “O” stands for OR (right?)
· Do this for the following logical operators - OR, AND, NOT. To make the reading with scanf simpler, use O for OR, A for AND and N for NOT. (NOT is a little weird in that it is a unary, operator, not a binary one, so you can ignore the second operand and just figure the negation (NOT) on the first operand alone).
· Print the result as “True” or “False”.
How to Complete this Project
First, get the example simple expression evaluation program on pages 85 and 86 of PIC to compile and run in your environment.
Add logic to convert the letters you read “T” and “F” to 1 and 0 respectively.
Alter the cases in the switch statement to look for the letters you are using for the logical operators (O, A, and N) instead of “+” or “-“.
Add logic to each case to evaluate the two operands using the selected logical operator. You can either use C’s logical operators ( ||, &&, !) in a logical expression with the two operands, or you can write if statements to evaluate the expression if you like.
Turn in the program and output for all possible combinations of input and operators, e.g.
T O T = True
F O F = False
etc.,
Hints
Watch out scanf troubles. Remember that the variables into which you are reading input must be preceded by the address of operator (&op1, not just op1). Also, the buffering of keyboard input by the OS sometimes causes extra characters to “hang around” for the next iteration of your loop. If you get weird results while trying to read the input, try adding a call to flush ( ) into your loop just before the scanf.
Code:
#include <stdio.h>
int main (void){
float value1, value2;
char operator;
printf ("Type in your expression. \n");
scanf ("%f %c %f", &value1, &operator, &value2);
switch(operator){
case'+':
printf("%.2f\n", value1 +value2);
break;
case '-':
printf("%.2f\n", value1-value2);
break;
case '*':
printf("%.2f\n", value1*value2);
break;
case '/':
if (value2==0)
printf ("Division by zero.n\");
else
printf("%.2f\n", value1/value2);
break;
default:
printf("Unknown operator.\n");
break;
}
return 0;
}