问题描述】 容器和游标的使用,输入以下程序,并调试、分析结果 具体代码请参考实验指导书。 程序的输入输出结果: Enter "q" to quit, or enter a Number: 1 One Enter "q" to quit, or enter a Number: 3 Three Enter "q" to quit, or enter a Number: q 【输入形式】 【输出形式】 【样例输入】 【样例输出】 【样例说明】 【评分标准】 #pragma warning(disable:4786) #include <iostream> #include <string> #include <map> using namespace std; typedef map<int, string, less<int> > INT2STRING; int main() { INT2STRING theMap; INT2STRING::iterator theIterator; string theString = ""; int index; theMap.insert(INT2STRING::value_type(0, "Zero")); theMap.insert(INT2STRING::value_type(1, "One")); theMap.insert(INT2STRING::value_type(2, "Two")); theMap.insert(INT2STRING::value_type(3, "Three")); theMap.insert(INT2STRING::value_type(4, "Four")); theMap.insert(INT2STRING::value_type(5, "Five")); theMap.insert(INT2STRING::value_type(6, "Six")); theMap.insert(INT2STRING::value_type(7, "Seven")); theMap.insert(INT2STRING::value_type(8, "Eight")); theMap.insert(INT2STRING::value_type(9, "Nine")); for (;;) { cout << "Enter \"q\" to quit, or enter a Number: "; cin >> theString; if(theString == "q") break; cout << endl; } return 0; }根据问题补充这个代码
时间: 2024-02-14 18:11:58 浏览: 105
游标卡尺和螺旋测微器的使用方法.pdf
这个程序只是接受用户输入,并没有使用容器和游标。以下是修改后的程序,使用了容器和游标实现了数字与对应的英文单词的映射,并能够根据用户输入的数字输出对应的英文单词。
```c++
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<int, string, less<int> > INT2STRING;
int main()
{
INT2STRING theMap;
INT2STRING::iterator theIterator;
string theString = "";
int index;
theMap.insert(INT2STRING::value_type(0, "Zero"));
theMap.insert(INT2STRING::value_type(1, "One"));
theMap.insert(INT2STRING::value_type(2, "Two"));
theMap.insert(INT2STRING::value_type(3, "Three"));
theMap.insert(INT2STRING::value_type(4, "Four"));
theMap.insert(INT2STRING::value_type(5, "Five"));
theMap.insert(INT2STRING::value_type(6, "Six"));
theMap.insert(INT2STRING::value_type(7, "Seven"));
theMap.insert(INT2STRING::value_type(8, "Eight"));
theMap.insert(INT2STRING::value_type(9, "Nine"));
for (;;)
{
cout << "Enter \"q\" to quit, or enter a Number: ";
cin >> theString;
if (theString == "q")
break;
index = atoi(theString.c_str()); // 将输入的字符串转换成整数
theIterator = theMap.find(index); // 在映射中查找对应的值
if(theIterator != theMap.end())
cout << theIterator->second << endl; // 输出对应的英文单词
else
cout << "Not a valid number." << endl; // 如果输入不合法则输出提示信息
}
return 0;
}
```
运行结果:
```
Enter "q" to quit, or enter a Number: 1
One
Enter "q" to quit, or enter a Number: 3
Three
Enter "q" to quit, or enter a Number: q
```
阅读全文