把12345用switch和case语句用拼音输出。 要分离出每一位数字从高位开始输出怎么实现? 你的程序要读入一个整数,范围是[-100000,100000]。然后,用汉语拼音将这个整数的每一位输出出来。如输入1234,则输出: yi er san si
时间: 2023-05-09 22:03:24 浏览: 137
使用switch和case语句把12345用拼音输出可以这样写:
int num = 12345;
int digit;
while (num > 0) {
digit = num % 10;
switch (digit) {
case 1:
cout << "yi ";
break;
case 2:
cout << "er ";
break;
case 3:
cout << "san ";
break;
case 4:
cout << "si ";
break;
case 5:
cout << "wu ";
break;
}
num /= 10;
}
要分离出每一位数字从高位开始输出,可以把数字转成字符串,然后按照字符串下标从高位到低位遍历输出每一位对应的拼音。具体实现代码如下:
int num;
cin >> num;
if (num < 0) {
cout << "fu ";
num = -num;
}
string digits = to_string(num);
for (int i = digits.size() - 1; i >= 0; i--) {
switch (digits[i]) {
case '0':
cout << "ling ";
break;
case '1':
cout << "yi ";
break;
case '2':
cout << "er ";
break;
case '3':
cout << "san ";
break;
case '4':
cout << "si ";
break;
case '5':
cout << "wu ";
break;
case '6':
cout << "liu ";
break;
case '7':
cout << "qi ";
break;
case '8':
cout << "ba ";
break;
case '9':
cout << "jiu ";
break;
}
}
阅读全文