C - Issue converting a user generated number into words

后端 未结 3 2020
予麋鹿
予麋鹿 2021-01-15 16:56

So I\'ve been working my way through Kochan\'s Programming in C and I\'ve hit a snag on one of the questions which reads as follows:

\"Write a program that takes an

相关标签:
3条回答
  • 2021-01-15 17:34

    Consider what the primary problem is you are dealing with, you need to process the left most digit first, then the next to the right, then the next. But the math of using modulus and division goes from right to left. So what you need is some way to either save the math processing and reverse, or have the output be delayed. Two options are available.

    For an iterative approach you could utilize a FIFO queue type approach that holds the results of each digit and then prints out the queue. Could be as simple as an array with indexing:

    int main(void) {
      int x, i;
      int result[32];     //arbitrary size
      int index = 0;
    
      printf("Enter the number you'd like converted to words\n");
      scanf("%i", &x);
    
     do {
        results[index++] = x % 10;
        x = x / 10;
      } while( index < 32 && x != 0 );
    
      //now print in reverse order
      for(i = index-1; i >= 0; i--) {
         switch (results[i]) {
           case 0:
             printf("zero\t");
             break;
    
           case 1:
             printf("one\t");
             break;  
    
           case 2:
             printf("two\t");
             break;
    
           case 3:
             printf("three\t");
             break;
    
           case 4:
             printf("four\t");
             break;
    
           case 5:
             printf("five\t");
             break;
    
           case 6:
             printf("six\t");
             break;
    
           case 7:
             printf("seven\t");
             break;
    
           case 8:
             printf("eight\t");
             break;
    
           case 9:
             printf("nine\t");
             break;
    
           default:
              break;
        }       
      }
    }
    

    There is second approach that works which is recursive. Here you delay the printing of the output until you reach the left most digit. The built in stack is used for by the recursive calls.

    void printNumbers(int x);
    
    int main(void) {
       int x;
    
       printf("Enter the number you'd like converted to words\n");
       scanf("%i", &x);
       printNumbers(x);
    }
    
    void printNumbers(int v) {
        if( v > 9 ) {
            printNumbers( v / 10 );
        }
        switch (v%10) {
          case 0:
            printf("zero\t");
            break;
    
          case 1:
            printf("one\t");
            break;
    
          case 2:
            printf("two\t");
            break;
    
          case 3:
            printf("three\t");
            break;
    
          case 4:
            printf("four\t");
            break;
    
          case 5:
            printf("five\t");
            break;
    
          case 6:
            printf("six\t");
            break;
    
          case 7:
            printf("seven\t");
            break;
    
          case 8:
            printf("eight\t");
            break;
    
          case 9:
            printf("nine\t");
            break;
    
          default:
            break;
        }
     }
    

    Both approaches will solve the problem, but not if the input is a negative number.

    0 讨论(0)
  • 2021-01-15 17:36

    In your reverseNumber function you have not initialized Rev. Make Rev=0

    int reverseNumber (int y)
    {
        int cnt, Rev=0;
        cnt = digitCount(y);  //returns number of digits
    
        printf("Digit count %d\n", cnt);
        while (cnt != 0) {
            Rev = Rev * 10 + y % 10;
            y = y / 10;
            cnt--;
        }
    
        return Rev;
    }
    

    In main in the do while loop use a temporary variable since you are overwriting numberValue with numberValue % 10. But the most ironic part in your program (where you complicated everything for yourself) is that there is no need to reverse the number at all. See the code here

    1. In the way user entered - http://ideone.com/pORaP2
    2. In reverse order - http://ideone.com/5GS8al

    When you find modulo you get the number in the reverse order itself. Suppose you entered 234

    1. First step 234%10 gives 4 prints four. And then makes 234 to 23

    2. Second step 23%10 gives 3 prints three. And then makes 23 to 2

    and then finally prints two.

    0 讨论(0)
  • 2021-01-15 17:36

    My simple answer:

    void printNum(int x)
    {
        static const char * const num[] = {
            "zero ", "one ", "two "  , "three ", "four ",
            "five ", "six ", "seven ", "eight ", "nine "
        };
    
        if (x < 10) {
            printf(num[x]);
            return;
        }
        printNum(x / 10);
        printNum(x % 10);
    }
    
    0 讨论(0)
提交回复
热议问题