C语言 获取整数输入并逐行输出,没有数组



当我使用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 = %dn", newNum);
while(newNum != 0){
newNum = newNum / 10;
count++;
}
count--;
count = pow(10, count);
printf("Power of ten = %dn", count);
while(count != 1){
new = no / count;
no = no % count;
printf("%dn", 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)

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

# 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 = %dn", newNum);
while(newNum != 0){
newNum = newNum / 10;
count++;
}
count--;
count = pow(10, count);
printf("Power of ten = %dn", count);
//  while(count != 1){
//      new = no / count;
//      no = no % count;
//      printf("%dn", new);
//      count = count / 10;
//  }
return 0;
}

输出:

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

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

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

  • 有没有办法在不使用数组的情况下做到这一点?

在代码变量中,count具有局部范围(自动存储持续时间(。

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

int count=0;

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

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

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

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

注意:应避免使用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( "%un", n / divisor );
for ( ; divisor != 1; divisor /= Base )
{
printf( "%un", 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( "%un", 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;
}

您要打印 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("%dn", (int)(x/num_10_pow_i));
x = x-((int)(x/num_10_pow_i) * num_10_pow_i);
}

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

阅读此处了解更多信息: pow(( 适用于 C 中的 int 数据类型吗?

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

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

之后,我的程序运行良好,但没有获得完整的输出。这个问题来自本节

while(count != 1){
new = no / count;
no = no % count;
printf("%dn", 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("%dn", new);
count = count / 10;
}
printf("%dn", no);

输出-:

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

现在它完成了。

最新更新