兔奶爸王冠:C语言编程高手请进

来源:百度文库 编辑:高校问答 时间:2024/04/29 04:42:23
求ax*x+bx+c=0根
怎么编程?

float f(float xx)
{float s;
s=-a*xx*xx-b*xx-c;
return(s);
}
p()
{printf(" ********************************\n"); }
p1()
{printf(" 二分法\n");}
main()
{

float x,a,b,ep;
int i=0;
char j;
p();
p1();
p();
printf("请输入 a、b 的值和精度:\n");
scanf("%f%f%f",&a,&b,&ep);
do {
i++;
x=(a+b)/2.0;
if(f(a)*f(x)<0) {b=x; j='+';}
else {a=x;j='-';}
printf("第 %d 次计算\ta=%f\tb=%f\tx=%f\tf(x) %c\t\n",i,a,b,x,j);
}
while((b-a)>ep);

}
ep是精度,
a、b代表x可能出现的范围
还有切线法:
#include <math.h>
p()
{printf("************ 切线法解方程 x^3-x-1=0 ***********\n");}
float f(float xx)
{float s;
s=xx*xx*xx-xx-1;
return(s);}
float f1(float yy)
{float t;
t=yy*yy*3-1;
return(t);}
main()
{
float x0,x1,ep;
int i=0;
p();
printf("\n");
printf("请输入初始近似根 x0 及精度:\n");
scanf("%f%f",&x0,&ep);
x1=x0-(f(x0)/f1(x0));
do{

i++;
x0=x1;
x1=x0-(f(x0)/f1(x0));
printf("第 %d 次计算\tx=%f\t\n",i,x1);
}while (fabs(x1-x0)>ep);
}

#include <stdio.h>
#include <math.h>

int main()
{
double a, b, c;
double sqrt_, exp = 0.0000001;

scanf("%lf%lf%lf",&a, &b, &c);
printf("The equation is: %lfx*x + %lfx + %lf = 0\n", a, b, c);

if(fabs(a-0) < exp)
{
if(fabs(b-0) < exp)
printf("Any number can be the root of the equation!");
else
printf("There is only one root: %lf!",-c/b);
}
else
{
sqrt_ = b*b - 4*a*c;
if(sqrt_ < 0)
printf("No root for this equation!");
else if(fabs(sqrt_-0) < exp)
printf("There is only one root: %lf!", -b/(2*a));
else
printf("There are two roots: %lf %lf!", (-b-sqrt(sqrt_))/2*a, (-b+sqrt(sqrt_))/2*a);

}
printf("\n");

return 0;
}
按方程的解法去做就是了哈!

要那全是不知