C:如何将多位数拆分成单独的变量?

时间:2012-02-15 22:46:52

标签: c math numbers digits digit

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

123会变成123

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

12 个答案:

答案 0 :(得分:31)

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

答案 1 :(得分:7)

首先,计算数字:

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;
}

答案 2 :(得分:1)

作为提示,获得数字中的第n位非常容易;除以10 n次,然后是mod 10,或C:

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

答案 3 :(得分:0)

123的最后一位数字是123%10。 您可以通过执行123/10来删除123的最后一位数 - 使用整数除法,这将给出12。 要回答关于“我怎么知道你有多少位数”的问题 - 尝试如上所述,你会看到如何知道何时停止。

答案 4 :(得分:0)

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

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;
}

答案 5 :(得分:0)

如果您想在不使用数组的情况下以相同顺序分隔数字,请尝试使用此代码。

//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);
}

答案 6 :(得分:0)

我们可以使用这个程序作为一个带有3个参数的函数。在“while(a ++&lt; 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;}

答案 7 :(得分:0)

//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

十分= 8

单位= 7

答案 8 :(得分:0)

我根据@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]);

}

效果很好,谢谢!

答案 9 :(得分:0)

您可以使用%10,这意味着剩余部分,如果您将其分割后的数字。所以123 % 10是3,因为余数是3,从123减去3,然后是120,然后将120与10除以12,并执行相同的过程。

答案 10 :(得分:-1)

你可以分而治之,但你已经重写了所有的算术库。我建议使用多精度库https://gmplib.org但当然这是一种很好的做法

答案 11 :(得分:-1)

currimg=currimg.next();
相关问题