編程題
請編寫一個函數(shù)float fun(double h),函數(shù)的功能是對變量h 中的值保留2位小數(shù),并對第三位進行四舍五入(規(guī)定h中的值為正數(shù))
例如:若h值為8.32433,則函數(shù)返回8.32,若h值為8.32533,則函數(shù)返回8.33
#include
#include
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;
}
編輯推薦:
2014年上半年計算機等級考試報名時間預(yù)告
2014年計算機等級考試考試時間預(yù)測
2013年9月計算機等級考試成績查詢時間及方式預(yù)告