如何显示二次公式

时间:2017-04-15 19:27:19

标签: c# math quadratic square-root

所以我用C#编写了一个二次方公式程序,如何进行二次公式程序并对其进行修改,以便程序正确显示解的数量。

如果有两个解决方案,

(x - x1)(x - x2) = 0

如果只有一个解决方案,

(x - x0)^2 = 0

如果没有解决方案,

没有解决方案。

这是程序,如果有人可以为我展示解决方案,这将是非常好的,我真的不知道如何去做。

using System;

namespace quadraticequation
{
class MainClass
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Enter a number for a"); //ask the user for information
        double a = double.Parse(Console.ReadLine()); //Gets a from the user
        Console.WriteLine("Enter a number for b"); //asks the user for information
        double b = double.Parse(Console.ReadLine()); //Gets b from the user
        Console.WriteLine("Enter a number for c"); //asks the user for information
        double c = double.Parse(Console.ReadLine()); //Gets c from the user

        //double.Parse --> is used to convert a number or string to a double.
        //Console.ReadLine() --> is used to take the input from the user.

        //We call a function here
        Quadratic(a, b, c);

    }
    //We need to create a new function

    public static void Quadratic(double a, double b, double c) 
    {
        double deltaRoot = Math.Sqrt(b * b - 4 * a * c); //Math.Sqrt takes the square root of the number


        if (deltaRoot >= 0)  // we use an if statement here to handle information
        {
            double x1 = (-b + deltaRoot) / 2 * a;  //We write the information for x1 here
            double x2 = (-b - deltaRoot) / 2 * a;  //We write the information for x2 here
            Console.WriteLine("x1 = " + x1 + " x2 = " + x2); //we use this to write the roots



        }
        else  // we use an else statement so that we dont return an error when there are no roots
        {
            Console.WriteLine("There are no roots");

        }



    }
}
}

1 个答案:

答案 0 :(得分:3)

我认为您必须查看二级公式解决方案 -skills。你写道:

double deltaRoot = Math.Sqrt(b * b - 4 * a * c);

但测试实际上是 b 2 -4×a×c 是否大于或等于零:实际上这实际上是我们检查的原因:因为我们不能采用负数的平方根(是的,存在复杂的数字可以取负数的平方根,但现在让我们忽略它。)

所以解决方案就是把它写成:

public static void Quadratic(double a, double b, double c) {
    double delta = b*b-4*a*c; //only delta

    if (delta > 0) {
        double deltaRoot = Math.Sqrt(delta);
        double x1 = (-b + deltaRoot) / (2 * a);  //We write the information for x1 here
        double x2 = (-b - deltaRoot) / (2 * a);  //We write the information for x2 here
        Console.WriteLine("x1 = " + x1 + " x2 = " + x2); //we use this to write the roots
    } else if(delta == 0) {
        double x1 = -b/(2*a);
        Console.WriteLine("x1 = " + x1); //we use this to write the roots
    } else {
        Console.WriteLine("There are no roots");
    }

}

您还必须撰写(-b + deltaRoot) / (2*a)(2*a)),否则您将将<{em} (-b + deltaRoot) / 2a相乘。

最后要注意的是,与浮点的相等比较非常棘手,因此delta == 0经常会失败,因为结果可能是1e-20 - ish,这在执行浮点运算时只是一个错误。因此,使用一小部分值可能更好。

这给出了:

csharp> MainClass.Quadratic(1,1,1);
There are no roots
csharp> MainClass.Quadratic(1,1,0); 
x1 = 0 x2 = -1
csharp> MainClass.Quadratic(1,0,0); 
x1 = 0