获取整数输入并在没有数组的情况下逐行输出

Get Integer input and output it line by line without an array

当我使用 scanf 输入一个整数并拆分该整数并在没有数组的情况下逐行打印时,我正在尝试编写一个 c 程序。我可以举例说明我将如何做到这一点。

123 / 100 = 1
123 % 100 = 23

23 / 10 = 2
23 % 19 = 3

1
2
3

我知道怎么做,但问题是当我 运行 这个代码 .

# include <stdio.h>
# include <string.h>
# include <math.h>

int main (void)
{
    int no, a;
    int count, new;
    int newNum = 0;

    printf("Enter an intger number = ");
    scanf("%d", &no);

    newNum = no;
    printf("You entered = %d\n", newNum);

    while(newNum != 0){
        newNum = newNum / 10;
        count++;
    }

    count--;
    count = pow(10, count);

    printf("Power of ten = %d\n", count);

    while(count != 1){
        new = no / count;
        no = no % count;
        printf("%d\n", new);
        count = count / 10;
    }
    return 0;
}

输出:

Enter an intger number = 123
You entered = 123
Power of ten = -2147483648
0
0
0
0
0
0
0
0
-5
-9
Floating point exception (core dumped)

问题是十行的幂没有输出正确的值但是如果我评论第二个while循环部分。

# include <stdio.h>
# include <string.h>
# include <math.h>

int main (void)
{
    int no;
    int count, new;
    int newNum = 0;

    printf("Enter an intger number = ");
    scanf("%d", &no);

    newNum = no;
    printf("You entered = %d\n", newNum);

    while(newNum != 0){
        newNum = newNum / 10;
        count++;
    }

    count--;
    count = pow(10, count);

    printf("Power of ten = %d\n", count);

//  while(count != 1){
//      new = no / count;
//      no = no % count;
//      printf("%d\n", new);
//      count = count / 10;
//  }
    return 0;
}

输出:

Enter an intger number = 123
You entered = 123
Power of ten = 100

这次 10 的幂显示了正确的值。

我该怎么做才能避免这个问题?

在您的代码中,变量 count 具有局部作用域(自动存储持续时间)。

局部作用域变量由于其堆栈分配而未初始化。

int count=0;

(C99 标准)第 6.7.8 节第 10 条:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

If an object that has static storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules;
  • if it is a union, the first named member is initialized (recursively) according to these rules.

注意:您应该避免使用 new 作为变量名。

对于初学者,您应该小心,不要在计算 10 的幂时溢出。

始终测试您的程序的边界值,例如 INT_MAXUINT_MAX

不需要使用数学函数。

给你。

#include <stdio.h>

int main( void ) 
{
    while ( 1 )
    {
        const unsigned int Base = 10;

        printf( "Enter a non-negative number (0 - exit): " );

        unsigned int n;

        if ( scanf( "%u", &n ) != 1 || n == 0 ) break;

        unsigned int divisor = 1; 

        for ( unsigned int tmp = n; !( tmp < Base ); tmp /= Base )
        {
            divisor *= Base;;
        }

        printf( "%u\n", n / divisor );

        for ( ; divisor != 1; divisor /= Base )
        {
            printf( "%u\n", n % divisor / ( divisor / Base ) );
        }

        putchar( '\n' );
    }

    return 0;
}

程序输出可能看起来像

Enter a non-negative number (0 - exit): 1
1

Enter a non-negative number (0 - exit): 10
1
0

Enter a non-negative number (0 - exit): 123456789
1
2
3
4
5
6
7
8
9

Enter a non-negative number (0 - exit): 4294967295
4
2
9
4
9
6
7
2
9
5

Enter a non-negative number (0 - exit): 0

另一种方法是使用此处所示的递归函数

#include <stdio.h>

void output_digits( unsigned int n )
{
    const unsigned int Base = 10;

    unsigned int digit = n % Base;

    if ( ( n /= Base ) != 0 ) output_digits( n );

    printf( "%u\n", digit );
}

int main( void ) 
{
    while ( 1 )
    {
        printf( "Enter a non-negative number (0 - exit): " );

        unsigned int n;

        if ( scanf( "%u", &n ) != 1 || n == 0 ) break;

        output_digits( n );

        putchar( '\n' );
    }

    return 0;
}

您想打印 10 进制数的每一位。 可以用这个简单的公式

检索位数
int n_digit = (int)log10(x) +1;

测试:(int)log10(99) +1 = (int)1.9956 +1 = 2(int)log10(100) +1 = (int)2 +1 = 3

所以你想打印 10 的每个幂的商:

for(int i=n_digit-1; i>=0; i--)
{
    // Calc 10^i without pow function
    int num_10_pow_i = 1;
    for(int j=0; j<i; j++)
        num_10_pow_i *=10;

    printf("N[%d]=",i);
    printf("%d\n", (int)(x/num_10_pow_i));
    x = x-((int)(x/num_10_pow_i) * num_10_pow_i);
}

请不要使用处理整数的 pow 函数。

阅读此处了解更多信息:

在我的程序中,我像这样将整数变量设置为 0。

int no, a = 0;
int count = 0, new = 0;
int newNum = 0;

之后我的程序运行完美,但没有得到完整的输出。这个问题来自这个部分

while(count != 1){
    new = no / count;
    no = no % count;
    printf("%d\n", new);
    count = count / 10;
}

输出-:

Enter an intger number = 123
You entered = 123
Power of ten = 100
1
2

你可以看到3 没有在输出部分。因为我在 while 循环之外添加了额外的行以获得我预期的输出。我已经添加了以下内容。

while(count != 1){
    new = no / count;
    no = no % count;
    printf("%d\n", new);
    count = count / 10;
}
printf("%d\n", no);

输出-:

Enter an intger number = 123
You entered = 123
Power of ten = 100
1
2
3

现在完成了。