在C ++中按顺序打印数字的数字

时间:2014-03-24 00:28:55

标签: c++

您好我正在尝试按顺序打印任意正数的数字,然后添加所有数字。例如:

If a user inputs 123, the program should output
1
2
3
6

我知道如何以相反的顺序打印它,并且还自己添加数字,但如果输入可以是任何大小的整数,我想不出按顺序打印它们的方法。

3 个答案:

答案 0 :(得分:1)

代码

#include <iostream>
#include <string>

int main() {
    std::cout << "Please enter a number: ";

    std::string data;
    std::getline(std::cin, data);

    int total = 0;

    for (char c : data) {
        int current = std::stoi(std::string(1, c));

        std::cout << current << std::endl;
        total += current;
    }

    std::cout << total << std::endl;
}

运行示例

[7:45pm][wlynch@watermelon /tmp] ./foo
Please enter a number: 123
1
2
3
6

答案 1 :(得分:0)

using namespace std;

int print_and_add_digits(int x)
{
    std::string str;
    int count = 0;
    int sum = 0;

    // ignore negative numbers
    if (x < 0)
    {
        return -1;
    }

    // special case for 0. Just print 0 and exit
    if (x == 0)
    {
         cout << 0 << endl;
         return 0;
    }

    while (x > 0)
    {
        int digit = x % 10;
        sum += digit;
        str += (char)('0' + digit); // convert each digit to a char and append to string
        count++;
        x = x / 10;           
    }

    // reverse the string and print each digit on it's own line
    str = std::string(str.end(), str.begin());
    for (int i = 0; i < count; i++)
    {
        cout << str[i] << endl;
    }

    // print and return the sum
    cout << sum << std::endl;
    return sum;    
}

答案 2 :(得分:-1)

我认为没有必要使用class std :: string。您可以通过以下方式执行此操作

#include <iostream>

int main()
{
    while ( true )
    {
        std::cout << "Enter a non-negative number (0 - exit): ";

        unsigned int x = 0;
        std::cin >> x;

        if ( !x ) break;

        unsigned int n = 1;

        const unsigned int base = 10;

        while ( x / n >= base ) n *= base;

        unsigned int total = 0;
        do
        {
            total += x /n;
            std::cout << x / n << ' ';
            x %= n;
        } while ( n /= base );

        std::cout << "\nSum of digits is " << total << std::endl;
    }
}

或者您可以通过以下方式更改输出

#include <iostream>

int main()
{
    while ( true )
    {
        std::cout << "Enter a non-negative number (0 - exit): ";

        unsigned int x = 0;
        std::cin >> x;

        if ( !x ) break;

        unsigned int n = 1;

        const unsigned int base = 10;

        while ( x / n >= base ) n *= base;

        unsigned int total = 0;
        do
        {
            total += x /n;
            std::cout << x / n;
            x %= n;
        } while ( ( n /= base ) && ( std::cout << " + " ) );

        std::cout << " = " << total << std::endl << std::endl;
    }
}

输出

Enter a non-negative number (0 - exit): 123456789
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

Enter a non-negative number (0 - exit): 0

你也可以写一个递归函数。例如

#include <iostream>

unsigned int PrintDigits( unsigned int x, std::ostream &os = std::cout )
{
    const unsigned int base = 10;

    unsigned int digit = x % base;

    unsigned int total = digit + ( ( x /= base ) ? PrintDigits( x ) : 0 );

    os << digit << std::endl;

    return total;
}

int main()
{
    while ( true )
    {
        std::cout << "Enter a non-negative number (0 - exit): ";

        unsigned int x = 0;
        std::cin >> x;

        if ( !x ) break;

        std::cout << PrintDigits( x ) << std::endl << std::endl;    }
    }
}

输出

Enter a non-negative number (0 - exit): 123456789
1
2
3
4
5
6
7
8
9
45

Enter a non-negative number (0 - exit): 0