1

I'm dealing with RPN calculator and I've found some approach that's using the switch where some of the case doesn't have any expression and break statement:

Here is my approach that's using this algorithm unfortunately it's not working properly As an input for the function I'm using the array of struct defines as follow:

num OPERATORS {val, PLUS, MINUS, MULTI, DIV, SIN, COS, O_PAREN, C_PAREN, END};
typedef struct value {

    double number;
    enum OPERATORS oper;
};

And here is the RPN parser:

void get_the_rpn(struct value values[], struct value rpn_values[], int length) {

    struct value stack[256]; //stack for keeping the values
    int i; //iterator
    int pos; //postion on stack

    i=pos=0;
    //go trough each element in values and reprase it to the rpn format
    while(values[i].oper != END) {

        //check first if current element isn't number
        if(values[i].oper == val) {
            rpn_values[i] = values[i];
            pos++;
        }
        //current element isn't number is an operator
        else
            switch(values[i].oper)
            {
                case PLUS:
                    ;
                case DIV:
                    while (pos && stack[pos-1].oper != O_PAREN &&
                                  stack[pos-1].oper != PLUS &&
                                  stack[pos-1].oper != MINUS) {
                        rpn_values[i] = stack[--pos];
                    }
                    stack[pos++] = values[i];
                    break;
                case MINUS:
                    while (pos && stack[pos-1].oper != O_PAREN) {
                        rpn_values[i] = stack[--pos];
                    }
                    stack[pos++] = values[i];
                    break;
                case MULTI:
                    ;
                case O_PAREN:
                    stack[pos++] = values[i];
                    break;
                case C_PAREN:
                    while (stack[pos-1].oper != O_PAREN) {
                        rpn_values[i] = stack[--pos];
                    }
                    pos--;
                    break;
                case SIN:
                    rpn_values[i] = values[i];
                    break;  //edited
                case COS:
                    rpn_values[i] = values[i];
                    break;  //edited
            }
        i++;

    }
}

The question mostly is why for PLUS and MULTI in the case clause there is no statement simply ;?

If someone would like to see whole code and maybe could found the bug here is the whole calculate.c program http://pastebin.com/WteqbmJg

advena
  • 83
  • 13
  • 1
    A equally entertaining question would be why is the same *not* done for `SIN` above `COS`. Its called *falling through* a switch-case, btw, and the fiend that wrote this should be consistent. They should also provide a proper `default:` for catching unexpected values like `val` or `END` (though those can't technically make it into the switch with the preamble code in-place). – WhozCraig May 23 '14 at 18:44

1 Answers1

4

In a switch() statement, control is passed to the matching case.

A case without a break (or anything else that might redirect control) will continue to execute the code in the next 'case' (including the default case).

In the question code, the reason that PLUS has no statement (even the ; is optional), is that it 'falls-through' to the DIV case. Just as the MULTI case falls-through to the O_PAREN case,

All very legitimate C coding.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • 3
    True its legitimate coding. Suggest a coding style that has a comment indicating intentional fall through. `case PLUS: /* fall through */ ;` – chux - Reinstate Monica May 23 '14 at 18:47
  • 1
    @chux, agreed. In the question code, such a construct is easily overlooked. – Mahonri Moriancumer May 23 '14 at 18:49
  • 1
    @chux likewise agreed. If you don't terminate a case label with a break, you better put a comment specifying that intent so the "helpful" engineer that follows you knows it was intentional. – WhozCraig May 23 '14 at 18:50
  • That's what I thought but in this approach what is happening with the `'+'` operator? It's not going into the `stack`... My solution is some combination of preprepared algorithm for RPN the use of `struct` array was my idea, not pretty sure if t;s correct. p.s. I added the `break` after `COS` and `SIN` – advena May 23 '14 at 20:01