所以我在学校做了一项任务,建立了一个计算器。 该程序应该包含一个“parcer.c”文件,其中所有不允许的输入都被排序,如果提交了任何不允许的值和输入,它应该给你一个“语法错误”-message。
我已经完成了,只留下了一件事,就是这样,如果应该检查并确保sizeof( op)不大于1,这意味着它只能包括1个符号(+, - ,,/,%) 但我的问题是,我仍然可以写; 4 + sdf 5并得到结果= 9 但我不能写4sdf 5或4 sdf 5,因为我得到了我的“语法错误”,这是正确的。
当我在操作员之后遇到垃圾时,我应该转向给我同样的错误? EX:4 + sdf 5
if(sizeof (*op) > 1 || !validOperator(op))
{
//Returns statuscode 2 and a "Syntax Error" if the operator
is consisting of more than 1 sign
printf("\n Syntax Error, only +, -, x, /, %% are allowed\n");
return 2;
}
答案 0 :(得分:1)
我认为op被定义为
char *op; /*or maybe as char op[]*/
使用sizeof(* op)检查时,检查字符数组中第一个元素的大小,即它将始终返回1.
您想使用strlen()而不是sizeof()。请尝试以下更改:
if( op!=0 && (strlen(*op) > 1 || !validOperator(op)) )