定义一个map对象,建立阿拉伯数字0-9与英文单词zero-nine的映射关系,实现输入一个阿拉伯数字,输出与之对应的英文单词。
时间: 2024-05-05 20:19:02 浏览: 139
用户输入阿拉伯数字,输出相应汉字
3星 · 编辑精心推荐
好的,以下是实现您提出的问题的代码:
```
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// 定义一个map对象,建立阿拉伯数字0-9与英文单词zero-nine的映射关系
map<int, string> numbers;
numbers[0] = "zero";
numbers[1] = "one";
numbers[2] = "two";
numbers[3] = "three";
numbers[4] = "four";
numbers[5] = "five";
numbers[6] = "six";
numbers[7] = "seven";
numbers[8] = "eight";
numbers[9] = "nine";
// 输入一个阿拉伯数字
int input_number;
cout << "Please input a number between 0 and 9: " << endl;
cin >> input_number;
// 输出与之对应的英文单词
if (numbers.count(input_number) > 0) {
cout << "The English word for " << input_number << " is " << numbers[input_number] << endl;
} else {
cout << "Invalid input number." << endl;
}
return 0;
}
```
输出结果示例:
```
Please input a number between 0 and 9:
2
The English word for 2 is two
```
阅读全文