Posted on 2007-07-01 00:40
停留的风 阅读(273)
评论(0) 编辑 收藏
#include <stdio.h>
float sqrt(float x)
{
const float epsilon= 0.0001;
float guess=1.0;
if(x<0)
{
printf("Negative argument to squareRoot.\n");
return -1.0;
}
while (((guess*guess)-x)>=epsilon)
{
guess=(x/guess+guess)/2.0;
}
return guess;
}
void qiuJie(int a,int b,int c)
{
float result=float(b*b-4*a*c);
if(result<0)
{
printf("方程的根是复数!\n");
}
else
{
float x1=(-b+sqrt(result))/(2*a);
printf("The first answer is %.2f.\n",x1);
float x2=(-b-sqrt(result))/(2*a);
printf("The second answer is %.2f.\n",x2);
}
}
int main(void)
{
int num1,num2,num3;
printf("Enter three numbers !\n");
scanf("%i",&num1);
scanf("%i",&num2);
scanf("%i",&num3);
qiuJie(num1,num2,num3);
return 0;
}
运行图: