1

这是代码,当我输入一个字母或使用逗号而不是点、fe 0,1 或 0,000045 等时它会循环。它不断循环如何避免这种情况?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double pi=0.0;
double eps = 0.0;
double summand=0.0;
int k=0;
char c;

int main(){
printf("Please enter EPS:\n");
scanf("%lf", &eps);

它从这里开始循环,但是当输入一个字母或使用逗号时,它会不断循环

 while ((eps>=1)||(eps<=0)) {printf("\nPlease enter a Precision Interval that is less than 1     and
 bigger than 0\n(do not use comma):\n");
    scanf("%lf", &eps);
 }

这是程序需要执行的实际代码。

  do{
    summand = 1/pow(16,k);
    pi += (double)summand*((double)4/(8*k+1)-(double)2/(8*k+4)-(double)1/(8*k+5)-(double)1
 /(8*k+6));
    printf("pi:%0.17lf EPS:%0.17lf summand:%0.17lf\n", pi, eps, summand);
    k++;
}while(summand>eps);

//pi = 3.141592653589793238462643;
printf("Pi is equal to %0.17lf\n\n\n\n", pi);
//-------------------------------------------------------------------------------

这里是重启程序与否。

do
{
printf("Do you want to restart the program? Y(es) or N(o)\n");
scanf(" %c",&c); // note the space
}while((c!='y')&&(c!='Y')&&(c!='n')&&(c!='N'));

   if ((c=='y')||(c=='Y')) { system("cls"); main();}
   else
    {
       return 0;
    }

  }
4

0 回答 0