【问题标题】:Get Integer input and output it line by line without an array获取整数输入并在没有数组的情况下逐行输出
【发布时间】:2020-04-12 20:37:04
【问题描述】:

当我使用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

这一次十的幂显示正确的值。

我可以做些什么来避免这个问题?

  • 有什么方法可以在不使用数组的情况下做到这一点?

【问题讨论】:

  • count 未初始化且具有不确定值时,您执行count++
  • 有什么方法可以在不使用数组的情况下做到这一点...?您没有使用数组...您能详细说明一下吗?
  • 为什么要在没有% 的情况下这样做?您到底要达到什么目标,或者您要满足的确切要求是什么?如果您使用递归,您可以在大约 5 行代码中完成(尽管仍然使用 %)。
  • 如我所说,你可以使用递归。
  • void p(int i) { if (!i) return; p(i/10); printf("%d\n",i%10); }

标签: c loops for-loop while-loop integer-division


【解决方案1】:

在您的代码变量中count 具有本地范围(自动存储持续时间)。

由于堆栈分配,局部范围变量未初始化。

int count=0;

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

如果具有自动存储期限的对象未初始化 明确地说,它的值是不确定的。

如果具有静态存储持续时间的对象未初始化 明确地,那么:

  • 如果有指针类型,则初始化为空指针;
  • 如果它具有算术类型,则将其初始化为(正或无符号)零;
  • 如果是聚合,则每个成员都根据这些规则进行初始化(递归);
  • 如果是联合,则根据这些规则(递归)初始化第一个命名的成员。

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

【讨论】:

  • 我犯了程序员不会犯的愚蠢错误。有没有其他代码可以做我的工作。我使用 '%' 来获取我的输出。没有它可以做到吗?
  • @Kalana 我不明白你。你的意思是% 作为printf 格式说明符吗?..
  • @LPs 我认为 OP 意味着模数。
  • @Kalana 这取决于您对数组的含义。您可以使用字符串(即使用c 的数组)和相关函数(如strlen)。无论如何,您的实现是计算整数位数的标准(更快)实现。
  • 打扰了,非常感谢@LPs
【解决方案2】:

对于初学者,您应该小心,不要计算 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;
}

【讨论】:

  • 这是我一直在寻找的答案。非常感谢你分享这个。我希望两个答案都能被接受
【解决方案3】:

您想要打印 base10 数字的每个数字。 用这个简单的公式就可以得到位数

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 函数处理整数。

阅读此处了解更多信息: Does pow() work for int data type in C?

【讨论】:

  • 是的,我能理解,谢谢@G。 C.
  • (int)log10(x)(以及 pow() 出于类似原因)容易出现舍入错误,因此代码失败。最好避免整数问题的浮点数学。 (int)log10(x)x==0时也是个问题。
【解决方案4】:

在我的程序中,我已经像这样将整数变量设置为 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

现在已经完成了。

【讨论】:

    猜你喜欢
    • 2010-11-27
    • 2012-04-26
    • 2020-06-02
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    相关资源
    最近更新 更多