寻求帮助,我的这个函数模块儿该怎么写?
我的一个程序如下:
/**** 这个程序是正确的程序****/
#include "stdio.h "
#include "conio.h "
double factorial ( int n )
{
double i, an = 1 ;
for( i=1;i <= n; i++)
an = an * i;
return ( an );
}
main()
{
char reply = 'N ';
do{ /**** 我想把这个do while 语句写成一个函数模块儿 ****/
double j, n=0, sn=0;
printf( "\n\n 1! + 2! + 3! + .... + n! = ?\n Please enter n: ");
scanf( "%lf ", &n );
for( j = 1; j <= n; j++ )
sn = sn + factorial(j);
printf( "1! + 2! + 3! + .... + n! = %lf \n if you wanna continue ,please enter y/Y : ", sn);
reply=getche();
}
while(reply == 'y ' || reply == 'Y ');
system( "pause ");
}
这个程序中的的那个do-while语句我经常要用到,于是我想,能不能把这个do-while语句写成一个void型的函数,放在一个叫做 reply.c 的文件中,以后在其他程序中直接调用就行了~~
于是,我模仿一个书本上的一个程序自己试着写了写,代码如下:
/****文件名为 reply.c****/
#include "stdio.h "
#include "conio.h "
void reply(void)
{
char reply= 'n ';
goon: printf( "if you wanna continue ,please enter y / Y: ");
reply = getche();
if( reply == 'y ' || reply == 'Y ')
goto goon;
}
我是想,如果我在下面的这个程序中:
#include "stdio.h "
main()
{
/****goon: ****/double i=0,tn=1, Pi=0,term=0 ,n=0 ;
printf( " accord this program , you can get approximation of Pi\n ");
printf( " Please enter the precision:\n n = ");
scanf( "%lf ",&n);
for( i=1 ; i <= n ; i++) /**/
{
term = 2*i*2*i/((2*i-1)*(2*i+1));
tn*=term;
}
Pi = 2*tn;
printf( "the approximation of Pi is %lf \n ", Pi);
/**** 调用函数reply()****/
system( "pause ");
}
我把注释符号去掉,就能调用函数了么?
然后,命运又一次捉弄了我。结果表明,我错了~
希望大家不吝赐教。
[解决办法]
晕,连goto都用上了-_-
goto只能在函数内部跳转的