#include
#include
#include
using namespace std;
int main()
{
double a=1, b=1, c=1, d=1;
cout << "Enter a,b,c:";
d = b * b - 4 * a * c;
cin >> a >> b >> c;
if (0==a) {if(0==b)cout<<"方程不成立\n";
else cout<< "x =" << fixed << setprecision(2) << -c / b << endl;}</span><br /><span style="display:inline-block;text-indent:2em;"> else if (d >= 0) {
cout << "x1 =" << fixed << setprecision(2) << (-b + sqrt(d)) / (2 * a) << endl;</span><br /><span style="display:inline-block;text-indent:2em;"> cout << "x2 =" << fixed << setprecision(2) << (-b - sqrt(d)) / (2 * a) << endl;</span><br /><span style="display:inline-block;text-indent:2em;"> }
else {
cout << "x1 =" << fixed << setprecision(2) << -b / (2 * a) << " + " << sqrt(-d) / (2 * a) << "i" << endl;
cout << "x2 =" << fixed << setprecision(2) << -b / (2 * a) << " - " << sqrt(-d) / (2 * a) << "i" << endl;
}
return 0;
}

为什么是错的求解答刚刚学
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- qzjhjxj 2022-10-15 22:47关注
修改如下,供参考:
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { double a=1, b=1, c=1, d=1; cout << "Enter a,b,c:"; cin >> a >> b >> c; //修改 d = b * b - 4 * a * c; //修改 if (0 == a) { if(0 == b) cout<<"方程不成立\n"; else cout<< "x =" << fixed << setprecision(2) << -c / b << endl; } else if (d >= 0) { cout << "x1 =" << fixed << setprecision(2) << (-b + sqrt(d)) / (2 * a) << endl; cout << "x2 =" << fixed << setprecision(2) << (-b - sqrt(d)) / (2 * a) << endl; } else { cout << "x1 =" << fixed << setprecision(2) << -b / (2 * a) << " + " << sqrt(-d) / (2 * a) << "i" << endl; cout << "x2 =" << fixed << setprecision(2) << -b / (2 * a) << " - " << sqrt(-d) / (2 * a) << "i" << endl; } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用