在if陈述中处理多个或的更优雅的方法是什么 [英] What is a more elegant way of dealing with multiple or's in an if statment
本文介绍了在if陈述中处理多个或的更优雅的方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码来检查char数组中的每个char是否满足一定数量的属性: *是一个数字 *或为(+,-,*,/)
I have this code to check every char in a char array if it satisfies a set number of properties: * Is a number * or is a (+, -, *, /)
bool chkArray(char input[]) {
for (auto x = 0; x < strlen(input); x++) {
if (isdigit(input[x]) || input[x] == '+' || input[x] == '-' || input[x] == '*' || input[x] == '/' || input[x] == ' ') {
continue;
}
else {
return false;
}
}
return true;
}
我觉得有一种更优雅的方式来处理用于检查(+,-,*,/)的倍数或.像这样:
I feel like there is a more elegant way of dealing with the multiple or's that check for the (+, -, *, /). Something like this:
bool chkArray(char input[]) {
for (auto x = 0; x < strlen(input); x++) {
if (isdigit(input[x]) || input[x] == '+', '-', '*', '/', ' ') {
continue;
}
else {
return false;
}
}
return true;
}
所以我现在想知道是否有人可以替代原始代码以使其更优雅?
So I am now wondering if anybody has an alternative to the original code to make it more elegant ?
推荐答案
老式的解决方案是使用 std::strchr
:
The oldschool solution is to use std::strchr
:
if (isdigit(input[x]) || std::strchr("+-*/ ", input[x]))
这篇关于在if陈述中处理多个或的更优雅的方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文