谁来改一下这个C++……

来源:百度知道 编辑:UC知道 时间:2024/06/06 18:46:23
就是做四舍五入的
#include<stdio.h>
int round(float x,int n)
{ float z;
long i;
i=pow(10,n);
z=(long)(x*i+0.5)/(float)i;
return i;
}
main()
{ float x,y;
int n;
printf("input x n\n");
scanf("%f%d",&x,&n);
y=round(x,n);
printf("x=%f, round(%f)=%f\n",x,x,y);
}

#include<stdio.h>
#include<math.h> //加这个头文件。pow需要用到
int round(float x,int n)
{ float z;
long i;
i=pow(10,n);
z=(long)(x*i+0.5)/(float)i;
return i;
}
main()
{ float x,y;
int n;
printf("input x n\n");
scanf("%f%d",&x,&n);
y=(float)round(x,n); //y是float型。round函数返回的是int 需要强制转换一下
printf("x=%f, round(%f)=%f\n",x,x,y);
}

pow函数好像还得定义吧。。。

pow函数要一个头文件#inlclude<math.h>

z=(long)(x*i+0.5)/(float)i;
return i;
这函数真奇怪, 算了一圈的z然后返回了个别的东西。。