我正在编写一个代码来显示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;
}
答案 0 :(得分:2)
如果您编写递归函数,它将帮助您避免用于以相反顺序打印的循环:
#include<stdio.h>
void print_num(int num)
{
if(num == 0) return;
print_num(num/10);
switch (num%10){
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;
case (0):
printf("zero ");
break;
}
}
int main(void)
{
int num, num_copy, accum = 1;
printf("Enter an integer ");
scanf("%i",&num);
print_num(num);
return 0;
}
使用数组存储数字的非递归解决方案:
void print_num(int num)
{
int i=0,j;
int arr[256];
while(num)
{
arr[i++]=num%10;
num/=10;
}
for(j=i-1; j>=0;j--)
{
switch (arr[j]){
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;
case (0):
printf("zero ");
break;
}
}
答案 1 :(得分:2)
喔!这听起来很有趣!没有数组,没有递归吗?
因为我们不能使用递归,所以我们需要向后构建数字:
#include<stdio.h>
int main(void)
{
int num, backwards = 0, digit, backupzeros = 0;
printf("Enter an integer ");
scanf("%i",&num); // get the number
while(num > 0){
digit = num % 10; // pry off the last digit
num /= 10; // take off the digit
if((backwards == 0) && (digit == 0)) // If it's a number that ends in 0
backupzeros++; // save it, we'll use that later
backwards = (backwards * 10) + digit; // glue it on backwards
}
// Now we have the number reversed. Next we need to print the digits
while (backwards > 0){
digit = backwards % 10;
backwards /= 10;
switch (digit){
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;
default:
printf("zero ");
break;
}
}
while(backupzeros > 0) {
printf("zero ");
backupzeros--;
}
return 0;
}
答案 2 :(得分:1)
这是很多年前的Univ练习!!!
从1到999的整数
#define MAX_BUF 100
int main(){
int num, i=0, j, digit;
char *buf[MAX_BUF];
printf("Integer: ");
scanf("%d",&num);
while(num){
digit = num %10;
num = num /10;
switch(digit){
case 0: buf[i++] = "zero"; break;
case 1: buf[i++] = "one"; break;
case 2: buf[i++] = "two"; break;
case 3: buf[i++] = "three"; break;
case 4: buf[i++] = "four"; break;
case 5: buf[i++] = "five"; break;
case 6: buf[i++] = "six"; break;
case 7: buf[i++] = "seven"; break;
case 8: buf[i++] = "eight"; break;
case 9: buf[i++] = "nine"; break;
}
}
for(j = i-1; j >= 0; j--){
printf("%s-", buf[j]);
}
return 0;
}
答案 3 :(得分:0)
“(..)但是某些整数如10,304,0”,
所有这些数字有什么共同之处? “零”就是答案。
你必须在你的开关中输入处理零点的条件。
您可以这样做:
#include<stdio.h>
int main(void)
{
int num, num_copy, accum = 1;
printf("Enter an integer ");
scanf("%i",&num);
char str[15];
sprintf(str, "%d", num);
for(num = 0; str[i] != '\O'; num++)
{
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;
case (0):
printf("zero ");
break;
}
}
return 0;
}
您可以使用sprintf()
将整数转换为char []
。然后你只需要迭代那个数组。
答案 4 :(得分:0)
未经测试...
char *digitNames[10] = {
"zero",
"one",
... // Too lazy to fill in the rest..
"nine"
}
void displayNumber(uint32_t number) {
int totalDigits = 0;
int scale = 10;
uint32_t divisor = 1000000000;
uint32_t digit;
do {
scale--;
digit = number / divisor;
number -= digit * divisor;
divisor /= 10;
if(scale == 0) {
fputs(stdout, digitNames[digit]);
} else if( (digit != 0) || (totalDigits != 0) )
printf("%s ", digitNames[digit]);
totalDigits++;
}
} while(scale != 0);
}