如何更好地检查两个 char 变量是否在一组值中?

How can I better check whether two char variables are in some set of values?

本文关键字:是否 一组 变量 两个 更好 何更好 检查 char      更新时间:2023-10-16

最近,我们的教授要求我们使用两个char变量(天(来接收用户的输入。

下面的代码可以很好地作为检查,以确保Mo,Tu,We,Th,Fr,Sa,Su是作为一对一起输入的仅有的两个字符。如果收到任何其他内容作为输入,它将循环并要求用户提供有效输入。

输入应不区分大小写,这意味着,例如,"mO""tu"是可以接受的。似乎有很多重复正在发生。有没有办法清理它?

cout << "Please enter the day of the week did you made the long distance call (Mo Tu We Th Fr Sa Su): ";
cin >> dayOne >> dayTwo;
while ((dayOne != 'M' && dayOne != 'm' || dayTwo != 'O' && dayTwo != 'o') &&
(dayOne != 'T' && dayOne != 't' || dayTwo != 'U' && dayTwo != 'u') &&
(dayOne != 'W' && dayOne != 'w' || dayTwo != 'e' && dayTwo != 'E') &&
(dayOne != 'T' && dayOne != 't' || dayOne != 'H' && dayTwo != 'h') &&
(dayOne != 'F' && dayOne != 'f' || dayTwo != 'R' && dayTwo != 'r') &&
(dayOne != 'S' && dayOne != 's' || dayTwo != 'A' && dayTwo != 'a') &&
(dayOne != 'S' && dayOne != 's' || dayTwo != 'U' && dayTwo != 'u'))
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
cout << endl << "You have entered an invalid day. Please re-enter a day in the correct format (Mo Tu We Th Fr Sa Su): ";
cin >> dayOne >> dayTwo;
}

您可以编写一个折叠表达式,将 2 个字符与字符串进行比较:

template<typename ...Days>
bool any_of(char a, char b, Days ...days)
{
return (... || (a == days[0] && b == days[1]));
}

然后像这样使用它:

while (! any_of(std::tolower(dayOne), std::tolower(dayTwo), "mo", "tu", "we", "th", "fr", "sa", "su"))
// keep asking for input

这是一个演示。

这应该满足使用 2 个char输入的要求。

通常使用tolowertoupper首先将char变量转换为正确的大小写。我喜欢使用tolower- 它看起来稍微好一点。

dayOne = tolower(dayOne);
dayTwo = tolower(dayTwo);
while (
(dayOne != 'm' || dayTwo != 'o') &&
(dayOne != 't' || dayTwo != 'u') &&
(dayOne != 'w' || dayTwo != 'e') &&
(dayOne != 't' || dayTwo != 'h') &&
(dayOne != 'f' || dayTwo != 'r') &&
(dayOne != 's' || dayTwo != 'a') &&
(dayOne != 's' || dayTwo != 'u'))
{
...
}

您可以通过使用memcmp一次比较两个字符来进一步更改它,但我不确定它会简化代码。

另一种可能值得一提的方法是组织数据,以便您可以使用 std 函数来对付它 (std::find(

// Example program
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
int main()
{
const std::vector<std::string> days = {
"mo", "tu", "we", "th", "fr", "sa", "su"
};
bool found = false;
while (found == false) {
char dayOne, dayTwo;
std::cout << "Please enter the first letter of the day" << std::endl;
std::cin >> dayOne;
std::cout << "Please enter the second letter of the day" << std::endl;
std::cin >> dayTwo;
std::string fullDay;
fullDay += std::tolower(dayOne);
fullDay += std::tolower(dayTwo);
found = std::find(days.begin(), days.end(), fullDay) != days.end();
std::cout << (found ? "correct day " : "invalid day, please try again ")
<< fullDay
<< std::endl;
}
}

在这里运行它

怎么样

switch (256 * tolower(dayOne) + tolower(dayTwo))
{
case 256 * 'm' + 'o':
// Monday
case 256 * 't' + 'u':
// Tuesday
}

等等?

不知道您是否正在使用/允许正则表达式,但我会像这样解决它:

bool isDayOfTheWeek(char a, char b)
{
std::string day({a, b});
std::regex pattern("Mo|Tu|We|Th|Fr|Sa|Su", std::regex_constants::icase);
return std::regex_search(day, pattern);
}

然后简单地:

cout << "Please enter the day of the week did you made the long distance call (Mo Tu We Th Fr Sa Su): ";
cin >> dayOne >> dayTwo;
while (!isDayOfTheWeek(dayOne, dayTwo))
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
cout << endl << "You have entered an invalid day. Please re-enter a day in the correct format (Mo Tu We Th Fr Sa Su): ";
cin >> dayOne >> dayTwo;
}

我会首先将输入转换为小写,这减少了可能的组合数量。然后我会每天用一个 if 语句来解决它:

// returns 0-6 for valid days, -1 for invalid ones
int dayOfWeek(char a, char b) {
a = tolower(a); // requires #include <cctype>
b = tolower(b);
if (a == 'm' && b == 'o') return 0;
// 5 more here
if (a == 's' && b == 'u') return 6;
return -1;      // date was invalid
}

然后我会按照@PaulEvans的建议使用它:

cout << "Please enter the day of the week did you made the long distance call (Mo Tu We Th Fr Sa Su): ";
cin >> dayOne >> dayTwo;
int day = -1;
while ((day = dayOfWeek(dayOne, dayTwo)) == -1)
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
cout << endl << "You have entered an invalid day. Please re-enter a day in the correct format (Mo Tu We Th Fr Sa Su): ";
cin >> dayOne >> dayTwo;
}
// day is 0 for monday, ... 6 for sunday