简体   繁体   English

使用 C 中的数组将 integer 输入转换为数字

[英]Convert integer input to digit using array in C

#include <stdio.h>

void seperate(int intNum);

int main()
{
   int x;
   
   printf("Please enter an integer: \n");
   scanf("%d",&x);
   seperate(x);
}

void seperate(int intNum)
{
    int i,count=0;
    while (intNum!=0)
    {
        intNum/=10;
        ++count;  //to calculate the number of digits for user input = size of array
    }
    int array[count];   
    
    printf("The number on seperate line as follows:\n");

    for(i=count-1; i>=0; i--)
    {
        array[i]= intNum%10;
        intNum /= 10;
    }           
    for(i=0; i<=count-1; i++)
    {
        printf("%d\n", array[i]);
    }
}

The expected output(it works when the array size is fixed):预期的输出(它在数组大小固定时起作用):

Please enter an integer:
42568
The number on separate line as follows:
4
2
5
6
8

The output: output:

Please enter an integer:
42568
The number on separate line as follows:
0
0
0
0
0

The code works only if the array sized is fixed, how to solve this problem?该代码仅在数组大小固定时才有效,如何解决此问题? Does it not work if I create an array without declaring the size?如果我在不声明大小的情况下创建数组,它不起作用吗?

EDIT: the value for variable intNum changed in while loop, nothing to do with the declaration of the array编辑:变量intNum的值在 while 循环中更改,与数组的声明无关

Your while loop, in which you count the number of digits, is changing the given intNum variable, which will be zero at the end of that loop.您在其中计算位数的while循环正在更改给定的intNum变量,该变量在该循环结束时为零。

You should make a copy of intNum and use/modify that in the first ( while ) loop, like this:您应该复制intNum并在第一个( while )循环中使用/修改它,如下所示:

void seperate(int intNum)
{
    int i, count = 0, temp = intNum; // Make a copy to use in the "count" loop
    while (temp != 0) {
        temp /= 10;
        ++count;  //to calculate the number of digits for user input = size of array
    }
    if (count == 0) count = 1; // To take care of the case when intNum == 0
    int array[count];

    printf("The number on seperate line as follows:\n");

    for (i = count - 1; i >= 0; i--) {
        array[i] = intNum % 10;
        intNum /= 10;
    }
    for (i = 0; i <= count - 1; i++) {
        printf("%d\n", array[i]);
    }
}

Alternatively, you could save the value of intNum and restore that after the while loop has completed:或者,您可以保存intNum的值并在while循环完成后恢复它:

void seperate(int intNum)
{
    int i, count = 0, save = intNum; // Save the original value of intNum ...
    while (intNum != 0) {
        intNum /= 10;
        ++count;  //to calculate the number of digits for user input = size of array
    }
    intNum = save;                   // ... and restore it after counting the digits
    if (!count) count = 1;
    int array[count];
//...

EDIT : There is a much simpler and shorter solution, with only one loop, using the sprintf function to get the digits (in the right order) for you:编辑:有一个更简单和更短的解决方案,只有一个循环,使用sprintf function 为您获取数字(以正确的顺序):

void seperate(int intNum)
{
    char output[100];// Should be long enough for the largest integer.
    sprintf(output, "%d", intNum);
    printf("The number on seperate line as follows:\n");
    for (char* cp = output; *cp; ++cp) {
        printf("%c\n", *cp);
    }
}

You could simplify this problem with a little math.你可以用一点数学来简化这个问题。 First,第一的,

#include <math.h>

Then, the count of digits can be determined by calculating log 10 intNum.然后,可以通过count log 10 intNum 来确定位数。 And you only need a single loop.你只需要一个循环。 Something like,就像是,

void seperate(int intNum)
{
        printf("The number on seperate line as follows:\n");
        double log10 = log(10);
        while (intNum > 0) {
                int count = (int) (log(intNum) / log10);
                int pow10 = pow(10, count);
                int digit = (int) (intNum / pow10);
                printf("%d\n", digit);
                intNum -= digit * pow10;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM