//此函数将计算一个数的就对值
#include
float absoluteValue (float x)
{
if ( x < 0 )
x = -x;
return x;
}
//此函数计算一个数的绝对值
float squreRoot (float x)
{
const float epsilon = 0.00001;
float guess =1.0;
while ( absoluteValue (guess * guess - x) >= epsilon );
guess = ( x/guess + guess)/2;
return guess;
}
int main()
{
int number;
printf("What number you want to square?\n");
float x;
scanf("%f",&x);
printf("%f\n" , squreRoot (x) );
return 0;
}
while那一句后面的分号去掉。
guess = ( x/guess + guess)/2;
->
guess = ( x/guess + guess)/2.0f;
while后边分号去掉,死循环了
while ( absoluteValue (guess * guess - x) >= epsilon );
应该是这行后面有了一个分号,于是,进入了死循环,
将分号去掉即可