我正在编写一个代码来显示 C 语言中任何正整数的数字。例如,整数 345 将显示为三四五。
我编写的代码适用于所有数字都大于 0 的整数。但某些整数(如 10、304、0 等)无法正确显示。为了挑战,不允许使用递归和数组。只允许循环和 if 构造。有什么建议么?
#include<stdio.h>
int main(void)
{
int num, num_copy, accum = 1;
printf("Enter an integer ");
scanf("%i",&num);
num_copy = num;
while (num > 0){
while (num > 9){
num = num / 10;
accum *= 10;
}
switch (num){
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;
}
num_copy = num_copy - (num*accum);
num = num_copy;
accum = 1;
}
return 0;
}