Learning how to code here, and I've spent a good long day coding what appears to be inefficient code on my part.Here is a summary of the assignment I tackled (this is all for self-study, but C is something I've wanted to learn for a long time):Using only switch/if selection statements, program a C calculator that handles simple expressions involving three values and two operators e.g. a op1 b op2 c. The calculator must perform most operations provided by C language. Should handle values of type int or float (as appropriate), and to handle the following operators: <, >, <=, >=, ==, !=, !, &&, ||, %After about ~2900 lines, I think I've got it (or at least most of it). It's too long for my own happiness. Here is a link to my code.Question: Is there a more efficient way to program this C calculator using only switch/if selection statements? I was told that I can use precedence to my advantage, but can someone give a concrete example (maybe even a few lines of code) with an explanation?printf(Program 5.9: \C\ Calculator for Three Numbers (e.g x op1 y op2 z)\n);printf(=================================================================\n\n);printf(Enter your first number: );scanf(%f, &a);printf(Enter your second number: );scanf(%f, &b);printf(Enter your third number: );scanf(%f, &c);printf(\nChoice of Arithmetic or Logical Operators between First and Second Number:\n);printf(==========================================================================\n\n);printf(Addition:                 (1)\n);printf(Subtraction:              (2)\n);printf(Multiplication:           (3)\n);printf(Division:                 (4)\n);printf(Less than:                (5)\n);printf(Greater than:             (6)\n);printf(Less than or equal to:    (7)\n);printf(Greater than or equal to: (8)\n);printf(Equals:                   (9)\n);printf(Does not equal (x != y):  (10)\n);printf(Logical NOT    (x !y):    (11)\n);printf(Logical AND    (x && y):  (12)\n);printf(Logical OR     (x || y):  (13)\n);printf(Remainder:                (14)\n);printf(\nPlease enter your choice: );scanf(%d, &choice);if ((choice < 1) || (choice > 14)) {    printf(Incorrect Choice!\n);    return 1;}if (choice == 11){logi_not = TRUE;printf(\nHow do you want to analyze %.2f and !%.2f?: , a, b);printf(\n\n%.2f && !%.2f:            (15)\n, a, b);printf(%.2f || !%.2f:            (16)\n, a, b);printf(\nPlease enter your choice: );scanf(%d, &choice_3);switch (choice_3){    case 15: logic_and_log_not = TRUE;         break;    case 16: logic_or_log_not = TRUE;        break;}}printf(\nChoice of Arithmetic or Logical Operators between Second and Third Number:\n);printf(==========================================================================\n\n);printf(Addition:                 (1)\n);printf(Subtraction:              (2)\n);printf(Multiplication:           (3)\n);printf(Division:                 (4)\n);printf(Less than:                (5)\n);printf(Greater than:             (6)\n);printf(Less than or equal to:    (7)\n);printf(Greater than or equal to: (8)\n);printf(Equals:                   (9)\n);printf(Does not equal (y != z):  (10)\n);printf(Logical NOT    (y !z):    (11)\n);printf(Logical AND    (y && z):  (12)\n);printf(Logical OR     (y || z):  (13)\n);printf(Remainder:                (14)\n);printf(\nPlease enter your choice: );scanf(%d, &choice_2);if ((choice_2 < 1) || (choice_2 > 14)) {    printf(Incorrect Choice!\n);    return 1;}if (choice_2 == 11){    logi_not_a = TRUE;    printf(\nHow do you want to analyze );    switch (choice) {        case 1: add = TRUE;            printf(%.2f + %.2f , a, b); break;        case 2: subtract = TRUE;            printf(%.2f - %.2f , a, b); break;        case 3: multiply = TRUE;            printf(%.2f * %.2f , a, b); break;        case 4: divide = TRUE;            printf(%.2f / %.2f , a, b); break;        case 5: less_than = TRUE;            printf(%.2f < %.2f , a, b); break;        case 6: greater_than = TRUE;            printf(%.2f > %.2f , a, b); break;        case 7: less_than_equal = TRUE;            printf(%.2f <= %.2f , a, b); break;        case 8: greater_than_equal = TRUE;            printf(%.2f >= %.2f , a, b); break;        case 9: equals = TRUE;            printf(%.2f = %.2f , a, b); break;        case 10: does_not_equal = TRUE;            printf(%.2f != %.2f , a, b); break;        case 11: logi_not = TRUE;{            if (logic_and_log_not)                printf(%.2f && !%.2f , a, b);            else if (logic_or_log_not)                printf(%.2f || !%.2f , a, b); break;        }        case 12: logi_and = TRUE;            printf(%.2f && %.2f , a, b); break;        case 13: logi_or = TRUE;            printf(%.2f || %.2f , a, b); break;        case 14: remain_mod = TRUE;            printf(%.0f %% %.0f , a, b); break;    }    printf(with !%.2f?:\n\n, c);    switch (choice) {        case 1: add = TRUE;            printf(%.2f + %.2f , a, b); break;        case 2: subtract = TRUE;            printf(%.2f - %.2f , a, b); break;        case 3: multiply = TRUE;            printf(%.2f * %.2f , a, b); break;        case 4: divide = TRUE;            printf(%.2f / %.2f , a, b); break;        case 5: less_than = TRUE;            printf(%.2f < %.2f , a, b); break;        case 6: greater_than = TRUE;            printf(%.2f > %.2f , a, b); break;        case 7: less_than_equal = TRUE;            printf(%.2f <= %.2f , a, b); break;        case 8: greater_than_equal = TRUE;            printf(%.2f >= %.2f , a, b); break;        case 9: equals = TRUE;            printf(%.2f = %.2f , a, b); break;        case 10: does_not_equal = TRUE;            printf(%.2f != %.2f , a, b); break;        case 11: logi_not = TRUE;{            if (logic_and_log_not)                printf(%.2f && !%.2f , a, b);            else if (logic_or_log_not)                printf(%.2f || !%.2f , a, b); break;        }        case 12: logi_and = TRUE;            printf(%.2f && %.2f , a, b); break;        case 13: logi_or = TRUE;            printf(%.2f || %.2f , a, b); break;        case 14: remain_mod = TRUE;            printf(%.0f %% %.0f , a, b); break;    }    printf( && !%.2f:  (15)\n, c);    switch (choice) {        case 1: add = TRUE;            printf(%.2f + %.2f , a, b); break;        case 2: subtract = TRUE;            printf(%.2f - %.2f , a, b); break;        case 3: multiply = TRUE;            printf(%.2f * %.2f , a, b); break;        case 4: divide = TRUE;            printf(%.2f / %.2f , a, b); break;        case 5: less_than = TRUE;            printf(%.2f < %.2f , a, b); break;        case 6: greater_than = TRUE;            printf(%.2f > %.2f , a, b); break;        case 7: less_than_equal = TRUE;            printf(%.2f <= %.2f , a, b); break;        case 8: greater_than_equal = TRUE;            printf(%.2f >= %.2f , a, b); break;        case 9: equals = TRUE;            printf(%.2f = %.2f , a, b); break;        case 10: does_not_equal = TRUE;            printf(%.2f != %.2f , a, b); break;        case 11: logi_not = TRUE;{            if (logic_and_log_not)                printf(%.2f && !%.2f , a, b);            else if (logic_or_log_not)                printf(%.2f || !%.2f , a, b); break;        }        case 12: logi_and = TRUE;            printf(%.2f && %.2f , a, b); break;        case 13: logi_or = TRUE;            printf(%.2f || %.2f , a, b); break;        case 14: remain_mod = TRUE;            printf(%.0f %% %.0f , a, b); break;    }    printf( || !%.2f:  (16)\n, c);    printf(\nPlease enter your choice: );    scanf(%d, &choice_4);    switch (choice_4){        case 15: logic_and_log_not_a = TRUE;            break;        case 16: logic_or_log_not_a = TRUE;            break;    }}/*******************************************************//* Printing which choice was made between var 1 and 2. *//*******************************************************/printf(\nYou have chosen );switch (choice) {    case 1: add = TRUE;        printf(%.2f + %.2f , a, b); break;    case 2: subtract = TRUE;        printf(%.2f - %.2f , a, b); break;    case 3: multiply = TRUE;        printf(%.2f * %.2f , a, b); break;    case 4: divide = TRUE;        printf(%.2f / %.2f , a, b); break;    case 5: less_than = TRUE;        printf(%.2f < %.2f , a, b); break;    case 6: greater_than = TRUE;        printf(%.2f > %.2f , a, b); break;    case 7: less_than_equal = TRUE;        printf(%.2f <= %.2f , a, b); break;    case 8: greater_than_equal = TRUE;        printf(%.2f >= %.2f , a, b); break;    case 9: equals = TRUE;        printf(%.2f = %.2f , a, b); break;    case 10: does_not_equal = TRUE;        printf(%.2f != %.2f , a, b); break;    case 11: logi_not = TRUE;{        if (logic_and_log_not)            printf(%.2f && !%.2f , a, b);        else if (logic_or_log_not)            printf(%.2f || !%.2f , a, b); break;    }    case 12: logi_and = TRUE;        printf(%.2f && %.2f , a, b); break;    case 13: logi_or = TRUE;        printf(%.2f || %.2f , a, b); break;    case 14: remain_mod = TRUE;        printf(%.0f %% %.0f , a, b); break;}/*******************************************************//* Printing which choice was made between var 2 and 3. *//*******************************************************/switch (choice_2) {    case 1: add_a = TRUE;        printf(+ %.2f.\n, c); break;    case 2: subtract_a = TRUE;        printf(- %.2f.\n, c); break;    case 3: multiply_a = TRUE;        printf(* %.2f.\n, c); break;    case 4: divide_a = TRUE;        printf(/ %.2f.\n, c); break;    case 5: less_than_a = TRUE;        printf(< %.2f.\n, c); break;    case 6: greater_than_a = TRUE;        printf(> %.2f.\n, c); break;    case 7: less_than_equal_a = TRUE;        printf(<= %.2f.\n, c); break;    case 8: greater_than_equal_a = TRUE;        printf(>= %.2f.\n, c); break;    case 9: equals_a = TRUE;        printf(= %.2f.\n, c); break;    case 10: does_not_equal_a = TRUE;        printf(!= %.2f.\n, c); break;    case 11: logi_not_a = TRUE;{        if (logic_and_log_not_a)            printf(&& !%.2f , c);        else if (logic_or_log_not_a)            printf(|| !%.2f , c); break;    }    case 12: logi_and_a = TRUE;        printf(&& %.2f.\n, c); break;    case 13: logi_or_a = TRUE;        printf(|| %.2f.\n, c); break;    case 14: remain_mod_a = TRUE;        printf(%% %.0f.\n, c); break;}/*******************************************************//*                 Calculation for ADD.                *//*******************************************************/if(add && add_a){    x = a + b + c;    printf(\nAnswer is %.2f\n\n, x);    return 1;}if(add && subtract_a){    x = a + b - c;    printf(\nAnswer is %.2f\n\n, x);    return 1;}if(add && multiply_a){    x = a + b * c;    printf(\nAnswer is %.2f\n\n, x);    return 1;}if(add && divide_a){    if (c == 0){        printf(\nThe solution does not exist!\n\n);        return 5;    }    x = a + b / c;    printf(\nAnswer is %.2f\n\n, x);    return 1;}if(add && less_than_a){    x = a + b < c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 8;}if(add && greater_than_a){    x = a + b > c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 8;}if(add && less_than_equal_a){    x = a + b <= c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 8;}if(add && greater_than_equal_a){    x = a + b >= c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 8;}if(add && equals_a){    x = a + b == c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 8;}if(add && does_not_equal_a){    x = a + b != c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 8;}if(add && logi_not_a && logic_and_log_not_a){    x = a + b && !c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 8;}if(add && logi_not_a && logic_or_log_not_a){    x = a + b || !c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 8;}if(add && logi_and_a){    x = a + b && c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 9;}if(add && logi_or_a){    x = a + b || c;    if (x == 0)        printf(\nThe statement is FALSE.\n\n);    else        printf(\nThe statement is TRUE.\n\n);    return 9;}if(add && remain_mod_a){    y = a, z = b, d = c;    w = y + z % d;    printf(\nThe answer is %d\n\n, w);    return 4;}
Handling simple expressions involving three values and two operators
beginner;c;calculator
Your code is very straightforward and easy to understand. Given the limited types of statements you're allowed to use, this is a reasonable answer. You say you're unhappy with the length, and that says to me that you know there's a better way, but just can't quite figure out what it is. You're right! Here's how I would go about it.SimplifyYou've been asked to handle 16 different operations for 2 operators. That gives you 16 * 16 = 256 different possible combinations. But that doesn't mean that you have to write cases for all 256. You should be able to do it with just 16 + 16 operations.To demonstrate, let's reduce the number of operators to just 4 - addition, subtraction, multiplication, and division. With 4 operators you have 16 possible combinations - add and add, add and subtract, add and multiply, add and divide, subtract and add, subtract and subtract, etc.What you can do is calculate the operation of the first operator and store the result in an intermediate variable. Then use it in the second operation. I'll assume you've already gotten the user's input here. The numbers are in a, b, and c, and the operators are in variables I'll call operator1 and operator2. First, I'd make an enum that describes the possible operators:enum {    OP_ADD = 0,    OP_SUBTRACT,    OP_MULTIPLY,    OP_DIVIDE};Then once you have the above inputs, you can do something like this:float intermediate = 0.0;switch (operator1) {    case OP_ADD:        intermediate = a + b;    break;    case OP_SUBTRACT:        intermediate = a - b;    break;    case OP_MULTIPLY:        intermediate = a * b;    break;    case OP_DIVIDE:        intermediate = a / b;    break;};float result = 0.0;switch (operator2) {    case OP_ADD:        result = intermediate + c;    break;    case OP_SUBTRACT:        result = intermediate - c;    break;    case OP_MULTIPLY:        result = intermediate * c;    break;    case OP_DIVIDE:        result = intermediate / c;    break;};So now we've done the operations with 4 + 4 cases instead of 4 * 4 cases.Operator PrecedenceNow you might have noticed that this doesn't properly handle operator precedence. For example, when you see a + b * c, it should be handled as a + (b * c). This can be handled by making an array that holds the precedence of each operator, and looking it up before doing the actual operations. It could work something like this:const int operator_precedence[] = {    0,    // OP_ADD    0,    // OP_SUBTRACT    1,    // OP_MULTIPLY    1,    // OP_DIVIDE};int op1_prec = operator_precedence [ operator1 ];int op2_prec = operator_precedence [ operator2 ];If we find that operator 2 has higher precedence, we need to switch the order of operations. We can do that like this:    bool swap_vars = FALSE;if (op1_prec < op2_prec){    // Swap the operators    int tempOp = operator1;    operator1 = operator2;    operator2 = tempOp;    // Perform (b op2 c) before working with a    float temp_var = a;    a = b;    b = c;    c = temp_var;    // we'll need to swap variables later    swap_vars = TRUE;}Now this will give us b op2 c op1 a which is still not quite right. (Imagine if op1 is either subtract or divide.) So once we've calculated the intermediate variable we'll need to swap the intermediate and c so we get the order right.if (swap_vars){    float temp_var = intermediate;    intermediate = c;    c = temp_var;}