我正在尝试编写一个C程序来打印整数的字符串形式。例如,1234 - >一二三四。是否有一个可以转换它的函数,或者我会创建一个数组来执行此操作?
答案 0 :(得分:2)
一个简单的递归解决方案:
void print_number(unsigned x) {
static const char *digit[10] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
if (x >= 10) {
print_number(x / 10);
fputc(' ', stdout);
}
fputs(digit[x % 10], stdout);
}
答案 1 :(得分:1)
我不认为你有直接的功能转换为文本。但是你可以做这样的事情
用于转换英文单词
中数字的每个数字的C代码#include<stdio.h>
int main(){
int number,i=0,j,digit;
char * word[1000];
printf("Enter any integer: ");
scanf("%d",&number);
while(number){
digit = number %10;
number = number /10;
switch(digit){
case 0: word[i++] = "zero"; break;
case 1: word[i++] = "one"; break;
case 2: word[i++] = "two"; break;
case 3: word[i++] = "three"; break;
case 4: word[i++] = "four"; break;
case 5: word[i++] = "five"; break;
case 6: word[i++] = "six"; break;
case 7: word[i++] = "seven"; break;
case 8: word[i++] = "eight"; break;
case 9: word[i++] = "nine"; break;
}
}
for(j=i-1;j>=0;j--){
printf("%s ",word[j]);
}
return 0;
}
示例输出:
输入任意整数:23451208
二三四有五一二零八。这也是先前提出的问题。请参阅链接Convert digit to text in C
答案 2 :(得分:-2)
没有这样做的功能。你需要手动完成。我给你的代码可以帮助你。
#include<stdio.h>
void check_print(int k);
int main()
{int n,i,m;
printf("Enter any number");
Scanf("%d",&i);
n=i;
while(n!=0)
{
m=n%10;
check_print(m);
n=n/10;
}
}
void check_print(int k)
{
switch(k)
{
case 0:
printf("zero");
break;
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("Three");
break;
case 4:
printf("four");
break;
case 5:
printf("Five");
break;
case 6:
printf("Six");
break;
case 7:
printf("seven");
break;
case 8:
printf("Eight");
break;
case 9:
printf("nine");
break;
}
}