5

我正在编写一个代码来显示 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;
}
4

5 回答 5

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;
}
于 2012-11-15T16:20:34.160 回答
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;
        }
}
于 2012-11-15T15:35:04.517 回答
1

这是很多年前的大学练习!

从 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;
}
于 2012-11-15T15:47:57.820 回答
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);
}
于 2012-11-15T16:15:27.493 回答
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 []. 然后你只需要遍历那个数组。

于 2012-11-15T15:27:05.067 回答