有没有人解决一下用C++

img

这个题目请你们能做一下
我能力有限
用C++做我用的VS2022
还请大家一起分享一下各位的方法

一元二次方程,问答里太多了。随便搜一下就有

【相关推荐】




如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

一个实现如下:

参考链接:


C++ 实例 – 求一元二次方程的根 | 菜鸟教程 C++ 实例 - 求一元二次方程的根 C++ 实例 二次方程 ax2+bx+c = 0 (其中a≠0),a 是二次项系数,bx 叫作一次项,b是一次项系数;c叫作常数项。 x 的值为: 根的判别式 实例[mycode3 type='js'] #include #include using namespace std; int main() { float a, b, c, x1, x.. https://www.runoob.com/cplusplus/cpp-examples-quadratic-roots.html

C++规定精确到小数点后几位的方法(cout与printf两种情况)_如何让g++代码保留小数点后两位_alisr的博客-CSDN博客 程序经常被要求输出结果精确到小数点后几位,又不会c++格式化输出,代码中如何编写?`#include#include#include “stdlib.h”using namespace std;int main(){double PI=3.1415926;cout< https://blog.csdn.net/zhaoweining2001/article/details/115009731


#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
    // https://ask.csdn.net/questions/7873710/54039096?spm=1001.2014.3001.5504
    //  https://www.runoob.com/cplusplus/cpp-examples-quadratic-roots.html
    //  https://baike.baidu.com/item/%E4%B8%80%E5%85%83%E4%BA%8C%E6%AC%A1%E6%96%B9%E7%A8%8B%E6%B1%82%E6%A0%B9%E5%85%AC%E5%BC%8F/56066624?fr=aladdin
    //  https://blog.csdn.net/qq_41757528/article/details/126376583
    //  http://c.biancheng.net/view/1763.html
    double a,b,c,d,x1,x2;
    scanf("%lf%lf%lf",&a,&b,&c);
    d=b*b-4*a*c;
    
    // https://www.runoob.com/cplusplus/cpp-examples-quadratic-roots.html
    // https://blog.csdn.net/zhaoweining2001/article/details/115009731
    // 让cout输出小数,保留小数后五位 
    cout << fixed << setprecision(5);
  // https://blog.csdn.net/qq_41583263/article/details/113751810
    if(d>0) // 判别式大于0的情况  https://baijiahao.baidu.com/s?id=1722370687157957463&wfr=spider&for=pc  第三题 
    {
        
        cout<<"x1="<<((-b+sqrt(d))/(2*a))<<";x2="<<((-b-sqrt(d))/(2*a))<<endl;
    }
    else if(fabs(d)<=1e-15) // 判别式等于0的情况 https://baijiahao.baidu.com/s?id=1722370687157957463&wfr=spider&for=pc 第16题 
    {
        double p = -b/(2*a);
        cout<<"x1=x2="<<p<<endl;
    }
    // 判别式小于0的情况,有虚根 
    else if(d<0)  // https://qb.zuoyebang.com/xfe-question/question/aecfcb8e38f761d3fa704d540264593b.html
    { //  https://www.runoob.com/cplusplus/cpp-examples-quadratic-roots.html
        
        double p = -b/(2*a);
        double k = sqrt(-d)/(2*a);
        
    
        
        if(k==0){
            cout<<"x1="<<p<<"+"<<0<<"i;x2="<<p<<"-"<<0<<"i"<<endl;
        }else if (k<0){
            cout<<"x1="<<p<<k<<"i;x2="<<p<<"+"<<(-1*k)<<"i"<<endl;
            
        }else{
            cout<<"x1="<<p<<"+"<<k<<"i;x2="<<p<<"-"<<k<<"i"<<endl;
        }
        
        
    }
    
    return 0;
}

img