Thread: logic calculator

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    Question 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. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Convert T to 1 and F to 0. Then use the logical operators in C. For example:

    T O T => 1 || 1

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    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. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    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. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    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. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    lol nvm guess I was supposed to read those in as an integer when I print in the switch function

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    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. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    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. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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;
    Last edited by KBriggs; 12-01-2010 at 07:39 PM.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    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. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    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. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    Also, could I condense the first two switch statements? like switch(value1&&value2)??

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator for logic operations
    By sspiritss in forum C Programming
    Replies: 1
    Last Post: 12-07-2008, 07:03 AM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM