我无法在运算符之间进行比较,它只需要 1 个输入,然后程序崩溃。
char operatorValue;
do
{
printf("\nEnter Operator:");
scanf("%c", &operatorValue);
} while (strcmp(operatorValue, '+') != 0 || strcmp(operatorValue, '-') != 0 ||
strcmp(operatorValue, '*') != 0 || strcmp(operatorValue, '/') != 0);
使用strchr函数可能更容易,定义为
char *strchr(const char *string, int c);
它查找字符串中字符的第一次出现。字符 c 可以是空字符 (\0);字符串的结尾空字符包含在搜索中。如果未找到该字符,则返回 NULL。有关完整说明和使用示例,请参阅以下页面
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rtref/strchr.htm
char target = "+-*/";
....
} while (strchr(target, (int) operatorValue)) ==0); // loops until it gets a match
然后,如果您想在搜索字符串中添加更多字符,这很容易。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句