1. ## logic calculator

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;
}```

2. Convert T to 1 and F to 0. Then use the logical operators in C. For example:

T O T => 1 || 1

3. So then there is no right or wrong answer to some of them? Like 1||0 can be 1 or 0?? And what about 1 AND 0???

4. Code:
```if (1 && 0)
puts("True");
else
puts("False");```
Code:
```if (1 || 0)
puts("True");
else
puts("False");```
Code:
```if (!0)
puts("True");
else
puts("False");```
If you're not already familiar with them I suggest you google for "logical operators c".

5. Why is this program printing smiley faces instead of a 1 or 0???

Code:
```#include <stdio.h>
#include <stdlib.h>

int main (void){
char value1;
char value2;
char loperator;

//char 1 = true;
//char 0 = false;

printf ("Please enter a logical expression:\n T=true; F=false; O=or; A=and; N=not\n");
scanf ("%c %c %c", &value1, &loperator, &value2);

char T = 1;
char F = 0;
switch(loperator){
case 'O':
printf("%c\n", value1 || value2);
break;
case 'A':
printf("%c\n", value1 && value2);
break;
case 'N':
printf("%c\n", value1);
break;
default:
printf("Unknown logical operator.\n");
break;
}

system("pause");
return 0;
}```

6. lol nvm guess I was supposed to read those in as an integer when I print in the switch function

7. In the following program, why when I type in "FOF" does it result to "true"? When should I use "=="?

Code:
```#include <stdlib.h>

int main (void){
char value1, value2, loperator;
char result;

//char 1 = true;
//char 0 = false;

printf ("Please enter a logical expression:\n T=true; F=false; O=or; A=and; N=not\n");
scanf ("%c %c %c", &value1, &loperator, &value2);

char T = 1;
char F = 0;

switch(loperator){
case 'O':
if(value1 && value2 == 0){
result = 0;}
if(value1 || value2 == 1){
result = 1;}
break;
case 'A':
result = value1 && value2;
break;
case 'N':
result = value1;
break;
default:
printf("Unknown logical operator.\n");
break;
}

if (result == 1){
printf("true.\n");}
if (result == 0){
printf("false.\n");}

system("pause");
return 0;
}```

8. Code:
```char T = 1;
char F = 0;```
doing this does not do what you think it does - that is, if someone enters F, this does not give that character the value 0. All that code does is declare two useless variables that are not referenced again.

You need to write a bit of code (another switch will do) to parse the character input, and make your value1 0 if it is F and 1 if it is T.

Note that character F has value 15, which is not 0, so F is true if evaluated like an integer as you do there.

9. I'm still getting "true" for everything with the following code. Also, how is F being treated as an integer? I have all my variables initialized as chars...

Code:
```#include <stdio.h>
#include <stdlib.h>

int main (void){
char value1, value2, loperator;
char result;

printf ("Please enter a logical expression:\n T=true; F=false; O=or; A=and; N=not\n");
scanf ("%c %c %c", &value1, &loperator, &value2);

switch(value1){
case 'T':
value1 == 1;
break;
case 'F':
value1 == 0;
break;
default:
printf("Unknown value.\n");
break;
}

switch(value2){
case 'T':
value2 == 1;
break;
case 'F':
value2 == 0;
break;
default:
printf("Unknown value.\n");
break;
}

switch(loperator){
case 'O':
if(value1 && value2 == 0){
result = 0;}
else if(value1 || value2 == 1){
result = 1;}
break;
case 'A':
result = value1 && value2;
break;
case 'N':
result = value1;
break;
default:
printf("Unknown logical operator.\n");
break;
}

if (result == 1){
printf("true.\n");}
else if (result == 0){
printf("false.\n");}

system("pause");
return 0;
}```

10. Code:
```switch(value1){
case 'T':
value1 == 1;
break;
case 'F':
value1 == 0;
break;
default:
printf("Unknown value.\n");
break;```
== is for comparison. For assignment, use =

saying value1==1 is just testing it for equality with 1, not assigning 1 to it.

chars and ints are very closely related in C. You'll need to ask someone with more knowledge than I for the details, but suffice it to say that when you compare two chars (using == or a switch), you are actually comapring their integer (ascii) value.

Also, your O case is wrong anyway:

Code:
```case 'O':
if(value1 && value2 == 0){
result = 0;}
else if(value1 || value2 == 1){
result = 1;}
break;```
This should be just like the others:

Code:
```case 'O':
result = value1 || value2;
break;```

11. I had that for Case O before, but now that I have changed it back AND made the value1 and 2 switch- it is now working! THANKS!

12. I'm now trying to do the extra credit : Make the program loop, asking for input and processing the operators and operands entered. (Hint:
You may need to become familiar with the flush ( ) function, as well as with scanf (). I have the following code, but when I run the program the answers are right but it is printing the default to the switch statements also ("Unknown value" and "Unknown logical operator"). Why is this? Should I be using the fflush function?

Code:
```#include <stdio.h>
#include <stdlib.h>

int main (void){
char value1, value2, loperator;
char result;
int i;

for (i=0; i<3; i++){
printf ("Please enter a logical expression:\n T=true; F=false; O=or; A=and; N=not\n");
scanf ("%c %c %c", &value1, &loperator, &value2);

switch(value1){
case 'T':
value1 = 1;
break;
case 'F':
value1 = 0;
break;
default:
printf("Unknown value.\n");
break;
}

switch(value2){
case 'T':
value2 = 1;
break;
case 'F':
value2 = 0;
break;
default:
printf("Unknown value.\n");
break;
}

switch(loperator){
case 'O':
result = value1 || value2;
break;
case 'A':
result = value1 && value2;
break;
case 'N':
result = value1;
break;
default:
printf("Unknown logical operator.\n");
break;
}

if (result == 1){
printf("true.\n");}
else if (result == 0){
printf("false.\n");}

//fflush(stdout);

}
system("pause");
return 0;
}```

13. Also, could I condense the first two switch statements? like switch(value1&&value2)??

14. Since your application here is going to need to flush input streams, you can tell whoever wrote the assignment that fflush should not be applied to input streams.

basically, the scanf function leaves a newline in the input buffer, so you need to get rid of the newline before you can read in anything new.

Your instructor likely wants you to use fflush(stdin) to do this, which is undefined behaviour. To flush an input stream, use this:

Code:
```int c;
while ((c=getchar())!='\n' && c!=EOF);```
This code snippet eats up characters from the input stream until it encounters and discards a newline, or runs our of input.