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

Print the digits of a number in sequential order in C++

本文关键字:数字 打印 顺序 c++      更新时间:2023-10-16

嗨,我想做一个函数,打印任何正数的数字顺序,然后添加所有的数字。例如:

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

我知道如何按反向顺序打印,也知道如何将数字相加,但如果输入可以是任意大小的整数,我想不出一种按顺序打印的方法。

代码

#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
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;    
}

我认为没有任何必要使用类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