2013年计算机二级C语言上机题库二十七_第2页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年5月2日
 改错题
  下列给定程序中,函数fun的功能是:根据以下公式求 值,并作为函数值返回,
  例如,给指定精度的变量eps输入0.005时应当输出pi=3.140578。
  请改正程序中的错误,使其能得出正确结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  试题 程序:#include <conio.h>
  #include <math.h>
  #include <stdio.h>
  double fun(double eps)
  {
  double s, t;
  int n = 1;
  s = 0.0;
  /********found********/
  t = 0;
  /********found********/
  while (t <= eps)
  {
  s += t;
  t = (t*n)/(2*n+1);
  n++;
  }
  return (s*2);
  } 
  main()
  {
  double x;
  printf("\nPlease enter a precision: ");
  scanf("%lf", &x);
  printf("\neps=%lf, Pi=%lf\n\n", x, fun(x));
  }
  第1处:t=0;应改为t=1.0;
  第2处:while(t<=eps)应改为while(t>=eps)

相关文章