代码:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
double discriminant, root1, root2;
printf(“请输入一元二次方程的系数:n”);
printf(“a: “);
scanf(“%lf”, &a);
printf(“b: “);
scanf(“%lf”, &b);
printf(“c: “);
scanf(“%lf”, &c);
discriminant = b * b – 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b – sqrt(discriminant)) / (2 * a);
printf(“方程有两个实根:root1 = %.2lf, root2 = %.2lfn”, root1, root2);
}
else if (discriminant == 0) {
root1 = -b / (2 * a);
printf(“方程有一个实根:root = %.2lfn”, root1);
}
else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf(“方程有两个复根:root1 = %.2lf + %.2lfi, root2 = %.2lf – %.2lfin”,
realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
原文地址:https://blog.csdn.net/hdz_wiz_csdn/article/details/135886729
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_63153.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!