求方程ax^2+bx+c=0的根,用三个函数gz,ez和sz分别求当b^2-4ac大于0、等于0和小于0时的根,并输出结果。从主函数输入a、b、c的值。(输出保留3位小数)用c语言编程
时间: 2023-11-29 13:15:06 浏览: 80
#include <stdio.h>
#include <math.h>
void gz(float a, float b, float c);
void ez(float a, float b, float c);
void sz(float a, float b, float c);
int main() {
float a, b, c;
printf("请输入方程的系数a、b、c:");
scanf("%f%f%f", &a, &b, &c);
if (b * b - 4 * a * c > 0) {
gz(a, b, c);
} else if (b * b - 4 * a * c == 0) {
ez(a, b, c);
} else {
sz(a, b, c);
}
return 0;
}
void gz(float a, float b, float c) {
float x1, x2;
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
printf("方程的根为:%.3f, %.3f\n", x1, x2);
}
void ez(float a, float b, float c) {
float x;
x = -b / (2 * a);
printf("方程的根为:%.3f\n", x);
}
void sz(float a, float b, float c) {
float realPart, imagPart;
realPart = -b / (2 * a);
imagPart = sqrt(-(b * b - 4 * a * c)) / (2 * a);
printf("方程的根为:%.3f+%.3fi, %.3f-%.3fi\n", realPart, imagPart, realPart, imagPart);
}
阅读全文