【问题描述】
从键盘接收用户输入的字符串, 对用户输入的每个字符串的处理是:将字符串内的每一个十进制数字字符置换成下列表格中右边所对应的一个字符串(所有其他字符不变),然后将转换的结果显示在屏幕上;并分别计算每个数字的置换次数。
十进制数字字符 | 置换成 |
0 | (Zero) |
1 | (One) |
2 | (Two) |
3 | (Three) |
4 | (Four) |
5 | (Five) |
6 | (Six) |
7 | (Seven) |
8 | (Eight) |
9 | (Nine) |
例如,若用户输入的字符串为
Page112-Line3,
则程序5的输出是:
Page(One) (One) (Two)-Line(Three),
数字0到9的置换次数分别是 0 2 1 1 0 0 0 0 0 0
【输入形式】
输入一行字符串,其中可包含字母、数字、空格或其他符号(英文)
【输出形式】
第一行为将字符串中的数字转换为表格中的内容后输出
第二行为数字0~9被转换的次数
【样例输入】
Page112-Line3
【样例输出】
Page(One)(One)(Two)-Line(Three) 0 2 1 1 0 0 0 0 0 0
#include<bits/stdc++.h>
using namespace std;
string change(char);//将数字转换为对应字符
int main() {
map<int,int>num;
for(int i=0; i<10; i++) {
num[i]=0;
}
string str1;
getline(cin,str1);
char tempc;
for(int i=0; i<str1.size(); i++) {
tempc=str1[i];
if(tempc>='0'&&tempc<='9') { //满足置换条件
cout<<change(tempc);
num[tempc-'0']++;
} else {
cout<<tempc;
}
}
cout<<endl;
// map<int,int>::iterator it;
// for(it=num.begin(); it!=num.end(); it++) {
// cout<<it->second<<' ';
// }
for(int i=0; i<10;i++)
cout<<num[i]<<' ';
return 0;
}
string change(char a) {
string temp;
switch(a) {
case '0': {
temp="(Zero)";
break;
}
case '1':
temp="(One)";
break;
case '2':
temp="(Two)";
break;
case '3':
temp="(Three)";
break;
case '4':
temp="(Four)";
break;
case '5':
temp="(Five)";
break;
case '6':
temp="(Six)";
break;
case '7':
temp="(Seven)";
break;
case '8':
temp="(Eight)";
break;
case '9':
temp="(Nine)";
break;
}
return temp;
}