C. 我的数字以相反的顺序打印(英文)?我怎样才能解决这个问题?

C. My digits are printing (in English) in reverse order? How can I fix this?

请多多帮助!我正在完成书中的练习,'Programming in C'。

我必须编写一个程序,接受一个整数,然后提取并用英文显示整数的每个数字。

所以如果我输入 1234,它应该打印回 'one two three four'。

由于这个练习接近本书的开头,它还没有教授数组、函数、指针或字符串。我认为这意味着我不允许使用它们中的任何一个。所以我必须用非常有限的选项以某种方式解决它。

我写的内容或多或少有效,但是,数字以相反的顺序打印回来。我真的很难找到一个替代方案,并且一直在研究过去几个人的代码。

我意识到几年前还有另一个非常相似的问题,但我在这个问题上能做的更有限,更不用说,his/her 问题要复杂得多.

如果您能看一看并提供一些建议,我将不胜感激。

#include <cs50.h>
#include <stdio.h>

int main (void)
{
    int digit;

    //Accept integer
    printf("Choose a number.\n");

    int num = GetInt();

     // adding minus to the beginning if int is negative
    if (num < 0) 
    {
        num = -num;
        printf("minus ");
    }

    // isolate each digit from integer and then print in english

    do
    {
        digit = num % 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;
            case 10:
                printf("Ten ");
                break;
            default:
                break;
        }

        num /= 10; 

    } while(num != 0);

    printf("\n");
}

digit = num % 10; 后跟 num /= 10; 将以相反的顺序获取每个数字。

因为digit = num % 10; return是整数的最后一位。

例如,如果您的号码是 1234,那么它将 return 值 4

因此,它正在反向打印数字。

您有:

    digit = num % 10;

作为 do 循环下的第一条语句。

num等于1234时,digit的值将是4。因此,您最终会先打印 four

如果使用递归函数,打印起来会更容易one two three four

这是我的建议:

void printNumber(int num, int level)
{
   // Break the recursion.
   if ( num == 0 )
   {
      if (level == 0)
      {
         // Make sure to print Zero when the original number is 0.
         printf("Zero ");
      }
      return;
   }

   // Recursive call to print the most significant digit first.
   printNumber (num/10, level+1); 

   int digit = num % 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;
   }
}

int main (void)
{
    int digit;

    //Accept integer
    printf("Choose a number.\n");

    int num = GetInt();

     // adding minus to the beginning if int is negative
    if (num < 0) 
    {
        num = -num;
        printf("minus ");
    }

    // isolate each digit from integer and then print in english
    printNumber(num, 0);

    printf("\n");
}

更新

如果您不能使用递归函数,则需要使用与问题中不同的策略。这是一个将数字存储在数组中并按正确顺序打印的方法。

int main (void)
{
   int digits[20]; // Make it large enough
   int count = 0;

   int digit;

   //Accept integer
   printf("Choose a number.\n");

   int num = GetInt();

   // adding minus to the beginning if int is negative
   if (num < 0) 
   {
      num = -num;
      printf("minus ");
   }

   do
   {
      digit = num % 10;
      digits[count] = digit;
      ++count;
      num /= 10;
   } while ( num > 0 );

   for ( int i = count-1; i >= 0; --i )
   {
      digit = digits[i];
      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;
      }
   }

   printf("\n");
}

循环 1: 1234 % 10 = 4

所以它首先打印 4。够清楚了吧?

假设你不能使用递归,因为你不能定义你自己的函数,你需要一个初始循环来找到最左边的数字位置。

以下代码使用 place 变量将 10 提升到 'number of digits minus 1',将零视为一位数。主循环将数字(的剩余部分)除以 place,打印出该数字,然后以 place 为模减少数字,并将 place 除以 10 进行下一次迭代,直到所有数字已打印(当 place 为零时)。

#include <cs50.h>
#include <stdio.h>

int main (void)
{
    int digit;

    //Accept integer
    printf("Choose a number.\n");

    int num = GetInt();

    // adding minus to the beginning if int is negative
    if (num < 0) 
    {
        num = -num;
        printf("minus ");
    }

    // get 10 to the power of 'number of digits minus 1'
    int place;

    for (place = 1; place <= num / 10; place *= 10)
        ;

    // isolate each digit from integer and then print in english

    do
    {
        digit = num / place;

        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;
        }

        num %= place;
        place /= 10; 

    } while (place != 0);

    printf("\n");
}

先把数字倒过来,然后同样的代码就可以了。

我建议,正如 Ian 上面所建议的那样,通过使用 10 的倍数从左到右获取数字来解决问题。

然而,还有一个使用递归的有趣解决方案:

/* program to convert a number to English words using recursion

   the digits are temporarily stored on the stack instead of in an array

   e.g. number = 123
   push 3, push 2, print 1, pop & print 2, pop & print 3
*/

#include <stdio.h>

void convertToWords (int num)
{
    // if more than one digit remains then make a recursive call
    if ( num > 9 )
        convertToWords (num / 10);

    switch (num % 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;
        default:
            printf ("error ");
            break;
    }

}

int main (void)
{
    int number;

    printf ("\nenter a number: ");
    scanf ("%i", &number);

    if ( number < 0 ) {
        printf ("negative ");
        number = -number;
    }

    convertToWords (number);

    printf ("\n");

    return 0;
}