繁体   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]);
    }
}

预期的输出(它在数组大小固定时起作用):

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

output:

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

该代码仅在数组大小固定时才有效,如何解决此问题? 如果我在不声明大小的情况下创建数组,它不起作用吗?

编辑:变量intNum的值在 while 循环中更改,与数组的声明无关

您在其中计算位数的while循环正在更改给定的intNum变量,该变量在该循环结束时为零。

您应该复制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]);
    }
}

或者,您可以保存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];
//...

编辑:有一个更简单和更短的解决方案,只有一个循环,使用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);
    }
}

你可以用一点数学来简化这个问题。 第一的,

#include <math.h>

然后,可以通过count log 10 intNum 来确定位数。 你只需要一个循环。 就像是,

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