A C# class to solve a quadratic equation

ddanbe 0 Tallied Votes 777 Views Share

Solve an equation of the form Ax^2 + Bx + C = 0.

The class is commented, but if you have any questions don't be affraid to ask.
You may exercise this class in a console application with the following snippet :

QuadraticEquation z = new QuadraticEquation(1, 3, 3);
            z.Solve();
            Console.WriteLine("D={0}",z.Discriminant());
            Console.WriteLine(z.Root1RealPart);
            Console.WriteLine(z.Root2RealPart);
            Console.WriteLine(z.Root1ImagPart);
            Console.WriteLine(z.Root2ImagPart);
namespace MathClasses
{
    /// <summary>
    /// quadratic equation Ax^2 + Bx + C = 0
    /// </summary>
    class QuadraticEquation
    {
        // ******************************************************************
        // A quadratic equation always has two roots
        // ******************************************************************
        private struct Root
        {
            public double R1_Re;
            public double R1_Im;
            public double R2_Re;
            public double R2_Im;
        }
        private Root MyRoot;

        // ******************************************************************
        // Keep the coefficient private we get and set them via properties
        // ******************************************************************
        private double cA = 0.0;
        private double cB = 0.0;
        private double cC = 0.0;

        // ******************************************************************
        // Constructors
        // ******************************************************************
        public QuadraticEquation(){}

        public QuadraticEquation(double A, double B, double C)
        {
            cA = A;
            cB = B;
            cC = C;
        }

        // ******************************************************************
        // Properties
        // ******************************************************************
        public double Acoefficient { get { return cA; } set { cA = value; } }
        public double Bcoefficient { get { return cB; } set { cB = value; } }
        public double Ccoefficient { get { return cC; } set { cC = value; } }
        public double Root1RealPart { get { return MyRoot.R1_Re; } }
        public double Root1ImagPart { get { return MyRoot.R1_Im; } }
        public double Root2RealPart { get { return MyRoot.R2_Re; } }
        public double Root2ImagPart { get { return MyRoot.R2_Im; } }

        // ******************************************************************
        // Return the discriminant b*b-4*a*c
        // ******************************************************************
        public double Discriminant()
        {
            return cB * cB - 4 * cA * cC;
        }

        // ******************************************************************
        // Solve the equation and fill the roots struct with the results
        // ******************************************************************
        public void Solve()
        {
            // Preliminary calculations to avoid numbercrunching overhead
            double D = Discriminant();
            double twoA = cA + cA;
            double B2A = -cB / twoA;

            if (D == 0)             // roots are equal and real
            {
                MyRoot.R1_Re = MyRoot.R2_Re = B2A;
                MyRoot.R1_Im = MyRoot.R2_Im = 0.0;
            }
            else if (D > 0)         // roots are distinct and real
            {
                MyRoot.R1_Re = B2A + Math.Sqrt( D ) / twoA;
                MyRoot.R2_Re = B2A - Math.Sqrt( D ) / twoA;
                MyRoot.R1_Im = 0.0;
                MyRoot.R2_Im = 0.0;
            }
            else if (D < 0)         // no real roots, 2 complex conjugate roots
            {
                MyRoot.R1_Re = MyRoot.R2_Re = B2A;
                D = -D;
                MyRoot.R1_Im = Math.Sqrt(D) / twoA;
                MyRoot.R2_Im = -Math.Sqrt(D) / twoA;
            }        
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.