自己写的一个开平方的函数,相当于sqrt( )函数,看看能不能简化一下
#include <stdio.h>void main()
{
double x, a, b, i;
printf("please input a number: ");
scanf("%lf",&x);
if(x > 4)
{
i = 1.0;
b = 2.0;
}
else if(x>1 && x<4)
{
i = 0.1;
b = 1.0;
}
else if(x>0 && x<1)
{
i = 0.1;
b = 0.0;
}
else if(x == 4)
{
b = 2;
}
else if(x == 1)
{
b = 1;
}
else if(x == 0)
{
b = 0;
}
else
{
printf("a wrong number.\n");
}
while(1)
{
a = b * b;
if(a == x)
{
break;
}
else if(a > x)
{
b = b - i;
i = i / 10;
b = b + i;
}
else
{
b = b + i;
}
if(i <=0.000001)
{
break;
}
}
printf("%lf\n",b);
}
用switch..
页:
[1]