我感到愤怒:
int iNums = 12476;
现在,我想从INum获得每位数位数。 类似:
foreach(iNum in iNums){
printf("%i-", iNum);
}
So the output would be: "1-2-4-7-6-". But i actually need each digit as int not as char.
得到帮助。
我感到愤怒:
int iNums = 12476;
现在,我想从INum获得每位数位数。 类似:
foreach(iNum in iNums){
printf("%i-", iNum);
}
So the output would be: "1-2-4-7-6-". But i actually need each digit as int not as char.
得到帮助。
void print_each_digit(int x)
{
if(x >= 10)
print_each_digit(x / 10);
int digit = x % 10;
std::cout << digit <<
;
}
因此,它要加以扼杀,然后就特性进行 it弄。 您可使用<代码>std:ostringstream,例如:
int iNums = 12476;
std::ostringstream os;
os << iNums;
std::string digits = os.str();
Btw the generally used term (for what you call "number") is "digit" - please use it, as it makes the title of your post much more understandable :-)
这里是一种比较通用的、可再生的解决办法,可产生数字媒介:
void collect_digits(std::vector<int>& digits, unsigned long num) {
if (num > 9) {
collect_digits(digits, num / 10);
}
digits.push_back(num % 10);
}
由于数字相对较少,重新入侵受到严格限制。
www.un.org/Depts/DGACM/index_spanish.htm 这里是采取这一行动的途径,但你会按相反顺序行事。
int num;
short temp = 0;
cin>>num;
while(num!=0){
temp = num%10;
//here you will get its element one by one but in reverse order
//you can perform your action here.
num /= 10;
}
我没有考验它只是写一下我的头脑。 任何辛加税错误的借口
http://ideone.com/flbAQ“rel=“nofollow noreferer”>online ideone demo。
vector <int> v;
int i = ....
while(i != 0 ){
cout << i%10 << " - "; // reverse order
v.push_back(i%10);
i = i/10;
}
cout << endl;
for(int i=v.size()-1; i>=0; i--){
cout << v[i] << " - "; // linear
}
在“拥有”职位上取得数位数(从职位1起算为最不重要的数字数字):
digit = (int)(number/pow(10,(pos-1))) % 10;
例:编号=57820 ->pos = 4 ->数 = 7
依次获得数字:
int num_digits = floor( log10(abs(number?number:1)) + 1 );
for(; num_digits; num_digits--, number/=10) {
std::cout << number % 10 << " ";
}
例:编号=57820 ->输出:0 2 8 7 5
你可以履行这一职能:
void printDigits(int number) {
if (number < 0) { // Handling negative number
printf( - );
number *= -1;
}
if (number == 0) { // Handling zero
printf( 0 );
}
while (number > 0) { // Printing the number
printf("%d-", number % 10);
number /= 10;
}
}
从D.Shawley的答复中可以得出以下结果:
void stream_digits(std::ostream& output, int num, const std::string& delimiter = "")
{
if (num) {
stream_digits(output, num/10, delimiter);
output << static_cast<char>( 0 + (num % 10)) << delimiter;
}
}
void splitDigits()
{
int num = 12476;
stream_digits(std::cout, num, "-");
std::cout << std::endl;
}
我不知道这是否更快或更慢或毫无价值,但这将是一种选择:
int iNums = 12476;
string numString;
stringstream ss;
ss << iNums;
numString = ss.str();
for (int i = 0; i < numString.length(); i++) {
int myInt = static_cast<int>(numString[i] - 0 ); // 0 = 48
printf("%i-", myInt);
}
我指出,“iNums”是指有可能成为用户投入,如果用户投入首先是一个扼杀物,那么你就不需要穿过将暗中转换为扼子的通道。
(to_string could be used in c++11)
我知道这是一个老的职位,但所有这些答案对我来说都是不可接受的。
我的目的是把一些人员放在屏幕上,从而提供功能名称。
void RenderNumber(int to_print)
{
if (to_print < 0)
{
RenderMinusSign()
RenderNumber(-to_print);
}
else
{
int digits = 1; // Assume if 0 is entered we want to print 0 (i.e. minimum of 1 digit)
int max = 10;
while (to_print >= max) // find how many digits the number is
{
max *= 10;
digits ++;
}
for (int i = 0; i < digits; i++) // loop through each digit
{
max /= 10;
int num = to_print / max; // isolate first digit
to_print -= num * max; // subtract first digit from number
RenderDigit(num);
}
}
}
根据@Abyx的答复,但使用div
因此,每个数字只有1个司。
#include <cstdlib>
#include <iostream>
void print_each_digit(int x)
{
div_t q = div(x, 10);
if (q.quot)
print_each_digit(q.quot);
std::cout << q.rem << - ;
}
int main()
{
print_each_digit(12476);
std::cout << std::endl;
return 0;
}
产出:
1-2-4-7-6-
N.B. Only works for non-negative ints.
我的解决办法:
void getSumDigits(int n) {
std::vector<int> int_to_vec;
while(n>0)
{
int_to_vec.push_back(n%10);
n=n/10;
}
int sum;
for(int i=0;i<int_to_vec.size();i++)
{
sum+=int_to_vec.at(i);
}
std::cout << sum << ;
}
所用的答案一是这一简单功能:
int getDigit(int n, int position) {
return (n%(int)pow(10, position) - (n % (int)pow(10, position-1))) / (int)pow(10, position-1);
}
希望有人会认为这有助益!
在线C++汇编者,在线管理C++方案
#include <iostream>
#include <cmath>
int main() {
int iNums = 123458;
//t iNumsSize = 5;
int iNumsSize = trunc(log10(iNums)) + 1; // Find length of int value
for (int i=iNumsSize-1; i>=0; i--) {
int y = pow(10, i);
// The pow() function returns the result of the first argument raised to
the power of the second argument.
int z = iNums/y;
int x2 = iNums / (y * 10);
printf("%d ",z - x2*10 ); // Print Values
}
return 0;
}
You can do it using a while loop and the modulo operators. It just gives the digits in the revese order.
int main() {
int iNums = 12476;
int iNum = 0;
while(iNums > 0) {
iNum = iNums % 10;
cout << iNum;
iNums = iNums / 10;
}
}
int a;
cout << "Enter a number: ";
cin >> a;
while (a > 0) {
cout << a % 10 << endl;
a = a / 10;
}
I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...
I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?