1

递归地以英文形式一次打印一个整数

如何在不将其转换为字符串/字符数组的情况下获取数字的数字?

这适用于 C 语言,而不是 C++ 语言。我对 C 的了解非常有限,因为我在入门级课程,而且我们刚刚过了中期。尽量保持简单,因为我不能包含我们在课堂上没有涉及的关键字或运算符。我认为这没有必要,因为我认为需要帮助的只是我的逻辑,而不是我的代码。

参考了上述两个示例来编写我的课程代码后,我很困惑如何完成我的最后一个小难题。我在这里找到了几个似乎相关的问题答案,但是他们使用代码来解决我不知道的问题。希望有人可以在这里帮助我理解我的逻辑。

我的任务目标是:

取一个用户定义的整数,并以英文显示数字。例如:

请输入整数:123
您已输入:一二三

然后,我需要将数字的总和相加(如果数字<10,则以英文显示)。在这种情况下:

各个数字的总和是:六

最后,我需要使用 2 个小数位对数字进行平均。在这种情况下:

平均值为:2.00

我已经完成了所有这些。除了:我的第一步是向后列出数字!它读取 10 位、100 位、1000 位等。例如:

请输入整数:123
您已输入:三二一

我对这部分赋值的条件是,我只能使用一个 switch 语句,并且我必须使用一个 switch 语句(这意味着需要一个循环(我选择了 do))。我也可能不使用数组。但最后,也是最重要的一点,我可能不会反转输入数字(这是该作业第一个版本的解决方案)。如果我能做到这一点,我就不会在这里。

这是相关的代码摘录。

#include <stdio.h>

int main(void)

{

    int userinput, digit

    printf("Please input a number:");
    scanf("%d", &userinput);

    printf("You have entered: ");

    if (userinput < 0)
    {
            printf("Negative ");
            userinput = -userinput;
    }

    do
    {
            digit = userinput%10;
            switch (digit)
            {
                    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;
                    }
                    default:
                    {
                            break;
                    }

            }

            userinput = userinput/10;

    } while (userinput > 0);

    printf("\n");
4

3 回答 3

1

如果不能使用数组,请使用递归:

void print_textual(int n)
{
    if (n > 9) {
        print_textual(n / 10);
    }

    switch (n % 10) {
    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;
    }
}

顺便说一句,如果您至少可以将数组用于数字名称,那真的会好得多:

void print_textual(int n)
{
    if (n > 9) {
        print_textual(n / 10);
    }

    static const char *names[] = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine"
    };

    printf("%s ", names[n % 10]);
}
于 2013-11-09T17:56:06.147 回答
0

为避免使用递归,请形成一个 10 的幂乘数,缩放为n. 然后使用这个乘数来确定从最高有效到最低有效顺序的数字。

使用相同digits_in_english()的方法打印数字总和。

void digits_in_english(const char *prompt, int n, int *Sum, int *Count) {
  fputs(prompt, stdout);
  *Sum = 0;
  *Count = 0;
  if (n < 0) {
    printf(" Negative");
    n = -n;
  }
  int m = n;
  int pow10 = 1;
  while (m > 9) {
    m /= 10;
    pow10 *= 10;
  }
  do {
    static const char *Edigit[] = { "Zero", "One", "Two", "Three", "Four",
       "Five", "Six", "Seven", "Eight", "Nine" };
    int digit = n / pow10;
    *Sum += digit;
    (*Count)++;

    // OP knows how to put a switch statement here instead of printf()
    printf(" %s", Edigit[digit]);

    n -= digit * pow10;
    pow10 /= 10;
  } while (pow10 > 0);
  fputs("\n", stdout);
}

void Etest(int n) {
  int Count, Sum;

  // Change this to a printf() and scanf()
  printf("Please enter an integer: %d\n", n);

  digits_in_english("You have entered:", n, &Sum, &Count);
  double Average = (double) Sum / Count;

  // Do no care about the resultant Sum, Count
  digits_in_english("The sum of the individual digits is:", Sum, &Sum, &Count);

  printf("The average is: %.2f\n\n", Average);
}

样本

Please enter an integer: -2147483647
You have entered: Negative Two One Four Seven Four Eight Three Six Four Seven
The sum of the individual digits is: Four Six
The average is: 4.60

不适用于 INT_MIN。以便携式方式这样做有点棘手。

看来 OP 不允许使用数组。希望不包括字符串。

于 2013-11-09T21:38:02.023 回答
-1

所以,经过很多的磕磕绊绊,我设法让我的代码按照预期的方式工作,只使用了我被允许使用的知识。这是成品(除非有人看到任何巨大的错误):

    //initializing variables, of course
    int userinput, number1, number2, numbersum;
    int div = 1;
    float number3;

    //displaying instructions to the user
    printf("Please input a number: ");
    scanf("%d", &userinput);

    printf("You have entered: ");

    if (userinput < 0)
    {
            printf("Negative ");
            userinput = -userinput;
    }

    //the variables number1-3 are for the data analysis at the end
    //I am preserving the original input in them so I can mutilate it in the following step
    number1 = userinput;
    number2 = userinput;

    while (div <= userinput)
    {
            div = div*10;
    }

    do
    {
            if (userinput != 0)
            {
                    div = div/10;

                    switch (userinput/div)
                    {
                            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;
                            }
                            default:
                            {
                                    break;
                            }

                    }

                    userinput = userinput%div;

            }

            else
            {
                    printf("Zero");
            }

    } while (userinput > 0);

    //line break to make it look pretty
    printf("\n");

    //boring math to determine the sum of the digits
    //assuming all are positive due to know contrary instructions
    //set equal to zero since this variable refers to itself in the following function
    numbersum = 0;

    while (number1 > 0)
    {
            numbersum = numbersum + (number1 % 10);
            number1 = number1 / 10;
    }

    //nested switch in if statement to print english if digits less than or equal to 10
    if (numbersum <= 10)
    {
            switch (numbersum)
            {
                    case 0:
                    {
                            printf("The sum of the individual integers is: Zero");
                            break;
                    }
                    case 1:
                    {
                            printf("The sum of the individual integers is: One");
                            break;
                    }
                    case 2:
                    {
                            printf("The sum of the individual integers is: Two");
                            break;
                    }
                    case 3:
                    {
                            printf("The sum of the individual integers is: Three");
                            break;
                    }
                    case 4:
                    {
                            printf("The sum of the individual integers is: Four");
                                    break;
                    }
                    case 5:
                            {
                            printf("The sum of the individual integers is: Five");
                            break;
                    }
                    case 6:
                    {
                            printf("The sum of the individual integers is: Six");
                            break;
                    }
                    case 7:
                    {
                            printf("The sum of the individual integers is: Seven");
                            break;
                    }
                    case 8:
                    {
                            printf("The sum of the individual integers is: Eight");
                            break;
                    }
                    case 9:
                    {
                            printf("The sum of the individual integers is: Nine");
                            break;
                    }
                    case 10:
                    {
                            printf("The sum of the individual integers is: Ten");
                    }
                    default:
                    {
                            break;
                    }

            }

            printf("\n");
    }

    //else if greater than 10, just print the decimal number
    else
    {
            printf("The sum of the individual digits in the integer is: %d\n", numbersum);
    }

    if (numbersum == 0)
    {
            printf("The average is of zero is not a number.\n");
    }

    else
    {

            //initializing a variable here because it's totally irrelevant to the above functions
            //and this feels cleaner because of it. I'm not sure if this is bad etiquette
            int i;

            //picks out the number of digits in the input and effectively sets i to that number
            for (i = 0; number2 > 0; i++)
            {
                    number2 = number2/10;
            }

            //this is necessary for turning number3 into an actual floating point, not an int stored as float
            number3 = numbersum;

            //math to determine average (sum of digits divided by number of digits)
            number3 = number3 / i;

            printf("The average is: %.2f\n", number3);

    }

    return 0;

它很大,而且可能有点草率(因为我很新),但它确实有效,这对我来说真的很重要。

谢谢你们的帮助,伙计们。

于 2013-11-14T10:46:21.827 回答