Ejemplo n.º 1
0
 public void SetUp()
 {
     _correctResult = new RootsResult();
     {
         _correctResult.X1    = -2;
         _correctResult.X2    = 0;
         _correctResult.Delta = 36;
         _max_y = Double.MaxValue;
         _max_x = Double.MaxValue;
     }
 }
Ejemplo n.º 2
0
        override public RootsResult CalculateRoots(int a, int b, int c)
        {
            // Подсчитываем корни уравнения, как если бы оно было квадратным.

            RootsResult quadResult = base.CalculateRoots(a, b, c);

            PropertyInfo[] rootProperties = quadResult.GetRootProperties(); // Список свойств, обеспечивающих доступ к корням уравнения.

            // Перебираем корни.
            List <double> roots = new List <double>(); // Список, необходимый для хранения новых корней.

            foreach (PropertyInfo rootProperty in rootProperties)
            {
                if (rootProperty.Name.ToString() == "NumOfRoots")
                {
                    continue;                                                  // Количество корней нам сейчас не нужно обрабатывать.
                }
                double root = (double)rootProperty.GetValue(quadResult, null); // Получаем значение текущего свойства.
                if (root < 0)
                {
                    continue;
                }
                if (root == 0)
                {
                    roots.Add(0);
                    continue;
                }

                roots.Add(Math.Sqrt(root));
                roots.Add(-Math.Sqrt(root));
            }
            if (roots.Count == 0)
            {
                return(new NoRoots());
            }
            RootsResult result = (RootsResult)Activator.CreateInstance(PossibleRoots[roots.Count], roots);

            return(result);
        }
Ejemplo n.º 3
0
        public void OutputRoots(RootsResult result)
        {
            Type resultType = result.GetType();

            if (resultType.Name.ToString() == "NoRoots")
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("Корней нет!");
                Console.ResetColor();
                return;
            }

            PropertyInfo[] rootProperties = result.GetRootProperties();

            Console.ForegroundColor = ConsoleColor.Green;
            foreach (PropertyInfo rootProperty in rootProperties)
            {
                var root = rootProperty.GetValue(result, null);
                Console.WriteLine(root);
            }
            Console.ResetColor();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Вывод корней
        /// </summary>
        public void PrintRoots(double a, double b, double c)
        {
            RootsResult result = this.CalculateRoots(a, b, c);

            Console.Write("Коэффициенты: a={0}, b={1}, c={2}. ", a, b, c);
            string resultType = result.GetType().Name;

            if (resultType == "NoRoots")
            {
                Console.WriteLine("Корней нет.");
            }
            else if (resultType == "OneRoot")
            {
                OneRoot rt1 = (OneRoot)result;
                Console.WriteLine("Один корень {0}", rt1.root);
            }
            else if (resultType == "TwoRoots")
            {
                TwoRoots rt2 = (TwoRoots)result;
                Console.WriteLine("Два корня {0} и {1}", rt2.root1, rt2.root2);
            }
        }
Ejemplo n.º 5
0
        public void PrintRoots(double a, double b, double c)
        {
            RootsResult result = this.CalculateRoots(a, b, c);

            Console.Write("Коэффициенты: a={0}, b={1}, c={2}. ", a, b, c);
            string resultType = result.GetType().Name;

            if (resultType == "NoRoots")
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\nКорней нет.");
            }
            else if (resultType == "OneRoot")
            {
                OneRoot rt1 = (OneRoot)result;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\nОдинкорень {0}", rt1.root);
            }
            else if (resultType == "TwoRoots")
            {
                TwoRoots rt2 = (TwoRoots)result;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\nДва корня: {0} и {1}", rt2.root1, rt2.root2);
            }
            else if (resultType == "ThreeRoots")
            {
                ThreeRoots rt3 = (ThreeRoots)result;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\nТри корня: {0}, {1}, {2}", rt3.root1, rt3.root2, rt3.root3);
            }
            else if (resultType == "FourRoots")
            {
                FourRoots rt4 = (FourRoots)result;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\nЧетыре корня: {0}, {1}, {2}, {3}", rt4.root1, rt4.root2, rt4.root3, rt4.root4);
            }
        }
Ejemplo n.º 6
0
        static public void Main(string[] args)
        {
            Console.WriteLine("Автор программы:\nПакало Александр Сергеевич РТ5-31Б");

            int a, b, c; // Коэффициенты уравнения.

            Console.WriteLine("Введите коэффициенты уравнений вида ax^2 + bx + c, ax^4 + bx^2 + c.");

            a = InitCoef(args[0], "a");
            b = InitCoef(args[1], "b");
            c = InitCoef(args[2], "c");

            Console.WriteLine("Корни квадратного уравнения:");
            QuadraticEquationSolver quadEq    = new QuadraticEquationSolver();
            RootsResult             quadRoots = quadEq.CalculateRoots(a, b, c);

            quadEq.OutputRoots(quadRoots);

            Console.WriteLine("Корни биквадратного уравнения:");
            BiquadraticEquationSolver biquadEq = new BiquadraticEquationSolver();
            RootsResult biquadRoots            = biquadEq.CalculateRoots(a, b, c);

            biquadEq.OutputRoots(biquadRoots);
        }
Ejemplo n.º 7
0
 public void TearDown()
 {
     _correctResult = null;
     _max_y         = _max_x = 0;
 }