C语言 如何从C中的数字中提取数字?从最高有效数字开始?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26688413/
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-09-02 11:31:53  来源:igfitidea点击:

How to extract digits from a number in C? Begining from the most significant digit?

c

提问by Brian Brown

Getting digits from a number beginning from the least significant in C is pretty easy:

从 C 中从最不重要的数字开始获取数字非常容易:

#include <stdio.h>

int main()
{
    int num = 1024;

    while(num != 0)
    {
        int digit = num % 10;
        num = num / 10;
        printf("%d\n", digit);
    }


    return 0;
}

But how to extract digits beginning from the first digit (here 1), that the solution could be applied to any number?

但是如何从第一个数字(此处为 1)开始提取数字,该解决方案可以应用于任何数字?

It would be trivial with arrays, but I don't want to use array, and I don't want to use logical operators.

使用数组会很简单,但我不想使用数组,也不想使用逻辑运算符。

回答by Spikatrix

The following program does what you want:

以下程序执行您想要的操作:

#include <stdio.h>

int main()
{

    int num =0;
    int power=1;

    printf("Enter any number:");
    scanf("%d",&num);

    while(num>power)
      power*=10;

    power/=10;

    while(num != 0)
    {
        int digit = num /power;
        printf("%d\n", digit);
        if(digit!=0)
          num=num-digit*power;
        if(power!=1)
          power/=10;
    }



    return 0;
}

回答by rcgldr

Assuming a 32 bit signed number that is positive, then as a simple example:

假设一个 32 位有符号数是正数,那么作为一个简单的例子:

#include <stdio.h>

int main()
{
int rmdr;
int dvsr;
int quot;
    scanf("%d", &rmdr);               // read in number
    dvsr = 1000000000;
    while(0 >= (quot = rmdr / dvsr)){ // skip leading zeroes
        rmdr %= dvsr;
        dvsr /= 10;
        if(dvsr == 1)
            break;
    }
    while(dvsr){                      // display number
        quot = rmdr / dvsr;
        printf("%1d", quot);
        rmdr %= dvsr;
        dvsr /= 10;
    }
    printf("\n");
    return(0);
}

or a slight optimization:

或者稍微优化一下:

int main()
{
int rmdr;
int rm10;
int dvsr;
int quot;
    scanf("%d", &rmdr);               // read in number
    rm10 = rmdr/10;
    dvsr = 1;
    while(dvsr <= rm10)               // skip to 1st digit
        dvsr *= 10;
    while(dvsr){                      // display number
        quot = rmdr / dvsr;
        printf("%1d", quot);
        rmdr %= dvsr;
        dvsr /= 10;
    }
    printf("\n");
    return(0);
}

回答by Chandru

Try to store the return value in integer or character array.

尝试将返回值存储在整数或字符数组中。

So I can print the value as we required.

所以我可以根据需要打印值。

If we use character array we can easily find the length and get the result easily.

如果我们使用字符数组,我们可以轻松找到长度并轻松获得结果。

#include <stdio.h>
#include<string.h>
int main()
{
    int num = 1024;
char a[10];
        int i=0;
    while(num != 0)
    {
        a[i++] = (num % 10)+'0';
        num = num / 10;

    }
    a[i]='
#include<stdio.h>
#include <math.h>

int reversDigits(int num)
{
int rev_num = 0;
while(num > 0)
{
rev_num = rev_num*10 + num%10;
num = num/10;
}
return rev_num;
}

int main() {
int i = 1024;
int number = reversDigits(i);
while(number != 0)
{
    int digit = number % 10;
    number = number/ 10;
    printf("%d\n", digit);
}
return 0;
}
'; printf("%s",a); return 0;

}

}

回答by Blackhat002

If you don't want to use array, a simple solution will be..

如果您不想使用数组,一个简单的解决方案将是..

  • Take Input from the user.

  • Reverse the number.

  • And then print the digits.

    #include<stdio.h>
    #include <math.h>
    
    int reversDigits(int num)
    {
    int rev_num = 0;
    while(num > 0)
    {
    rev_num = rev_num*10 + num%10;
    num = num/10;
    }
    return rev_num;
    }
    
    int main() {
    int i = 1024;
    int number = reversDigits(i);
    while(number != 0)
    {
        int digit = number % 10;
        number = number/ 10;
        printf("%d\n", digit);
    }
    return 0;
    }
    
  • 从用户那里获取输入。

  • 倒数。

  • 然后打印数字。

    #include<stdio.h>
    
    //get_int_first gets each digit and multiplies it by it's placeholder value.
    // the number is reconstructed and returned to main
    
    int get_int_first(int num){
        int power = 1,len = 0,first = 0,i = 0;
        int number = 0;
    
        while (num>power){                                          
            power=power*10;                                 //calculating number of zeroes in number. for 789, power = 10 -> 100 -> 1000
        }
        power = power/10;                                   // to multiply with power directly and get highest placeholder, we divide by 10. now power = 100
    
        while (power>1){                                    
            first = (num/power);                            //get digits from the leftmost(100th placeholder/nth placeholder)  f = 7                    f = 8
            number = number + first*power;                  //first*power = gives number as                                    nb = 0 +7*100 = 700      nb = 700 + 80
            num = num - (power*(first));                    //change number to get next significant digit from left            n = 789 - (100*7) = 89   n = 89-10*8=9
            power = power/10;                               //change power to divide number by appropriate power.              p = 100/10 = 10          p = 1
        }
        number = number + num;                              //number is missing the unit's digit and it is stored in num. thus, it is added to number
        return number;
    }
    
    
    int main() {
    
        printf("digits are %d\n",get_int_first(789));
        return 0;
    }
    

回答by Fabulous

I know the question has been answered, but I shall post this answer in case anyone finds it useful.

我知道这个问题已经得到回答,但我会发布这个答案,以防有人觉得它有用。

#include <iostream>
#include <cmath>

using namespace std;
using bignum = unsigned long long;

inline
bignum Power(unsigned x, unsigned y) {
    return y>0 ? x*Power(x,y-1) : 1;
}

// return digits count in a number
inline
int Numlen(bignum num) {
    return num<10 ? 1 : floor(log10(num))+1;
}

// get the starting divisor for our calculation
inline
bignum Getdivisor(unsigned factor) {
    return Power(10, factor);
}


int main()
{
    bignum num{3252198};
    //cin >> num;

    unsigned numdigits = Numlen(num);    
    auto divisor = Getdivisor(numdigits-1);

    while(num > 0) {

        cout << "digit = " << (num/divisor) << "\n";

        num %= divisor;
        divisor /= 10;
    }
}

/*
output:
digit = 3
digit = 2
digit = 5
digit = 2
digit = 1
digit = 9
digit = 8
*/

回答by NeutronStar

Here is my try. Works for positive numbers only. Max range 2^64 (unsigned long long)

这是我的尝试。仅适用于正数。最大范围 2^64 (unsigned long long)

##代码##