设计一个代码,输入一个一元二次方程的系数abc,若a为0,求x的值,若a不为零,delta大于等于零时求实实数解,若delta小于零给出无实数解的信息
时间: 2023-05-29 11:05:42 浏览: 96
```
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0) {
cout << "方程有无数解" << endl;
} else {
cout << "方程无解" << endl;
}
} else {
cout << "方程的解为:" << -c / b << endl;
}
} else {
double delta = b * b - 4 * a * c;
if (delta >= 0) {
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
cout << "方程的实数解为:" << x1 << "和" << x2 << endl;
} else {
cout << "方程无实数解" << endl;
}
}
return 0;
}
```
阅读全文