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

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年4月25日
 编程题
  请编写一个函数float fun(double h),函数的功能是对变量h 中的值保留2位小数,并对第三位进行四舍五入(规定h中的值为正数)
  例如:若h值为8.32433,则函数返回8.32,若h值为8.32533,则函数返回8.33
  #include <stdio.h>
  #include <conio.h>
  float fun ( float h )
  {
  }
   main( )
  {
  float a;
  FILE *out;
  printf ( "Enter a: ");
  scanf ( "%f", &a );
  printf ( "The original data is: ");
  printf ( "%f \n\n", a );
  printf ( "The result : %f\n", fun ( a ) );
  out = fopen("out.dat", "w");
  fprintf(out, "%f" , fun(3.141593));
  fclose(out);
  } 
  答案是:
  float fun(float h)
  {
  long t;
  float s;
  h=h*1000;
  t=(h+5)/10;
  s=(float)t/100.0;
  return s;
  }
首页 1 2 3 尾页

相关文章