Printing an integer each digit at a time in english form recursively

时间秒杀一切 提交于 2020-01-06 15:42:09

问题


I've been given an assignment where we must print out an integer digit by digit in english form using a recursive method. E.G. 534 prints out "five three four".

This is what I've got:

int englishInt(int num) {
    if(num < 10) {
        switch(num) {
            case 0: cout << "zero ";
            case 1: cout << "one ";
            case 2: cout << "two ";
            case 3: cout << "three ";
            case 4: cout << "four ";
            case 5: cout << "five ";
            case 6: cout << "six ";
            case 7: cout << "seven ";
            case 8: cout << "eight ";
            case 9: cout << "nine ";
        }
    } else
        return englishInt(num / 10);
}

For some reason it's printing out the lowest digit to the highest digit in English...Shouldn't this keep returning until it reaches the first digit then print it out, and then print out each digit up as the stack unwinds?


回答1:


This happens because your method does not do anything in the invocations where you recurse down. You should do a print in each invocation, but recurse down only when the num is not zero.

The algorithm (in pseudocode) should be as follows:

englishInt(int num) {
    if (num != 0) {
        englishInt(num/10)
    }
    cout << english letter for (num % 10)
}

You need a special case for num == 0, so that calling englishInt(0) would produce some output.




回答2:


Think about it with an example, say 537.

englishInt(537) -- prints nothing, calls
    englishInt(53) -- prints nothing, calls
        englishInt(5) -- prints "five six seven eight nine"

The reasons being:

  1. You don't have any break statements in your cases.
  2. You don't print anything out if num >= 10.



回答3:


First, the minimal change I see to fixing your code.

  1. Add a break after each output in your switch.
  2. Recurse prior to the output, and always output after.

Therefore,

void englishInt(int num)
{
    if (num >=10)
        englishInt(num/10);

    switch(num % 10)
    {
        case 0: cout << "zero "; break;
        case 1: cout << "one "; break;
        case 2: cout << "two "; break;
        case 3: cout << "three "; break;
        case 4: cout << "four "; break;
        case 5: cout << "five "; break;
        case 6: cout << "six "; break;
        case 7: cout << "seven "; break;
        case 8: cout << "eight "; break;
        case 9: cout << "nine "; break;
    }
}

Ditch The Switch

I'm not following why you have a switch statement in the first place. If you look at your switch you're always evaluating a number between 0..9. So why not use that number for a simple index into an array of ten strings:

#include <iostream>
using namespace std;

void print_num(unsigned int num)
{
    static const char *strs[] =
    {
        "zero", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine"
    };

    if (num >= 10)
        print_num(num/10);
    cout << strs[num % 10] << ' ';
}

int main(int argc, char *argv[])
{
    print_num(100); cout << endl;
    print_num(12345);  cout << endl;
    print_num(3);  cout << endl;
    print_num(1024*1024*1024); cout << endl;
    return 0;
}

Output

one zero zero 
one two three four five 
three 
one zero seven three seven four one eight two four 



回答4:


There are a couple of problems with your code.

Firstly, your switch statement is broken: you forgot to put a break statement after each case, so at the end of recursion your program will print out the most significant digit and then iterate through the greater digits up to nine.

Fix it as follows:

   switch(num) {
        case 0: cout << "zero "; break;
        case 1: cout << "one "; break;
        case 2: cout << "two "; break; 
        case 3: cout << "three "; break;
        case 4: cout << "four "; break;
        case 5: cout << "five "; break; 
        case 6: cout << "six "; break;
        case 7: cout << "seven "; break; 
        case 8: cout << "eight "; break;
        case 9: cout << "nine "; break;
    }

Secondly, your program will have an output only for the most significant digit. You don't print anything for the intermediate steps. It's not a problem with your code, but with your algorithm, so you'll have to fix that on your own.




回答5:


A wild method to do

int main()
{
   int n;
   char s[32];
   char word[10][10]{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

   cout << "enter a number" << endl;
   cin >> n;

   sprintf(s,"%d", n );

   for( int i = 0; s[i] != '\0'; i++)
   {
       cout << word[s[i]-'0'];
   }

   return 0;
}


来源:https://stackoverflow.com/questions/15060350/printing-an-integer-each-digit-at-a-time-in-english-form-recursively

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!