Skip to main content
Spelling and grammar
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

Seems to me you overcomplicate the things.

IF you split your number into "begining""beginning" and "last 3 digits" inside your RecursiveCommas you have to call it recusevlyrecursively on the "begining" "beginning" and then print the "last 3 digits". The only difference will be - that 1st chunk could be less than 3 digits and has no comma before it, all other chunks - following comma have to be printed with 0 filling.

Here is thea sample implemantationimplementation of the above approach (template is just to avoid selecting specific integer type - so I'm skipping the checks of the TT intentionally):

#include <iostream>
#include <iomanip>

template<class T>
void RecursiveCommas(std::ostream& os, T n)
{
    T rest = n % 1000; //"last 3 digits"
    n /= 1000;         //"begining"

    if (n > 0) {
        RecursiveCommas(os, n); //printing "begining"

        //and last chunk
        os << ',' << std::setfill('0') << std::setw(3) << rest;
    }
    else
        os << rest; //first chunk of the number
}

int main()
{
    RecursiveCommas(std::cout, 123456789);
    std::cout << '\n';
    RecursiveCommas(std::cout, 1234);
    std::cout << '\n';
    RecursiveCommas(std::cout, 123);
    std::cout << '\n';
    RecursiveCommas(std::cout, 123456789012345);
    std::cout << '\n';

    return 0;
}

Seems to me you overcomplicate the things.

IF you split your number into "begining" and "last 3 digits" inside your RecursiveCommas you have to call it recusevly on the "begining" and then print the "last 3 digits". The only difference will be - that 1st chunk could be less than 3 digits and has no comma before it, all other chunks - following comma have to be printed with 0 filling.

Here is the sample implemantation of the above approach (template is just to avoid selecting specific integer type - so I'm skipping the checks of the T intentionally)

#include <iostream>
#include <iomanip>

template<class T>
void RecursiveCommas(std::ostream& os, T n)
{
    T rest = n % 1000; //"last 3 digits"
    n /= 1000;         //"begining"

    if (n > 0) {
        RecursiveCommas(os, n); //printing "begining"

        //and last chunk
        os << ',' << std::setfill('0') << std::setw(3) << rest;
    }
    else
        os << rest; //first chunk of the number
}

int main()
{
    RecursiveCommas(std::cout, 123456789);
    std::cout << '\n';
    RecursiveCommas(std::cout, 1234);
    std::cout << '\n';
    RecursiveCommas(std::cout, 123);
    std::cout << '\n';
    RecursiveCommas(std::cout, 123456789012345);
    std::cout << '\n';

    return 0;
}

Seems to me you overcomplicate the things.

IF you split your number into "beginning" and "last 3 digits" inside your RecursiveCommas you have to call it recursively on the "beginning" and then print the "last 3 digits". The only difference will be - that 1st chunk could be less than 3 digits and has no comma before it, all other chunks - following comma have to be printed with 0 filling.

Here is a sample implementation of the above approach (template is just to avoid selecting specific integer type - so I'm skipping the checks of the T intentionally):

#include <iostream>
#include <iomanip>

template<class T>
void RecursiveCommas(std::ostream& os, T n)
{
    T rest = n % 1000; //"last 3 digits"
    n /= 1000;         //"begining"

    if (n > 0) {
        RecursiveCommas(os, n); //printing "begining"

        //and last chunk
        os << ',' << std::setfill('0') << std::setw(3) << rest;
    }
    else
        os << rest; //first chunk of the number
}

int main()
{
    RecursiveCommas(std::cout, 123456789);
    std::cout << '\n';
    RecursiveCommas(std::cout, 1234);
    std::cout << '\n';
    RecursiveCommas(std::cout, 123);
    std::cout << '\n';
    RecursiveCommas(std::cout, 123456789012345);
    std::cout << '\n';

    return 0;
}
Source Link

Seems to me you overcomplicate the things.

IF you split your number into "begining" and "last 3 digits" inside your RecursiveCommas you have to call it recusevly on the "begining" and then print the "last 3 digits". The only difference will be - that 1st chunk could be less than 3 digits and has no comma before it, all other chunks - following comma have to be printed with 0 filling.

Here is the sample implemantation of the above approach (template is just to avoid selecting specific integer type - so I'm skipping the checks of the T intentionally)

#include <iostream>
#include <iomanip>

template<class T>
void RecursiveCommas(std::ostream& os, T n)
{
    T rest = n % 1000; //"last 3 digits"
    n /= 1000;         //"begining"

    if (n > 0) {
        RecursiveCommas(os, n); //printing "begining"

        //and last chunk
        os << ',' << std::setfill('0') << std::setw(3) << rest;
    }
    else
        os << rest; //first chunk of the number
}

int main()
{
    RecursiveCommas(std::cout, 123456789);
    std::cout << '\n';
    RecursiveCommas(std::cout, 1234);
    std::cout << '\n';
    RecursiveCommas(std::cout, 123);
    std::cout << '\n';
    RecursiveCommas(std::cout, 123456789012345);
    std::cout << '\n';

    return 0;
}