c++用枚举值MON、TUE、WED、THU、FRI、SAT和SUN表示一个星期中的七天。键盘输入一个0~6的整数,根据输入的整数输出对应的英文缩写
时间: 2024-10-27 07:06:58 浏览: 41
在C++中,你可以通过枚举类型来定义一周的天数,并创建一个映射表将整数值转换成英文缩写。首先,定义枚举类型:
```cpp
enum class DayOfWeek {
MON = 0,
TUE = 1,
WED = 2,
THU = 3,
FRI = 4,
SAT = 5,
SUN = 6
};
```
然后,你可以创建一个辅助函数来进行整数到英文缩写的转换:
```cpp
std::string dayOfWeekToString(DayOfWeek day) {
switch (day) {
case DayOfWeek::MON:
return "MON";
case DayOfWeek::TUE:
return "TUE";
case DayOfWeek::WED:
return "WED";
case DayOfWeek::THU:
return "THU";
case DayOfWeek::FRI:
return "FRI";
case DayOfWeek::SAT:
return "SAT";
case DayOfWeek::SUN:
return "SUN";
default:
throw std::invalid_argument("Invalid day number");
}
}
```
最后,用户输入并处理数据部分:
```cpp
int inputDay;
std::cout << "Enter a day number from 0 to 6: ";
std::cin >> inputDay;
if (inputDay >= 0 && inputDay < 7) {
DayOfWeek dayEnum = static_cast<DayOfWeek>(inputDay);
std::cout << "The corresponding day is: " << dayOfWeekToString(dayEnum) << std::endl;
} else {
std::cerr << "Invalid input! Please enter a number between 0 and 6." << std::endl;
}
```
阅读全文