用C ++求解二次方程

时间:2009-05-22 14:12:48

标签: c++ math equation quadratic

我正在尝试用C ++编写一个函数,用二次方程求解X.这是我最初写的,只要答案没有复杂的数字,这似乎有效:

float solution1 = (float)(-1.0 * b) + (sqrt((b * b) - (4 * a * c)));
solution1 = solution1 / (2*a);

cout << "Solution 1: " << solution1 << endl;

float solution2 = (float)(-b) - (sqrt((b*b) - (4 * a * c)));
solution2 = solution2 / (2*a);
cout << "Solution 2: " << solution2;

例如,如果我使用等式:x ^ 2 - x - 6,我正确得到解3,-2。

我的问题是如何解释复数...例如,给出等式:

x ^ 2 + 2x + 5

手工解决,我会得到-1 + 2i,-1 - 2i。

嗯,我猜两个问题,我可以更好地编写上面的内容,还可以说明复数吗?

感谢您的帮助!

7 个答案:

答案 0 :(得分:20)

所有这一切的重要说明。这些回答和原始问题中显示的解决方案并不健全。

众所周知的解决方案( - b + - sqrt(b ^ 2 - 4ac))/ 2a ac 非常非常强大的计算中由于一个人正在减去两个非常相似的值,所以很少得到 b ^ 2 。最好使用鲜为人知的解决方案 2c /( - b - + sqrt(b ^ 2 -4ac))作为另一个根。

强大的解决方案可以计算为:

temp = -0.5 * (b + sign(b) * sqrt(b*b - 4*a*c);
x1 = temp / a;
x2 = c / temp;

使用符号(b)可确保我们不会减去两个相似的值。

对于OP,请修改复数,如其他海报所示。

答案 1 :(得分:8)

这样的事情会起作用:

struct complex { double r,i; }
struct pair<T> { T p1, p2; }

pair<complex> GetResults(double a, double b, double c)
{
  pair<complex> result={0};

  if(a<0.000001)    // ==0
  {
    if(b>0.000001)  // !=0
      result.p1.r=result.p2.r=-c/b;
    else
      if(c>0.00001) throw exception("no solutions");
    return result;
  }

  double delta=b*b-4*a*c;
  if(delta>=0)
  {
    result.p1.r=(-b-sqrt(delta))/2/a;
    result.p2.r=(-b+sqrt(delta))/2/a;
  }
  else
  {
    result.p1.r=result.p2.r=-b/2/a;
    result.p1.i=sqrt(-delta)/2/a;
    result.p2.i=-sqrt(-delta)/2/a;
  }

  return result;
}

通过这种方式,您可以以类似的方式获得实际结果和复杂结果(实际结果只是将虚部设置为0)。看起来更美观一点!

编辑:为delta事物修复,并添加了对a = 0等退化情况的检查。不眠之夜!

答案 2 :(得分:4)

你或多或少拥有它,只需检查平方根内的部分是否为负数,然后在减少中单独跟踪它。

答案 3 :(得分:3)

作为旁注: 分割时,请务必检查分母是否为零。并记住浮点数使用类似的东西:

#inlcude <float.h>
if (fabs(a) < FLT_EPSILON)
    then a is considered 0

答案 4 :(得分:3)

您基本上只需使用std::complex<float>代替float即可获得对复杂数字的支持。

答案 5 :(得分:1)

从Blindy那里捏造这个想法:

typedef std::complex<double> complex;
using std::pair;
pair<complex> GetResults(double a, double b, double c)
{
  double delta=(b*b-4*a*c);
  double inv_2a = 1/2/a;
  if(delta >= 0) {
    double root = sqrt(delta);
    return std::make_pair(
        complex((-b-root)*inv_2a),
        complex((-b+root)*inv_2a);
  } else {
    double root = sqrt(-delta);
    return std::make_pair(
        complex(-b*inv_2a, -root*inv_2a)),
        complex(-b*inv_2a, +root*inv_2a)));
  }
}

答案 6 :(得分:-1)

我在没有使用&#39; math.h&#39;的情况下尝试了该程序。标题也尝试了不同的逻辑...但我的程序只能回答那些系数为&#39; x square&#39;的二次方程式。作为一个.....和&#39; x&#39;的系数可以表示为两个数字的相加,这两个数字是常数项的因子。 例如。 x square + 8x + 16;     x square + 7x + 12; 这里8 = 4 + 4&amp; 16 = 4 * 4;这里x的系数可以表示为两个数的相加,它们是常数项16的因子...... 我自己并不完全满意,但尝试了不同的东西,没有使用解决二次方程的公式。 代码是;

        #include<iostream.h>
        #include<conio.h>
         class quadratic
              {
                int b,c ;
                float l,k;
                public:
               void solution();
              };
        void quadratic::solution()
             {
                 cout<<"Enter coefficient of x and the constant term of the quadratic eqn where coefficient of x square is one";
                 cin>>b>>c;

                 for(l=1;l<b;l++)
                  {
                   for(k=1;k<b;k++)
                    {
                     if(l+k==b&&l*k==c)
                        {
                          cout<<"x="<<-l<<"\t"<<"or"<<"\t"<<"x="<<-k;
                          cout<<"\n";
                         }
                    }
                }
            }
              void main()
                 {
                  quadratic a;
                   clrscr();
                  a.solution();
                  getch();
                 }
相关问题