将整数拆分为数字c ++

nku*_*eck 2 c++

我正在尝试自己学习c ++,而且我遇到了一些障碍.问题是我需要取一个整数,将其拆分为数字并获得数字的总和并显示它们.

例:

输入数字:123456
整数中的数字:1 2 3 4 5 6
sum: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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的解决方案

看不到:)

riw*_*alk 8

你的问题来自你正在向后读数字的事实,因此你需要向后打印它们.一个堆栈将极大地帮助你.

#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;
}
Run Code Online (Sandbox Code Playgroud)

哦,顺便说一句,保持你的缩进清洁.这一点很重要.

编辑:回应史蒂夫汤森,这种方法不一定是矫枉过正,它与你的不同.代码可以缩小,以便它看起来不像矫枉过正:

#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;
}
Run Code Online (Sandbox Code Playgroud)