Hi all,
i need to change this code to use struct for all the types making the
equation and the solution
any ideas?
        class SecondDegreeEquationSolution
        {
                double[] solutions;

                public SecondDegreeEquationSolution()
                {
                        solutions = new double[0];
                }
                public SecondDegreeEquationSolution(double _solution)
                {
                        solutions = new double[1];
                        solutions[0] = _solution;
                }
                public SecondDegreeEquationSolution(double _solution1,double
_solution2)
                {
                        solutions = new double[2];
                        solutions[0] = _solution1;
                        solutions[1] = _solution2;
                }
                public int NumberOfSolutions
                {
                        get
                        {
                                return solutions.Length;
                        }
                }
                public double this[int n]
                {
                        get
                        {
                                if(solutions.Length  == 1 && n == 1)
                                        return (solutions[0]);
                                else if(solutions.Length == 2 && (n == 1 || n 
== 2))
                                        return (solutions[n-1]);
                                else return double.NaN;
                        }
                }
        }
        class SecondDegreeEquationSolver
        {
                public static SecondDegreeEquationSolution Solve
(SecondDegreeEquation eq)
                {
                        if(eq.A == 0)
                        {
                                return  new 
SecondDegreeEquationSolution(-eq.C/eq.B);
                        }
                        double d = eq.B * eq.B - 4 * eq.A * eq.C;
                        if(d<0)
                                return new SecondDegreeEquationSolution();
                        else if(d==0)
                                return new 
SecondDegreeEquationSolution(-eq.B/2*(eq.A));
                        else return new 
SecondDegreeEquationSolution((-eq.B+Math.Sqrt(d))/
2*eq.A,(-eq.B-Math.Sqrt(d))/2*eq.A );
                }
        }

        class SecondDegreeEquation
        {
                double a,b,c;
                public SecondDegreeEquation(double _a,double _b,double _c)
                {
                        a = _a;
                        b = _b;
                        c = _c;
                }
                public double A
                {
                        set
                        {
                                a = value;
                        }
                        get
                        {
                                return a;
                        }
                }
                public double B
                {
                        set
                        {
                                b = value;
                        }
                        get
                        {
                                return b;
                        }
                }
                public double C
                {
                        set
                        {
                                c = value;
                        }
                        get
                        {
                                return c;
                        }
                }
        }
        class MainClass
        {
                static void Main(string[] args)
                {
                        string strEndProgram;
                        Console.Out.WriteLine("This program solves equation of 
the form [ax²
+bx+c=0]");
                        bool endProgram = false;
                        do
                        {
                                Console.Out.Write("Please enter the value of 
a:");
                                double a = double.Parse(Console.In.ReadLine());
                                Console.Out.Write("Please enter the value of 
b:");
                                double b = double.Parse(Console.In.ReadLine());
                                Console.Out.Write("Please enter the value of 
c:");
                                double c = double.Parse(Console.In.ReadLine());

                                SecondDegreeEquation eq = new 
SecondDegreeEquation(a,b,c);
                                SecondDegreeEquationSolution solutions =
SecondDegreeEquationSolver.Solve(eq);
                                switch(solutions.NumberOfSolutions)
                                {
                                        case 0:
                                                System.Console.WriteLine("No 
Solution");
                                                break;
                                        case 1:
                                                Console.Out.WriteLine("The 
Solution is:{0}",solutions[1]);
                                                break;
                                        case 2:
                                                Console.Out.WriteLine("The 
Solution is:{0},{1}",solutions
[1],solutions[2] );
                                                break;
                                }
                                do
                                {
                                        Console.Out.Write("Would you like to 
solve another equation?(y/
n)");
                                        strEndProgram = Console.In.ReadLine();
                                }
                                while(strEndProgram!="n" && strEndProgram!="y");
                                if(strEndProgram.StartsWith("n"))
                                        endProgram = true;
                        }
                        while(!endProgram);
                }
        }

Reply via email to