假设我在C中有一个多位数的整数。我想将其分解为单位整数。

123 会变成 1, 2, , 和 3.

我该怎么做,特别是如果我不知道整数有多少位数字?

有帮助吗?

解决方案

int value = 123;
while (value > 0) {
 int digit = value % 10;
 // do something with digit
 value /= 10;
}

其他提示

首先,计算数字:

unsigned int count(unsigned int i) {
 unsigned int ret=1;
 while (i/=10) ret++;
 return ret;
}

然后,您可以将它们存储在数组中:

unsigned int num=123; //for example
unsigned int dig=count(num);
char arr[dig];
while (dig--) {
 arr[dig]=num%10;
 num/=10;
}

作为一个提示,在数字中获得第n个数字非常容易。除以10 n次,然后在mod 10或c中除以:

int nthdig(int n, int k){
     while(n--)
         k/=10;
     return k%10;
}

123的最后一位数字为123%10。您可以通过123/10删除123个数字 - 使用Integer Division这将为您提供12.回答您的问题:“我如何知道您拥有多少位数字” - 尝试按照上述进行操作,您将看到如何知道何时停止。

我认为下面的代码会有所帮助。

temp = num;
while(temp)
{
    temp=temp/10;
    factor = factor*10;
}

printf("\n%d\n", factor);
printf("Each digits of given number are:\n");

while(factor>1)
{
    factor = factor/10;
    printf("%d\t",num/factor);
    i++;
    num = num % factor;
}

我们可以将此程序用作3个参数的函数。在“ while(a ++ <2)”中,2是您需要的数字数(可以给出一个参数)替换2,而无需数字。在这里,我们可以使用“ z/= pow(10,6)”,如果我们不需要最后一位数字,请用您不需要的数字替换6(可以作为另一个参数),第三个参数是您需要中断的数字。

int main(){
long signed c=0,z,a=0,b=1,d=1;
scanf("%ld",&z);
while(a++<2){
       if(d++==1) 
       z/=pow(10,6);
       c+=(z%10)*b; 
       z/=10;
       b*=10;}
        return c;}
//Based on Tony's answer
#include <stdio.h> 
int nthdig(int n, int k){
    while(n--)
        k/=10;
    return k%10;
}

int main() {
    int numberToSplit = 987;
    printf("Hundreds = %i\n",nthdig(2, numberToSplit));
    printf("Tens     = %i\n",nthdig(1, numberToSplit));
    printf("Units    = %i\n",nthdig(0, numberToSplit));
}

这将导致以下打印输出:

百万= 9

TENS = 8

单位= 7

我根据@Asaelr的代码制作了此内容:

typedef struct digitsArrayPlusNumber {
    uint32_t *baseAddress;
    uint32_t number;
} digitsArrayPlusNumber;

digitsArrayPlusNumber *splitDigits (uint32_t inValue) {
    // based on code from asaelr@stackoverflow.com

    uint32_t inputValue = inValue;

    //Count digits

    uint32_t theCount = 1;
    while (inputValue /= 10)
        theCount++;

    // put in array
    uint32_t *arr = malloc(sizeof(uint32_t) * theCount);
    uint32_t dig = theCount;
    while (dig--) {
        arr[dig]=inValue % 10;
        inValue /= 10;
        //  printf ("%d\n", arr[dig]);
    }

    digitsArrayPlusNumber *dandn = malloc (sizeof(digitsArrayPlusNumber));

    dandn->baseAddress = arr;
    dandn->number = theCount;

    return dandn;

}

int main(int argc, const char * argv[]) {


    for (int d = 0; d < splitDigits(12345678)->number; d++)
        printf ("%u\n", (splitDigits(12345678)->baseAddress)[d]);

}

它运行良好,谢谢!

如果您想以相同的顺序分开数字,则可以尝试此代码,而无需使用数组。

//Separate number digits
#include <stdio.h>
#include <math.h>

void main()
{
    int x, y, n = 0;

    scanf("%d", &x);

    //counting digits
    y = x;
    while (y != 0)
    {
        n += 1;
        y /= 10;
    }

    //printing separated digits
    int i;
    for (i = ceil(pow(10, (n - 1))); i != 0; i /= 10)
        printf("%d  ", (x / i) % 10);
}

您可以使用%10,这意味着剩余的,如果您将其分配后的数字。所以 123 % 10 IS 3,因为其余的为3,从123中提取3,然后是120,然后将120与12分隔为12。并执行相同的过程。

您可以分割和征服,但您已经重写了所有算术库。我建议使用多精确库 https://gmplib.org 但是当然是好习惯

int l1; //123456 for example
scanf("%d",&l1);
char s[sizeof(l1)];
sprintf(s,"%5d",l1);'

//This will give you separate digits of the number in char format inside s[0],s[1] 
//and so on.

//If you want them in int format, declare a int array say int i[sizeof(l1)] and add 
//the following code

for(int c=1;c<=sizeof(l1);c++){
i[c] = s[c] - '0';
} 

//Now i[0], i[1] etc will have the digits in int format
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top