改错题
1. 下列给定程序中,函数proc()的功能是:计算n!。
例如,若输入6,则输出“6!=720.000000”。
请修改程序中的错误,使它能得出正确的结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdio.h>
#include<conio.h>
double proc(int n)
{
double result=1.0;
//****found****
if n==0
return 1.0;
if(n>1&&n<170)
//****found****
result*=n--
return resuh;
}
void main()
{int n;
printf("Input N:");
scanf("%d",&n);
printf("\n\n%d!=%1f\n\n",n,proc(n));
}
【正确答案】(1)错误:if n==0
正确:if(n==0)
(2)错误:result *=n--
正确:result=n*proc(n-1);
【答案解析】 根据C语言的语法规则,语句if的条件必须在括号内,因此,“if n==0”应改为“if(n==0)”。该程序中求整数n的阶乘是使用递归调用来实现的,因此,“result *=n--”应改为“result=n*proc(n-1)”。