当输入为 10 且阶乘值仅返回最多 33 个数字时程序失败
Program fails when input is 10 and factorial value is returning upto 33 number only
当输入 10 时,程序(用于计算阶乘)失败,而对于所有其他输入(在系统限制内),程序运行正常,除非输入任何字符它进入无限循环,输出为 "Factorial of 1 is 1"。
如果我输入 34 或任何大于 34 的数字,那么我得到的输出为 "factorial of [entered number] is 0"。为什么大于 33 的阶乘值为 0?
程序如下:
#include <stdio.h>
#include <stdlib.h>
int display();
void fact_fun(int num_fact);
int main()
{
int num = 0;
char next;
next=display();
while( next != '\n')
{
num = next;
fact_fun(num);
next=display();
}
return 0;
}
int display()
{
int input;
printf("\nEnter number to find factorial or press ENTER to exit :");
scanf("%d",&input);
return input;
}
void fact_fun(int num_fact)
{
int fact=1;
if((num_fact>='a' && num_fact<='z') || (num_fact>='A' && num_fact<='Z'))
{
printf("Alphabet is not allowed");
}
else if(num_fact < 0)
{
printf("Negative number is not allowed");
}
else if(num_fact == 0)
{
printf("Factorial of %d is 1",num_fact);
}
else
{
for(int i = 1; i<= num_fact; i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d",num_fact,fact);
}
}
请指导我哪里有错误。
在 display
函数中,您使用 char
而不是 int
,因为您在 scanf
中使用 %d
格式说明符,将其更改为 int
无处不在。你肯定在编译的时候看到了警告:
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’
除此之外,您的程序在输入数字10时发送失败,解决方法:
while(next>=0) //WHEN NEGATIVE NUMBER IS ENTERED IT WILL END THE LOOP
{
num = next;
fact_fun(num);
next=display();
}
简化fact_fun:
void fact_fun(int num_fact)
{
int fact =1;
if(num_fact == 0)
{
printf("Factorial of %d is 1",num_fact);
return;
}
else
{
for(int i = 1; i<= num_fact; i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d",num_fact,fact);
}
}
然后就可以正常工作了。
当输入为 10 时程序失败的原因:换行符 \n
的 ASCII
值也是 10,所以当输入 10 时,\n
的 int
值与 display
返回的值进行比较,即 10,因此 while 循环在这种情况下永远不会执行。
这是一种安全的输入方法(scanf
是一场灾难):
int display()
{
char inp[10]={0};
int input;
int index=0;
printf("Enter number to find factorial or press ENTER to exit : ");
while(((input=getchar())!=EOF)&(index<10))
{
if((input>='0')&&(input<='9'))
{
inp[index++]=input;
}
else if(input=='\n')
break;
else
return -1;
}
input=atoi(inp);
return input;
}
当输入 10 时,程序(用于计算阶乘)失败,而对于所有其他输入(在系统限制内),程序运行正常,除非输入任何字符它进入无限循环,输出为 "Factorial of 1 is 1"。
如果我输入 34 或任何大于 34 的数字,那么我得到的输出为 "factorial of [entered number] is 0"。为什么大于 33 的阶乘值为 0?
程序如下:
#include <stdio.h>
#include <stdlib.h>
int display();
void fact_fun(int num_fact);
int main()
{
int num = 0;
char next;
next=display();
while( next != '\n')
{
num = next;
fact_fun(num);
next=display();
}
return 0;
}
int display()
{
int input;
printf("\nEnter number to find factorial or press ENTER to exit :");
scanf("%d",&input);
return input;
}
void fact_fun(int num_fact)
{
int fact=1;
if((num_fact>='a' && num_fact<='z') || (num_fact>='A' && num_fact<='Z'))
{
printf("Alphabet is not allowed");
}
else if(num_fact < 0)
{
printf("Negative number is not allowed");
}
else if(num_fact == 0)
{
printf("Factorial of %d is 1",num_fact);
}
else
{
for(int i = 1; i<= num_fact; i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d",num_fact,fact);
}
}
请指导我哪里有错误。
在 display
函数中,您使用 char
而不是 int
,因为您在 scanf
中使用 %d
格式说明符,将其更改为 int
无处不在。你肯定在编译的时候看到了警告:
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’
除此之外,您的程序在输入数字10时发送失败,解决方法:
while(next>=0) //WHEN NEGATIVE NUMBER IS ENTERED IT WILL END THE LOOP
{
num = next;
fact_fun(num);
next=display();
}
简化fact_fun:
void fact_fun(int num_fact)
{
int fact =1;
if(num_fact == 0)
{
printf("Factorial of %d is 1",num_fact);
return;
}
else
{
for(int i = 1; i<= num_fact; i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d",num_fact,fact);
}
}
然后就可以正常工作了。
当输入为 10 时程序失败的原因:换行符 \n
的 ASCII
值也是 10,所以当输入 10 时,\n
的 int
值与 display
返回的值进行比较,即 10,因此 while 循环在这种情况下永远不会执行。
这是一种安全的输入方法(scanf
是一场灾难):
int display()
{
char inp[10]={0};
int input;
int index=0;
printf("Enter number to find factorial or press ENTER to exit : ");
while(((input=getchar())!=EOF)&(index<10))
{
if((input>='0')&&(input<='9'))
{
inp[index++]=input;
}
else if(input=='\n')
break;
else
return -1;
}
input=atoi(inp);
return input;
}