Ejemplo n.º 1
0
        /// <summary>
        /// Checks and corrects mistakes in equation.
        /// </summary>
        /// <exception cref="EquationIsEmptyException">
        /// The exception that is thrown when equation class has empty list of elements,
        /// because given text is empty or correction of incorrectly written equation text left no elements in the equation.
        /// </exception>
        private void Integrity_Check()
        {
            Elements_Enumerator Equation_Rator = new Elements_Enumerator(Element_Colection);

            Check_Integrity_Of_Equation_Begining(Equation_Rator);

            if (Equation_Rator.MoveNext() == false)
            {
                IElement Even_Preious_Element = null;

                do
                {
                    Check_Integrity_Of_Current_Element(Equation_Rator, Even_Preious_Element);

                    Even_Preious_Element = Equation_Rator.Previous_Element;
                } while (Equation_Rator.MoveNext() == false);
            }

            if (Element_Colection.Count < 1)
            {
                throw new EquationIsEmptyException();
            }

            while (Element_Colection[Element_Colection.Count - 1] is INot_At_The_End)
            {
                Element_Colection.RemoveAt(Element_Colection.Count - 1);
                if (Element_Colection.Count < 1)
                {
                    throw new EquationIsEmptyException();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes wrong elements at the begining of the equation and returns number of removed elements.
        /// </summary>
        /// <param name="Equation_Rator"></param>
        /// <exception cref="EquationIsEmptyException">
        private int Check_Integrity_Of_Equation_Begining(Elements_Enumerator Equation_Rator)
        {
            int removed_Elements = 0;

            if (Equation_Rator.MoveNext() == true)
            {
                throw new EquationIsEmptyException();
            }

            // If equation begings by operand[s] - removes it[them]
            while (Equation_Rator.Current_Element is INot_At_the_Begining)
            {
                if (Element_Colection.RemoveAt(Equation_Rator.Current_Index) == false)
                {
                    throw new EquationIsEmptyException();
                }
                removed_Elements++;
            }
            return(removed_Elements);
        }
        /// <summary>
        /// Finds mistakes of given elements and correct them in custom way.
        /// </summary>
        /// <param name="Equation_Rator">Enumerator of current list.</param>
        /// <param name="Even_Preious_Element">elemnt </param>
        private void Check_Integrity_Of_Current_Element(Elements_Enumerator Equation_Rator, IElement Even_Preious_Element)
        {
            IElement Current_Element  = Equation_Rator.Current_Element;
            IElement Previous_Element = Equation_Rator.Previous_Element;

            if (Even_Preious_Element is Bracket && Equation_Rator.Previous_Element is IOperand && Equation_Rator.Current_Element is Bracket)
            {
                if (Even_Preious_Element is Open_Bracket || Equation_Rator.Current_Element is Close_Bracket)
                {
                    Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                    Equation_Rator--;

                    Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                    int removed_Elements = Check_Integrity_Of_Equation_Begining(
                        new Elements_Enumerator(Element_Colection));

                    for (int i = 0; i <= removed_Elements; i++)
                    {
                        Equation_Rator--;
                    }
                }
            }
            else if (Current_Element is Open_Bracket && Previous_Element is INot_Outside_The_Open_Bracket)
            {
                Element_Colection.Insert(Equation_Rator.Current_Index, new Multiplication());

                Equation_Rator--;
            }
            else if (Previous_Element is Operand && Current_Element is Operand)
            {
                Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                Equation_Rator--;
            }
            else if (Previous_Element is Open_Bracket && Current_Element is Close_Bracket)
            {
                Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                Equation_Rator--;

                Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                Equation_Rator--;
            }
            else if (Previous_Element is Close_Bracket && Current_Element is INot_Outside_The_Close_Bracket)
            {
                Element_Colection.Insert(Equation_Rator.Current_Index, new Multiplication());

                Equation_Rator--;
            }
            else if (Previous_Element is Open_Bracket && Current_Element is INot_Intside_Bracket)
            {
                Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                Equation_Rator--;
            }
            else if (Current_Element is Close_Bracket && Previous_Element is INot_Intside_Bracket)
            {
                Element_Colection.RemoveAt(Equation_Rator.Current_Index - 1);

                Equation_Rator--;
            }
        }