#include "math.h"
/*求一元二次方程ax*x+bx+c=0的解*/
main()
{
float a,b,c,x1,x2,d
printf("请输入a:")
scanf("%f",&a)
printf("请输入b:")
scanf("%f",&b)
printf("请输入c:")
scanf("%f",&c)
d=b*b-4*a*c
if(d < 0)
printf("方程没有实数解。\n")
if (d==0)
{
x1=(-b)/(2*a)
printf("x1=%f\n",x1)
}
if (d>0)
{
x1=(-b+sqrt(d))/(2*a)
x2=(-b-sqrt(d))/(2*a)
printf("x1=%f,x2=%f\n",x1,x2)}
}
请输入a:12
请输入b:34
请输入c:4
x1=-0.122985,x2=-2.710348
Press any key to continue
#include "math.h"typedef struct {
double* coef
int n
}poly
double dfx(poly* p, double v) {
double t = v
double r = 0
double* coef = p->coef
int i
r += coef[1]
for (i = 2 i < p->n i++) {
r += coef[i]*i*t
t *= v
}
return r
}
double fx(poly* p, double v) {
double t = v
double r = 0
double* coef = p->coef
int i
r += coef[0]
for (i = 1 i < p->n i++) {
r += coef[i]*t
t *= v
}
return r
}
double polyroot(poly* p, double x0) {
double x = x0 - 1
int n = 0
while (fabs(x-x0) > 1e-12 && n++ < 100) {
double dx0 = dfx(p, x0)
x = x0
if (dx0)
x0 -= fx(p, x0)/dx0
else
x0 -= 0.1
printf("inter:%d f(%lf) = %lf\n", n, x0, fx(p,x0))
}
return x0
}
int main()
{
double c[4] = {-6, 3, -4, 2}
poly d = {c, 4}
polyroot(&d, 1.5)
return 0
}
#include<stdio.h>#include<math.h>
main()
{
double a,b,c,d,x1,x2
while(1){
scanf(" %lf%lf%lf",&a,&b,&c)
d=b*b-4*a*c
if(d<0) printf("此方程无实根!\n")
else if(d==0) printf("此方程有一个实根为:%lf\n",-b/(2*a))
else printf("此方程有两个实根为分别为:%lf\t%lf\n",(-b-sqrt(d))/(2*a),(-b+sqrt(d))/(2*a))
}
}
刚写的一个,如图所示,望采纳。。。。。。