编程题 请编写一个函数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;
}