无敌源崽 2022-10-15 22:07 采纳率: 100%
浏览 19
已结题

为什么是错的求解答刚刚学

#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;
}

  • 写回答

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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 已采纳回答 10月15日
  • 创建了问题 10月15日

悬赏问题

  • ¥15 python实现CAD识图
  • ¥15 如何实现在renpy中创建并应用不同大小尺寸对话框?
  • ¥15 table表格有一列是固定列 滑动到最下面时 固定的那一列有几行没显示出来
  • ¥20 单细胞数据注释时报错
  • ¥20 dify工作流分析sql查询结果
  • ¥15 vscode编译ros找不到头文件,cmake.list文件出问题,如何解决?(语言-c++|操作系统-linux)
  • ¥15 通过AT指令控制esp8266发送信息
  • ¥15 有哪些AI工具提供可以通过代码上传EXCEL文件的API接口,并反馈分析结果
  • ¥15 二维装箱算法、矩形排列算法(相关搜索:二维装箱)
  • ¥20 nrf2401上电之后执行特定任务概率性一直处于最大重发状态