提取整数的数字元素

extracting the digital elements of an integer

本文关键字:元素 数字 整数 提取      更新时间:2023-10-16

我想知道如何获得整数的数字元素,以便显示一个数组,例如来自数字17的[1][7]。我在这里找到了这个计算数字的解决方案,现在我需要将组成整数的数字放入一个数组中,所以我想检索每个数字的值

int A = 17;
int table[lenght_of_A];
A 'operation_to_fit_A_in' table;

在C++中,我会这样做。那么就不需要事先对数字进行计数:

#include <list>
std::list<int> digits;
// Slightly adapted algorithm from sharptooth: this one yields a zero if 
// the value was 0. (sharptooth's wouldn't yield the digit 0 if a zero was 
// being analyzed.)
do {
    digits.push_front( value%10 );
    value /= 10;
} while( value!=0 );

digits现在包含一个单独数字的列表,并且可以以您喜欢的任何形式显示。

这个想法是运行一个循环(伪代码):

while( value != 0 ) {
    nextDigitValue = value % 10; // this is "modulo 10"
    nextDigitChar = '0' + nextDigitValue;
    value = value / 10; // this lets you proceed to next digit
}
感谢您的帮助。终于InI让它工作起来了。
void Getint(int A; int* array)
{
    if (A < 10)
        array[0] = A;
        array[1] = 0;
    if (A >= 10 &&  A < 100)
        array[0] = A / 10;
        array[1] = A % 10;
}