C++ 如何在不将数字转换为字符串/字符数组的情况下获取数字的数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1397737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:51:30  来源:igfitidea点击:

How to get the digits of a number without converting it to a string/ char array?

c++

提问by chustar

How do I get what the digits of a number are in C++ without converting it to strings or character arrays?

如何在不将其转换为字符串或字符数组的情况下获取 C++ 中数字的数字?

回答by Vinay Sajip

The following prints the digits in order of ascending significance (i.e. units, then tens, etc.):

以下按重要性升序(即单位,然后是十位等)打印数字:

do {
    int digit = n % 10;
    putchar('0' + digit);
    n /= 10;
} while (n > 0);

回答by tunnuz

What about floor(log(number))+1?

怎么样floor(log(number))+1

With ndigits and using base byou can express any number up to pow(b,n)-1. So to get the number of digits of a number xin base byou can use the inverse function of exponentiation: base-b logarithm. To deal with non-integer results you can use the floor()+1trick.

使用n位数字并使用b基数,您可以表示最多pow(b,n)-1. 因此,要获得以b为底数的数字x的位数,您可以使用求幂的反函数:以 b 为底数的对数。要处理非整数结果,您可以使用该技巧。floor()+1

PS: This works for integers, not for numbers with decimals (in that case you should know what's the precision of the type you are using).

PS:这适用于整数,不适用于带小数的数字(在这种情况下,您应该知道您使用的类型的精度是多少)。

回答by Martin York

Since everybody is chiming in without knowing the question.
Here is my attempt at futility:

因为每个人都在不知道问题的情况下插话。
这是我徒劳的尝试:

#include <iostream>

template<int D> int getDigit(int val)       {return getDigit<D-1>(val/10);}
template<>      int getDigit<1>(int val)    {return val % 10;}

int main()
{
    std::cout << getDigit<5>(1234567) << "\n";
}

回答by P Shved

I have seen many answers, but they allforgot to use do {...} while()loop, which is actually the canonical way to solve this problem and handle 0properly.

看了很多答案,但是忘记用do {...} while()loop了,这其实是解决这个问题的规范方法,处理0得当。

My solution is based on thisone by Naveen.

我的解决方案是基于一个由纳文

int n = 0;
std::cin>>n;

std::deque<int> digits;
n = abs(n);
do {
    digits.push_front( n % 10);
    n /= 10;
} while (n>0);

回答by Naveen

You want to some thing like this?

你想要这样的东西吗?

 int n = 0;
    std::cin>>n;

    std::deque<int> digits;
    if(n == 0)
    {
        digits.push_front(0);
        return 0;
    }

    n = abs(n);
    while(n > 0)
    {
        digits.push_front( n % 10);
        n = n /10;
    }
    return 0;

回答by Steve Rowe

Something like this:

像这样的东西:

int* GetDigits(int num, int * array, int len) {
  for (int i = 0; i < len && num != 0; i++) {
    array[i] = num % 10;
    num /= 10;
  }
}

The mod 10's will get you the digits. The div 10s will advance the number.

mod 10's 将为您提供数字。div 10s 将推进数字。

回答by Ivan Gelov

Integer version is trivial:

整数版本是微不足道的:

int fiGetDigit(const int n, const int k)
{//Get K-th Digit from a Number (zero-based index)
    switch(k)
    {
        case 0:return n%10;
        case 1:return n/10%10;
        case 2:return n/100%10;
        case 3:return n/1000%10;
        case 4:return n/10000%10;
        case 5:return n/100000%10;
        case 6:return n/1000000%10;
        case 7:return n/10000000%10;
        case 8:return n/100000000%10;
        case 9:return n/1000000000%10;
    }
    return 0;
}

回答by user unknown

simple recursion:

简单递归:

#include <iostream>

// 0-based index pos
int getDigit (const long number, int pos) 
{
    return (pos == 0) ? number % 10 : getDigit (number/10, --pos);
}

int main (void) {
    std::cout << getDigit (1234567, 4) << "\n";    
}

回答by Mac

Those solutions are all recursive or iterative. Might a more direct approach be a little more efficient?

这些解决方案都是递归的或迭代的。更直接的方法会更有效吗?

Left-to-right:

左到右:

int getDigit(int from, int index)
{
   return (from / (int)pow(10, floor(log10(from)) - index)) % 10;
}

Right-to-left:

右到左:

int getDigit(int from, int index)
{
   return (from / pow(10, index)) % 10;
}

回答by Deep Panchal

A simple solution would be to use the log 10 of a number. It returns the total digits of the number - 1. It could be fixed by using converting the number to an int.

一个简单的解决方案是使用数字的对数 10。它返回数字的总位数 - 1。可以通过将数字转换为 int 来修复它。

int(log10(number)) + 1