第一版代码
#include<iostream>
using namespace std;
void pinyin(int a)
{
switch (a)
{
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;
case 0:cout << "ling"; break;
default:break;
}
}
int main()
{
char a[101],i=0,j=0;
int sum = 0;
cin >> a;
while ( a[i]!='\0')
{
sum += a[i] - '0';//字符型数据转换为整型
i++;
}
while (sum)
{
a[j++] = sum % 10;
sum /= 10;
}
for (int i = j - 1; i >= 0; --i)
{
pinyin(a[i]);
if (i != 0)
{
cout << " ";
}
}
return 0;
}
第二版代码
#include<iostream>
using namespace std;
int main()
{
const char * pinyin[] = { "ling","yi", "er", "san", "si", "wu", "liu", "qi", "ba","jiu"};
char a[101], i = 0, j = 0;
int sum = 0;
cin >> a;
while (a[i] != '\0')
{
sum += a[i] - '0';//字符型数据转换为整型
i++;
}
while (sum)
{
a[j++] = sum % 10;
sum /= 10;
}
for (int i = j - 1; i >= 0; --i)
{
cout << pinyin[a[i]];
if (i != 0)
{
cout << " ";
}
}
return 0;
}