|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
// 求给定数的平方根
# include<stdio.h>
int main()
{
float sqrt_02(float x,float y); // 声明 sqrt_02 函数,求 x 的平方根,精度为 y
float x = 0,y = 0;
double z = 0;
double result;
scanf("%f,%f",&x,&y);
result = sqrt_02(x,y);
printf("%f 的平方根为:%04lf\n",x,result);
}
float sqrt_02(float x,float y)
{
float m = 1;
double p = 0;
double q = 0;
double result = 0;
double s = 0;
while (m * m < x) // 从自然数逼近
{
p = m;
m++;
}
while ((p + 0.5) * (p + 0.5) < x) // 从小数的十位逼近
{
p = p + 0.5;
q = p;
}
while ((q + 0.05) * (q + 0.05) < x) // 从小数的百位逼近
{
q = q + 0.05;
s = q;
}
while ((s + y) * (s + y) < x) // 从要求的精度位逼近
{
s = s + y;
}
result = s;
return 0;
}
此程序编译没有问题,返回却是0,请问,我该如何修改?谢谢!
将 return 0; 改为 return result
- #include <stdio.h>
- int main()
- {
- float sqrt_02(float x, float y); // 声明 sqrt_02 函数,求 x 的平方根,精度为 y
- float x = 0, y = 0;
- double z = 0;
- double result;
- scanf("%f,%f", &x, &y);
- result = sqrt_02(x, y);
- printf("%f 的平方根为:%04lf\n", x, result);
- }
- float sqrt_02(float x, float y)
- {
- float m = 1;
- double p = 0;
- double q = 0;
- double result = 0;
- double s = 0;
- while (m * m < x) // 从自然数逼近
- {
- p = m;
- m++;
- }
- while ((p + 0.5) * (p + 0.5) < x) // 从小数的十位逼近
- {
- p = p + 0.5;
- q = p;
- }
- while ((q + 0.05) * (q + 0.05) < x) // 从小数的百位逼近
- {
- q = q + 0.05;
- s = q;
- }
- while ((s + y) * (s + y) < x) // 从要求的精度位逼近
- {
- s = s + y;
- }
- result = s;
- return result;
- }
复制代码
|
|