c#计算一元二次方程根 abc由用户输入

1个回答

  • static void Main(string[] args)

    {

    Double A;

    Double B;

    Double C;

    Double T;

    Double X1;

    Double X2;

    Console.WriteLine("请输入3个数");

    try

    {

    A = Double.Parse(Console.ReadLine());

    B = Double.Parse(Console.ReadLine());

    C = Double.Parse(Console.ReadLine());

    if (A == 0)

    {

    Console.WriteLine("不是一元二次方程");

    Console.ReadLine();

    }

    else

    {

    T = B * B - (4 * A * C);

    if (T > 0)

    {

    X1 = ((-B) + Math.Sqrt(T)) / (2 * A);

    X2 = ((-B) - Math.Sqrt(T)) / (2 * A);

    Console.WriteLine(X1);

    Console.WriteLine(X2);

    Console.ReadLine();

    }

    if (T < 0)

    {

    X1 = (-B) / (2 * A);

    X2 = Math.Sqrt((-T)) / (2 * A);

    Console.WriteLine("X1 = {0} + {1}i",X1,X2);

    Console.WriteLine("X2 = {0} - {1}i",X1,X2);

    Console.ReadLine();

    }

    if (T == 0)

    {

    X1 = X2 = (-B) / (2 * A);

    Console.WriteLine(X1);

    Console.WriteLine(X2);

    Console.ReadLine();

    }

    }

    }

    catch (Exception ex)

    {

    Console.WriteLine(ex.Message);

    Console.ReadLine();

    }

    }