以下程序的功能是求一元二次方程的实数根。
#include “stdio.h”
#include “math.h”
double *quadratic(double *factor)
{ static double result[2];
double delta;
delta=factor[1]*factor[1]-4*factor[0]*factor[2];
if(delta<0) return 0;
delta=sqrt(delta);
result[0]=(-factor[1]+delta)/(2*factor[0]);
result[1]=(-factor[1]-delta)/(2*factor[0]);
return result;
}
void main()
{ double fun[3],r; ★
printf(“Please enter three factors:”);
scanf(“%f%f%f”,&fun[0],&fun[1],&fun[2]); ★
r=quadratic(fun[3]); ★
if(r)
printf(“The solution is: X1=%lf\tX2=%lf\n”,r[0],r[1]);
else
printf(“No solution!\n”);
}
#include “stdio.h”
#include “conio.h”
#include “math.h”
double *quadratic(double *factor)
{ static double result[2];
double delta;
delta=factor[1]*factor[1]-4*factor[0]*factor[2];
if(delta<0) return 0;
delta=sqrt(delta);
result[0]=(-factor[1]+delta)/(2*factor[0]);
result[1]=(-factor[1]-delta)/(2*factor[0]);
return result;
}
void main()
{ double fun[3],*r;
printf(“Please enter three factors:”);
scanf(“%lf%lf%lf”,&fun[0],&fun[1],&fun[2]);
r=quadratic(fun);
if(r)
printf(“The solution is: X1=%lf\tX2=%lf\n”,r[0],r[1]);
else
printf(“No solution!\n”);
getch();
}
程序的执行结果是
Please enter three factors:1 3 2
The solution is: X1=-1.000000 X2=-2.000000
暂无评论内容