C语言的一个题请帮忙

来源:百度知道 编辑:UC知道 时间:2024/07/01 07:51:40
请编一个函数float fun(double c),函数的功能是对变量c中的值保留2位小数,并对第三位进行四舍五入(规定c中的值为正数)。
例如,若c值为1.234,则函数返回1.230000;若c值为1.235,则函数返回1.240000。
注意:部分源程序给出如下。
请勿改动主函数main与其他函数中的任何内容,仅在fun函数的花括号中填入所编写的若干语句。
# include <stdio.h>
# include <conio.h>
float fun(float c)
{

}
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("outfile.dat","w");
fprintf(out,"%f",fun(3.141593));
fclose(out);
}
请各位大大帮帮忙呀!

# include <stdio.h>
# include <conio.h>
float fun(float c)
{ int i,j;
i=1000*c;
if(i%10>=5) return (i/10+1)/100.0;
else return i/10/100.0;
}
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("outfile.dat","w");
fprintf(out,"%f",fun(3.141593));
fclose(out);
getch();
}


float fun(float x)
{
int t1 = x * 100;
int t2 = int(x * 1000) % 10; //得到小数点后第三位数
if(t2 >= 5)
t1++;
return t1 / 100.0;

}