怎样用指针和数组,不用if和switch,读出自然数的汉语拼音?
char *shu[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int main()
{
int n, len = 0, dx = 1, t;
scanf("%d", &n);
t = n;
while (t)
{
t /= 10;
len++;
dx *= 10;
}
for (dx /= 10; len > 0 && n; len--)
{
t = n / dx;
printf("%s ", shu[t]);
n %= dx;
dx /= 10;
}
return 0;
}
读出自然数的汉语拼音,供参考:
#include <stdio.h>
int main()
{
char* num[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
char c[100] = { 0 };
int i = 0;
scanf("%s", c);
for (i = 0; c[i] != '\0'; i++)
printf(i == 0 && c[i] == '-' ? "fu" : i == 0 ? "%s" : " %s", num[c[i] - '0']);
return 0;
}