代码之家  ›  专栏  ›  技术社区  ›  Johannes Jensen

从一个整数中得到每个数字

  •  21
  • Johannes Jensen  · 技术社区  · 14 年前

    int score = 1529587;
    

    使用位运算符

    我很确定这是可以做到的,因为我曾经使用过类似的方法从十六进制颜色值中提取红绿色和蓝色值。

    我该怎么做?

    编辑

    8 回复  |  直到 7 年前
        1
  •  56
  •   Martin B    14 年前

    使用模运算符:

    while(score)
    {
        printf("%d\n", score % 10);
        score /= 10;
    }
    

    请注意,这将以相反的顺序给出数字(即先给出最低有效位)。如果你想先得到最重要的数字,你必须将这些数字存储在一个数组中,然后以相反的顺序读出它们。

        2
  •  4
  •   David M    14 年前

        3
  •  4
  •   valdo    14 年前

    同意前面的回答。

    一点修正:有一种更好的方法可以从左到右打印十进制数字,而不需要分配额外的缓冲区。此外,如果 score

    int div;
    for (div = 1; div <= score; div *= 10)
        ;
    
    do
    {
        div /= 10;
        printf("%d\n", score / div);
        score %= div;
    } while (score);
    
        4
  •  2
  •   Roberto Caboni Prince Rabadiya    3 年前

    不要重新发明轮子。C有 sprintf

    因为变量名为score,所以我猜这是一个游戏,您计划使用分数的各个数字将数字图示符显示为图像。在这种情况下, 把格式数据写成串

        5
  •  1
  •   Britton Kerin    7 年前

    此解决方案在整个范围内给出正确的结果[0,UINT\u MAX] 无需缓冲数字。

    它还适用于更广泛的类型或有符号类型(具有正值),并进行适当的类型更改。

    #include <limits.h>
    #include <stdio.h>
    
    int
    main (void)
    {
      unsigned int score = 42;   // Works for score in [0, UINT_MAX]
    
      printf ("score via printf:     %u\n", score);   // For validation
    
      printf ("score digit by digit: ");
      unsigned int div = 1;
      unsigned int digit_count = 1;
      while ( div <= score / 10 ) {
        digit_count++;
        div *= 10;
      }
      while ( digit_count > 0 ) {
        printf ("%d", score / div);
        score %= div;
        div /= 10;
        digit_count--;
      }
      printf ("\n");
    
      return 0;
    }
    
        6
  •  0
  •   PADYMKO    7 年前

    通常,这个问题可以通过在循环中使用数字的模来解决,或者将数字转换为字符串。要将数字转换为字符串,可以使用函数 itoa ,所以考虑循环中一个数的模的变量。


    文件的内容 get_digits.c

    $ cat get_digits.c 
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    // return a length of integer
    unsigned long int get_number_count_digits(long int number);
    
    // get digits from an integer number into an array
    int number_get_digits(long int number, int **digits, unsigned int *len);
    
    // for demo features
    void demo_number_get_digits(long int number);
    
    
    int
    main()
    {
        demo_number_get_digits(-9999999999999);
        demo_number_get_digits(-10000000000);
        demo_number_get_digits(-1000);
        demo_number_get_digits(-9);
        demo_number_get_digits(0);
        demo_number_get_digits(9);
        demo_number_get_digits(1000);
        demo_number_get_digits(10000000000);
        demo_number_get_digits(9999999999999);
        return EXIT_SUCCESS;
    }
    
    
    unsigned long int
    get_number_count_digits(long int number)
    {
        if (number < 0)
            number = llabs(number);
        else if (number == 0)
            return 1;
    
        if (number < 999999999999997)
            return floor(log10(number)) + 1;
    
        unsigned long int count = 0;
        while (number > 0) {
            ++count;
            number /= 10;
        }
        return count;
    }
    
    
    int
    number_get_digits(long int number, int **digits, unsigned int *len)
    {
        number = labs(number);
    
        // termination count digits and size of a array as well as
        *len = get_number_count_digits(number);
    
        *digits = realloc(*digits, *len * sizeof(int));
    
        // fill up the array
        unsigned int index = 0;
        while (number > 0) {
            (*digits)[index] = (int)(number % 10);
            number /= 10;
            ++index;
        }
    
        // reverse the array
        unsigned long int i = 0, half_len = (*len / 2);
        int swap;
        while (i < half_len) {
            swap = (*digits)[i];
            (*digits)[i] = (*digits)[*len - i - 1];
            (*digits)[*len - i - 1] = swap;
             ++i;
        }
    
        return 0;
    }
    
    
    void
    demo_number_get_digits(long int number)
    {
        int *digits;
        unsigned int len;
    
        digits = malloc(sizeof(int));
    
        number_get_digits(number, &digits, &len);
    
        printf("%ld --> [", number);
        for (unsigned int i = 0; i < len; ++i) {
            if (i == len - 1)
                printf("%d", digits[i]);
            else
                printf("%d, ", digits[i]);
        }
        printf("]\n");
    
        free(digits);
    }
    

    用GNU GCC演示

    $~/Downloads/temp$ cc -Wall -Wextra -std=c11 -o run get_digits.c -lm
    $~/Downloads/temp$ ./run
    -9999999999999 --> [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
    -10000000000 --> [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    -1000 --> [1, 0, 0, 0]
    -9 --> [9]
    0 --> [0]
    9 --> [9]
    1000 --> [1, 0, 0, 0]
    10000000000 --> [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    9999999999999 --> [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
    

    用LLVM/Clang演示

    $~/Downloads/temp$ rm run
    $~/Downloads/temp$ clang -std=c11 -Wall -Wextra get_digits.c -o run -lm
    setivolkylany$~/Downloads/temp$ ./run
    -9999999999999 --> [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
    -10000000000 --> [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    -1000 --> [1, 0, 0, 0]
    -9 --> [9]
    0 --> [0]
    9 --> [9]
    1000 --> [1, 0, 0, 0]
    10000000000 --> [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    9999999999999 --> [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
    

    $~/Downloads/temp$ cc --version | head -n 1
    cc (Debian 4.9.2-10) 4.9.2
    $~/Downloads/temp$ clang --version
    Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
    Target: x86_64-pc-linux-gnu
    Thread model: posix
    
        7
  •  0
  •   Esann    6 年前
    //this can be easily understandable for beginners     
    int score=12344534;
    int div;
    for (div = 1; div <= score; div *= 10)
    {
    
    }
    /*for (div = 1; div <= score; div *= 10); for loop with semicolon or empty body is same*/
    while(score>0)
    {
        div /= 10;
        printf("%d\n`enter code here`", score / div);
        score %= div;
    }
    
        8
  •  0
  •   RohitK    6 年前
    #include<stdio.h>
    
    int main() {
    int num; //given integer
    int reminder;
    int rev=0; //To reverse the given integer
    int count=1;
    
    printf("Enter the integer:");
    scanf("%i",&num);
    
    /*First while loop will reverse the number*/
    while(num!=0)
    {
        reminder=num%10;
        rev=rev*10+reminder;
        num/=10;
    }
    /*Second while loop will give the number from left to right*/
    while(rev!=0)
    {
        reminder=rev%10;
        printf("The %d digit is %d\n",count, reminder);
        rev/=10;
        count++; //to give the number from left to right 
    }
    return (EXIT_SUCCESS);}
    
        9
  •  0
  •   Roberto Caboni Prince Rabadiya    3 年前

    sprintf chars unsigned 分数:

    unsigned int score = 1529587, i;
    char stringScore [11] = { 0 };
    
    sprintf( stringScore, "%d, score );
    
    for( i=0; i<strlen(stringScore); i++ )
        printf( "%c\n", stringScore[i] );
    

    请注意:

    • stringScore 假设 int ,在您的平台中为4字节,因此最大整数长度为10位。第十一个是字符串终止符 '\0' .
    • 把格式数据写成串 为你做所有的工作

    你需要每一个数字都有一个整数吗?

    stringScore公司 只包含数字,转换非常简单。如果 dig

    int intDigit = dig - '0';
    
        10
  •  -1
  •   fsalazar_sch    4 年前

    我已经做了这个解决方案,它很简单,取而代之的是读取一个整数,我读取一个字符串(C中的char数组),然后用一个for bucle来写,代码还写数字的和

    // #include<string.h>
    
    scanf("%s", n);
    int total = 0;
    
    for (int i = 0; i< strlen(n); i++){
        printf("%c", n[i]);
        total += (int)(n[i]) -48;
    }
    
    printf("%d", total);