当询问“是否要退出”问题/主要功能时,回答“ n”时,我需要代码才能一次又一次地问问题,直到用户完成程序为止。视需要重复的时间
int getNumTerms()
{
int n;
printf("Enter the number of terms: \n");
scanf("%d",&n);
return n;
}
double coshyper (double x, int n)
{
int i;
double sum=1, term=1;
for (i=1; i<n; i++)
{
term=(x*x/(2.0*(double)i*(2.0*(double)i-1.0)))*term;
sum=sum+term;
}
return sum;
}
int main()
{
char q;
printf("Do you wish to quit(y/n)?\n");
scanf("%c",&q);
if(q=='y')
{
printf("Program Terminated\n");
}
while(q=='n')
{
int n;
double x;
printf("Enter x: \n");
scanf("%lf",&x);
n=getNumTerms();
double result = coshyper(x,n);
printf("The value for cosh(%.3lf) for %d terms is %.6lf\n",x,n,result);
}
}
答案 0 :(得分:0)
您可以将主体更改为以下内容:
int main()
{
char q = 'n';
do{
if(q == 'y'){
printf("Program Terminated\n");
break;
}
if(q == 'n'){
int n;
double x;
printf("Enter x: \n");
scanf("%lf",&x);
n=getNumTerms();
double result = coshyper(x,n);
printf("The value for cosh(%.3lf) for %d terms is %.6lf\n",x,n,result);
}
else
{
printf("Invalid");
}
printf("Do you wish to quit(y/n)?\n");
scanf("%c",&q);
}while(1);
return 0;
}