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