c++ - 将一个整数拆分成它的数字 C++

标签 c++

我正在尝试自学 C++,但遇到了一些障碍。问题是我需要取一个整数,将其拆分成数字,然后得到数字的总和并显示它们。

例子:

输入号码:123456
整数中的数字:1 2 3 4 5 6
总和:21

我已经全部完成了,但是当我将整数转换为数字时,我无法正确显示它。它以相反的顺序显示。

所以在下面的程序中,我输入 1234,它会输出 4 3 2 1。我知道为什么,我只是不知道如何解决它。

到目前为止,这是我的代码:

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <math.h>

int countDigitsInInteger(int n)
{
    int count =0;
    while(n>0)
    {
       count++;
       n=n/10;
    }
    return count;
}

using namespace std;

int main(int argc, char *argv[])
{  
    int intLength =0;
    int number;
    int digit;      
    int sum = 0;
    string s;    
    cout << "Please enter an integer ";
    cin >>number;
    cout << "Orginal Number = "<<number <<endl;
    //make the number positive
    if (number<0)
    number = -number;    
    intLength = countDigitsInInteger(number);
    //break apart the integer into digits
    while(number>0)
    {                         
        digit = number % 10;
        number = number / 10;        
        cout <<digit << " "; 
        sum = sum+digit; 
    } 
    cout <<endl <<"Sum of the digits is: "<<sum<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

这是我的解决方案

看不到:)

最佳答案

您的问题来自于您正在向后读取数字,因此您需要向后打印它们。 stack将极大地帮助你。

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stack>

int countDigitsInInteger(int n)
{
    int count =0;
    while(n>0)
    {
        count++;
        n=n/10;
    }
    return count;
}

using namespace std;

int main(int argc, char *argv[])
{  
    int intLength =0;
    int number;
    int digit;      
    int sum = 0;
    string s;    
    cout << "Please enter an integer ";
    cin >>number;
    cout << "Orginal Number = "<<number <<endl;
    //make the number positive
    if (number<0)
        number = -number;    

    intLength = countDigitsInInteger(number);
    //break apart the integer into digits

    stack<int> digitstack;
    while(number>0)
    {                         
        digit = number % 10;
        number = number / 10;
        digitstack.push(digit);
        sum = sum+digit; 
    }

    while(digitstack.size() > 0)
    {
        cout << digitstack.top() << " ";
        digitstack.pop();
    }

    cout <<endl <<"Sum of the digits is: "<<sum<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

哦,顺便说一句,保持你的缩进干净。这很重要。

编辑:针对 Steve Townsend 的回应,这种方法不一定矫枉过正,只是与您的方法不同。可以精简代码,使其看起来不那么矫枉过正:

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int getInput(string prompt)
{
    int val;
    cout << prompt;
    cin >> val;
    return val < 0 ? -val : val;
}

int main(int argc, char** argv)
{
    int num = getInput("Enter a number: ");
    cout << "Original Number: " << num << endl;

    stack<int> digits;
    int sum = 0;
    while(num > 0)
    {
        digits.push(num % 10);
        sum += digits.top();
        num = num / 10;
    }

    while(digits.size() > 0)
    {
        cout << digits.top() << " ";
        digits.pop();
    }

    cout << endl << "Sum of digits is " << sum << endl;

    return 0;
}

关于c++ - 将一个整数拆分成它的数字 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4207696/

相关文章:

C++ 用指针反转字符串中单词的顺序

c++ - Google 测试 - 将数组作为 void* 参数进行比较

c++ - 复杂数据类型 vector 的迭代器

子类中的 C++ 收紧函数参数类型

c++ - 许多嵌套回调的优点/缺点?

c++ - 命名管道相对于匿名管道的优势c++

C++继承 - 无法访问的基础?

c++ - OpenGL - 制作跟随相机的点光源

c++ - 逗号作为变量初始化中的分隔符(不是作为运算符)

c++ - 如何在 C++ 中填充张量