将整数拆分为其数字c ++ [英] Split an Integer into its digits c++
问题描述
我想自己学习c ++,我打了一个路障。问题是我需要一个整数,将它拆分成数字,并获得数字的和显示它们。
I'm trying to learn c++ on my own and I've hit a bit of a road block. The problem is I need to take an integer,split it into its digits and get the sum of the digits and display them.
示例:
输入数字: 123456
在整数中的数字: 1 2 3 4 5 6
sum: 21
完成,但是当我把整数,成数字,我不能正确显示。
I have it all done, but when I rip the integer, into digits I can't display it correctly. It displays in reverse order.
因此,在下面的程序中输入 1234
,并且输出 4 3 2 1
。我知道为什么,我只是不知道如何解决它。
So in the program below I enter 1234
and it spits out 4 3 2 1
. I know why, I just don't know how to fix it.
以下是我的代码:
#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;
}
这里是我的解决方案
无法看到:)
推荐答案
您的问题来自于您正在向后读取数字,需要向后打印出来。 堆叠将大有助益。
Your problem comes from the fact that you are reading the digits backwards, thus you need to print them out backwards. A stack will help you tremendously.
#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;
}
哦,和BTW,保持你的缩进干净。它的重要性。
Oh, and BTW, keep your indentation clean. Its important.
编辑:为了回应史蒂夫·汤森,这种方法不一定是过度的,它只是不同于你的。代码可以细化,以使它看起来不像overkill:
In response to Steve Townsend, this method is not necessarily overkill, it is just different from yours. The code can be slimmed down so that it seems less like overkill:
#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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!