C - Issue converting a user generated number into words

后端 未结 3 2060
予麋鹿
予麋鹿 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.

提交回复
热议问题