C:如何将多位数分解为单独的变量?

发布于 2025-01-06 18:34:13 字数 149 浏览 0 评论 0原文

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

123 将变成 123

我该如何做到这一点,特别是如果我不知道整数有多少位?

Say I have a multi-digit integer in C. I want to break it up into single-digit integers.

123 would turn into 1, 2, and 3.

How can I do this, especially if I don't know how many digits the integer has?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

吻风 2025-01-13 18:34:13
int value = 123;
while (value > 0) {
 int digit = value % 10;
 // do something with digit
 value /= 10;
}
int value = 123;
while (value > 0) {
 int digit = value % 10;
 // do something with digit
 value /= 10;
}
彻夜缠绵 2025-01-13 18:34:13

首先,计算数字:

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

First, count the digits:

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

Then, you can store them in an array:

unsigned int num=123; //for example
unsigned int dig=count(num);
char arr[dig];
while (dig--) {
 arr[dig]=num%10;
 num/=10;
}
孤寂小茶 2025-01-13 18:34:13

123 的最后一位数字是 123 % 10。
您可以通过执行 123/10 去掉 123 的最后一位数字——使用整数除法,这将得到 12。
回答你关于“我怎么知道你有多少位数字”的问题——
尝试按照上面的描述去做,你就会知道如何知道何时停止。

The last digits of 123 is 123 % 10.
You can drop the last digit of 123 by doing 123/10 -- using integer division this will give you 12.
To answer your question about "how do I know how many digits you have" --
try doing it as described above and you will see how to know when to stop.

戏蝶舞 2025-01-13 18:34:13

提示一下,获取数字中的第 n 位数字非常容易;除以 10 n 次,然后模 10,或者在 C 中:

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

As a hint, getting the nth digit in the number is pretty easy; divide by 10 n times, then mod 10, or in C:

int nthdig(int n, int k){
     while(n--)
         k/=10;
     return k%10;
}
万水千山粽是情ミ 2025-01-13 18:34:13

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

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

I think below piece of code will help....

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;
}
飘逸的'云 2025-01-13 18:34:13
//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

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

This results in the following printout:

Hundreds = 9

Tens = 8

Units = 7

手心的海 2025-01-13 18:34:13

我根据@asaelr 的代码做了这个:

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

digitsArrayPlusNumber *splitDigits (uint32_t inValue) {
    // based on code from [email protected]

    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]);

}

它工作得很好,谢谢!

I made this based on the code from @asaelr:

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

digitsArrayPlusNumber *splitDigits (uint32_t inValue) {
    // based on code from [email protected]

    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]);

}

It works quite well, thanks!

冷了相思 2025-01-13 18:34:13

可以使用%10,表示除以后的余数。所以 123 % 10 是 3,因为余数是 3,用 123 减去 3,就是 120,然后用 10 除 120,就是 12。同样的过程。

You can use %10, which means the remainder if the number after you divided it. So 123 % 10 is 3, because the remainder is 3, substract the 3 from 123, then it is 120, then divide 120 with 10 which is 12. And do the same process.

南烟 2025-01-13 18:34:13
        //This is c#, but you can see how easy it would be to convert
        //to another language.

        int myNumber = 123456;
        string myWord = Convert.ToString(myNumber);

        string partOne = myWord.Substring(0, 2);
        string partTwo = myWord.Substring(2, 2);
        string partThree = myWord.Substring(4, 2);

        int num01 = Convert.ToInt32(partOne);
        int num02 = Convert.ToInt32(partTwo);
        int num03 = Convert.ToInt32(partThree);

        Console.WriteLine(num01);
        Console.WriteLine(num02);
        Console.WriteLine(num03);

        Output:
        12
        34
        56
        //This is c#, but you can see how easy it would be to convert
        //to another language.

        int myNumber = 123456;
        string myWord = Convert.ToString(myNumber);

        string partOne = myWord.Substring(0, 2);
        string partTwo = myWord.Substring(2, 2);
        string partThree = myWord.Substring(4, 2);

        int num01 = Convert.ToInt32(partOne);
        int num02 = Convert.ToInt32(partTwo);
        int num03 = Convert.ToInt32(partThree);

        Console.WriteLine(num01);
        Console.WriteLine(num02);
        Console.WriteLine(num03);

        Output:
        12
        34
        56
不及他 2025-01-13 18:34:13

我们可以将此程序用作具有 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;}

we can use this program as a function with 3 arguments.Here in "while(a++<2)", 2 is the number of digits you need(can give as one argument)replace 2 with no of digits you need. Here we can use "z/=pow(10,6)" if we don't need last certain digits ,replace 6 by the no of digits you don't need(can give as another argument),and the third argument is the number you need to break.

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;}
过期以后 2025-01-13 18:34:13

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

You can divide and conquer but you have rewrite all of arithmetic libraries. I suggest using a multi-precision library https://gmplib.org But of course it is good practice

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文