我正在处理 RPN 计算器,我发现了一些使用switch
其中一些case
没有任何表达式和break
语句的方法:
这是我使用此算法的方法,不幸的是它无法正常工作作为函数的输入,我使用的struct
定义数组如下:
num OPERATORS {val, PLUS, MINUS, MULTI, DIV, SIN, COS, O_PAREN, C_PAREN, END};
typedef struct value {
double number;
enum OPERATORS oper;
};
这是 RPN 解析器:
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++;
}
}
问题主要是为什么 for PLUS
andMULTI
在case
子句中没有简单的语句;
?
如果有人想查看整个代码并且可能会发现这里的错误是整个calculate.c
程序http://pastebin.com/WteqbmJg