将整数拆分为其数字c++; 我试图自己学习C++,我碰到了一个路障。问题是,我需要取一个整数,将其拆分为数字,得到数字的和,然后显示它们
例如: 输入号码:将整数拆分为其数字c++; 我试图自己学习C++,我碰到了一个路障。问题是,我需要取一个整数,将其拆分为数字,得到数字的和,然后显示它们,c++,C++,例如: 输入号码:123456 整数中的数字:123456 总和:21 我已经全部完成了,但是当我将整数撕成数字时,我无法正确显示它。它以相反的顺序显示 因此,在下面的程序中,我输入1234,它会吐出43221。我知道为什么,我只是不知道如何修复它 以下是我目前的代码: #include "stdafx.h" #include <cstdlib> #include <iostream> #include <math.h> int countDigitsInI
123456
整数中的数字:
123456
总和:
21
我已经全部完成了,但是当我将整数撕成数字时,我无法正确显示它。它以相反的顺序显示
因此,在下面的程序中,我输入1234
,它会吐出43221
。我知道为什么,我只是不知道如何修复它
以下是我目前的代码:
#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;
}
#包括“stdafx.h”
#包括
#包括
#包括
int countDigitsInInteger(int n)
{
整数计数=0;
而(n>0)
{
计数++;
n=n/10;
}
返回计数;
}
使用名称空间std;
int main(int argc,char*argv[])
{
int长度=0;
整数;
整数位数;
整数和=0;
字符串s;
数量;
cout使用std::stack存储单独的数字,然后打印出stack的内容
这里有一篇好文章:)
下面是如何在C++中使用堆栈:您必须存储它们并以相反的顺序打印出来,或者执行一些递归并在递归调用后打印出来
int printdigits(int number)
{
if (number == 0)
return 0;
int digit = number % 10;
int sum = printdigits(number / 10);
cout <<digit << " ";
return sum+digit;
}
int打印数字(int编号)
{
如果(数字==0)
返回0;
整数位数=数字%10;
整数和=打印数字(数字/10);
你的缩进很可怕。也就是说,你的数字以相反的顺序出现的原因很简单:你以相反的顺序得到数字。想想看:你把数字的余数除以10,然后打印余数。然后你把数字除以10…依此类推。你应该把数字存储在n数组,然后按相反顺序打印数组
并学习缩进。将数字推到堆栈上
在你得到所有数字后
sum = 0 ;
while( stack not empty ) {
pop the stack to get a digit
sum += digit
display digit
}
display sum
你说,你需要一个堆栈吗?使用STL的堆栈:std::stack
作为一个和并不真正影响顺序,但是你使用的逻辑将其拆分
从最低有效位到最高有效位(这是最简单的方法)。您的问题来自于您正在反向读取数字,因此需要反向打印。A将极大地帮助您
#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;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
int countDigitsInInteger(int n)
{
整数计数=0;
而(n>0)
{
计数++;
n=n/10;
}
返回计数;
}
使用名称空间std;
int main(int argc,char*argv[])
{
int长度=0;
整数;
整数位数;
整数和=0;
字符串s;
数量;
库特
此操作将返回最右边的数字
因此,当您尝试使用输入“1357”启动应用程序时,在第一次运行while循环时,它将返回7。接下来它将返回5,依此类推
如果您只需要单个数字的总和,那么获取它们(并打印出来)的顺序将不会影响最终答案。如果您仍然希望它们以正确的顺序打印,则可以将数字存储在数组(或向量或堆栈,如其他人所述)中,反转内容,然后在结构中循环,以打印出其内容。堆栈和递归对于这个问题来说太过分了。只需将每个数字存储到a中,然后在输出之前将其还原。您需要了解如何使用字符串成员调用反转,以使其工作。可用于输出每个元素线的长度
为了获得额外的积分(由于简洁性和表达性),请将数字直接插入,并将其用作可逆字符串的基础
我的stringstream
版本的代码有5行。逻辑是:
- 声明字符串流
- 用值声明int
- 将int输出到stringstream
- 从stringstream创建字符串
- 使用每个_
的逐位输出结果
您可以使用字符串上的数字求和,前提是您考虑到int('1')!=1
。这是额外的两行,用于求和数字并输出结果
重点并不是说通过堆栈或递归来实现这一点是不好的,而是一旦你更加熟悉STL,通常会有比显而易见的更优雅的方法来完成工作。使用堆栈、递归和你能想到的任何其他方法来实现这一点,会使简单的家庭作业变成一种很棒的现实学习体验
下面是累加
代码,用于对由十进制数字组成的字符串的成员求和,例如:
#include <string>
#include <numeric>
std::string intString("654321");
int sum = accumulate(intString.begin(), intString.end(), 0) -
(intString.size() * int('0'));
#包括
#包括
std::string intString(“654321”);
int sum=accumulate(intString.begin(),intString.end(),0)-
(intString.size()*int('0');
编辑:以下是完整的代码,用于比较:
ostringstream intStream;
int value(123456);
intStream << value;
string intString(intStream.str());
for_each(intString.begin(), intString.end(), [] (char c) { cout << c << endl; });
int sum = accumulate(intString.begin(), intString.end(), 0) -
(intString.size() * int('0'));
cout << "Sum is " << sum << endl;
ostringstream intStream;
整数值(123456);
intStream让我们不要忘记stringstream方法,我也觉得它很优雅
#include <iostream>
#include <sstream>
int main()
{
int num = 123456789;
std::cout << "Number: " << num << std::endl;
std::stringstream tmp_stream;
tmp_stream << num;
std::cout << "As string: " << tmp_stream.str() << std::endl;
std::cout << "Total digits: " << tmp_stream.str().size() << std::endl;
int i;
for (i = 0; i < tmp_stream.str().size(); i++)
{
std::cout << "Digit [" << i << "] is: " << tmp_stream.str().at(i) << std::endl;
}
return 0;
}
我会真诚地举一个例子,你不会复制它,如果这是一个家庭作业,把它作为一个学习的机会
// reference: www.cplusplus.com
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
// What does this do?
template <typename T>
struct gen
{
gen(T start) : _num(start) {}
// Do we need these? But why have I commented them out?
//gen(gen const& copy) : _num(copy._num) {}
//gen& operator=(gen const& copy) { _num = copy._num; }
//~gen() {}
// Why do we do this?
T operator()() { T digit = _num % 10; _num /= 10; return digit; }
T _num;
};
// How is this different to the above?
template <typename T>
bool check_non_zero (T i)
{
return i != 0;
}
// And this? what's going on here with the parameter v?
template <typename T, int _size>
T sum_of_digits(T value, std::vector<T>& v)
{
// Why would we do this?
if (value == 0)
{
v.push_back(0);
return 0;
}
// What is the purpose of this?
v.resize(_size);
// What is this called?
gen<T> gen_v(value);
// generate_n? What is this beast? what does v.begin() return? where did _size come from?
generate_n(v.begin(), _size, gen_v);
// reverse? what does this do?
reverse(v.begin(), v.end());
// erase? find_if? what do they do? what the heck is check_non_zero<T>?
v.erase(v.begin(), find_if(v.begin(), v.end(), check_non_zero<T>));
// what the heck is accumulate?
return accumulate(v.begin(), v.end(), 0);
}
int main()
{
// What does this do?
vector<int> v;
// What is this peculiar syntax? NOTE: 10 here is because the largest possible number of int has 10 digits
int sum = sum_of_digits<int, 10>(123, v);
cout << "digits: ";
// what does copy do? and what the heck is ostream_iterator?
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "sum: " << sum << endl;
// other things to consider, what happens if the number is negative?
return 0;
}
//参考:www.cpluplus.com
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//这有什么用?
模板
结构生成
{
gen(T start):\u num(start){
//我们需要这些吗?但是为什么我要把它们注释掉呢?
//gen(gen const©):\u num(copy.\u num){
//gen&operator=(gen const©){u num=copy.\u num;}
//~gen(){}
//我们为什么要这样做?
T运算符(){T digit=_num%10;_num/=10;返回数字;}
T_num;
};
//这与上面的有什么不同?
模板
布尔检查非零(TI)
{
返回i!=0;
}
//这个呢?参数v是怎么回事?
模板
T位之和(T值,标准::向量&v)
{
//我们为什么要这样做
#include <iostream>
#include <sstream>
int main()
{
int num = 123456789;
std::cout << "Number: " << num << std::endl;
std::stringstream tmp_stream;
tmp_stream << num;
std::cout << "As string: " << tmp_stream.str() << std::endl;
std::cout << "Total digits: " << tmp_stream.str().size() << std::endl;
int i;
for (i = 0; i < tmp_stream.str().size(); i++)
{
std::cout << "Digit [" << i << "] is: " << tmp_stream.str().at(i) << std::endl;
}
return 0;
}
Number: 123456789
As string: 123456789
Total digits: 9
Digit [0] is: 1
Digit [1] is: 2
Digit [2] is: 3
Digit [3] is: 4
Digit [4] is: 5
Digit [5] is: 6
Digit [6] is: 7
Digit [7] is: 8
Digit [8] is: 9
// reference: www.cplusplus.com
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
// What does this do?
template <typename T>
struct gen
{
gen(T start) : _num(start) {}
// Do we need these? But why have I commented them out?
//gen(gen const& copy) : _num(copy._num) {}
//gen& operator=(gen const& copy) { _num = copy._num; }
//~gen() {}
// Why do we do this?
T operator()() { T digit = _num % 10; _num /= 10; return digit; }
T _num;
};
// How is this different to the above?
template <typename T>
bool check_non_zero (T i)
{
return i != 0;
}
// And this? what's going on here with the parameter v?
template <typename T, int _size>
T sum_of_digits(T value, std::vector<T>& v)
{
// Why would we do this?
if (value == 0)
{
v.push_back(0);
return 0;
}
// What is the purpose of this?
v.resize(_size);
// What is this called?
gen<T> gen_v(value);
// generate_n? What is this beast? what does v.begin() return? where did _size come from?
generate_n(v.begin(), _size, gen_v);
// reverse? what does this do?
reverse(v.begin(), v.end());
// erase? find_if? what do they do? what the heck is check_non_zero<T>?
v.erase(v.begin(), find_if(v.begin(), v.end(), check_non_zero<T>));
// what the heck is accumulate?
return accumulate(v.begin(), v.end(), 0);
}
int main()
{
// What does this do?
vector<int> v;
// What is this peculiar syntax? NOTE: 10 here is because the largest possible number of int has 10 digits
int sum = sum_of_digits<int, 10>(123, v);
cout << "digits: ";
// what does copy do? and what the heck is ostream_iterator?
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "sum: " << sum << endl;
// other things to consider, what happens if the number is negative?
return 0;
}
cout << "Enter your digit: ";
cin >> number;
if (number == 0)
return 0;
int a = number % 10;
int second = (number / 10);
int b = second % 10;
int third = (second / 10);
int c = third % 10;
int fourth = (third / 10);
int d = fourth % 10;
int fifth = (fourth / 10);
int e = fifth % 10;
cout << e << " " << d << " " << c << " " << b << " " << a << endl;
return 0;
int n = 12345;
vector<int> digits;
while (n != 0) {
digits.insert(digits.begin(), n%10);
n /= 10;
}
for(auto & i : digits)
cout << i << " ";