/// <summary> /// Constructs given string to equation elements list, finds mistakes and correct them /// </summary> /// <param name="eq">String that will be convert to equation elements list.</param> /// <param name="ns">Numeral system </param> public Equation(string eq, Numeral_System ns, string[] Commas_Type) { the_Equation = eq; _Numerical_System = ns; _Parser = new Parser(_Numerical_System, Commas_Type); Element_Selector(); try { Integrity_Check(); } catch (EquationIsEmptyException) { IMessage_Box Message_Box = Factory.Get_Message_Box; Message_Box.Pop_Up("Given equation to solve is empty or correction of incorrectly written equation " + "text left no elements in the equation."); Element_Colection = new Equation_Elements { new Number(0) }; } Bracket_Pairer(); }
/// <summary> /// Solves given bracket and returns solved number as result. /// </summary> /// <param name="elements">Equation elements sorce list.</param> /// <param name="Current_Bracket_Pair">Pairs of brackets that inner equation is to be solved.</param> /// <returns></returns> private Number Inner_Brackets_Solver(Equation_Elements elements, Bracket_Pair Current_Bracket_Pair) { foreach (Bracket_Pair Inner_Bracket_pair in Current_Bracket_Pair.Inner_Brackets) { int Index_Of_Open_Bracket = elements.IndexOf(Inner_Bracket_pair.Open_Bracket); int Lenght_Of_Current_Bracket = elements.IndexOf(Inner_Bracket_pair.Close_Bracket) - Index_Of_Open_Bracket + 1; Number Solved_Inner_Bracket = Inner_Brackets_Solver( new Equation_Elements(elements.GetRange(Index_Of_Open_Bracket + 1, Lenght_Of_Current_Bracket - 2)), Inner_Bracket_pair ); Exchange_Solved_Range_to_Calculated_Value( elements, Index_Of_Open_Bracket, Lenght_Of_Current_Bracket, Solved_Inner_Bracket); } Number Result_Of_Current_Bracket; try { Result_Of_Current_Bracket = No_Bracket_Solver(elements); } catch (EquationIsEmptyException) { IMessage_Box Message_Box = Factory.Get_Message_Box; Message_Box.Pop_Up($"Something went wrong equation or part of eqaution: {elements.Recreate_Equation()} " + $"had been solved improperly so \"0\" been pushed as result of shown equation.{Environment.NewLine}" + "If there is an error in input eqution correct or try write write equation in other way"); Result_Of_Current_Bracket = new Number(0); } catch (EquationNotSolvedProperlyException e) { IMessage_Box Message_Box = Factory.Get_Message_Box; Message_Box.Pop_Up($"Something went wrong equation or part of eqaution: {elements.Recreate_Equation()} " + $"had been solved as \"{e.Message}\" so \"0\" been pushed as result of shown equation.{Environment.NewLine}" + "If there is an error in input eqution correct or try write write equation in other way"); Result_Of_Current_Bracket = new Number(0);; } return(Result_Of_Current_Bracket); }
/// <summary> /// Solves given equation elements returns solved number as result. /// </summary> /// <param name="elements">Equation elements sorce list.</param> /// <exception cref="EquationIsEmptyException">Thrown when solving equation after been solved is left empty.</exception> /// <exception cref="EquationNotSolvedProperlyException">Thrown when solving equation after been solved is not composed of one number element.</exception> private Number No_Bracket_Solver(Equation_Elements elements) { foreach (IOperand[] operand_Level in Order_Of_Operations.Operands) { for (int operand_index = 0; operand_index < elements.Count; operand_index++) { if (elements[operand_index] is IOperand Equation_Operand) { foreach (IOperand operand_of_Level in operand_Level) { if (operand_of_Level == Equation_Operand) { INumber First_Number = (INumber)elements[operand_index - 1]; INumber Seccond_Number = (INumber)elements[operand_index + 1]; INumber Result_Number = _Calculator.Operand_Selector(Equation_Operand, First_Number, Seccond_Number); Exchange_Solved_to_Simple_Calculated_Value(elements, operand_index, Result_Number); operand_index--; } } } } } if (elements.Count < 1) { throw new EquationIsEmptyException(); } else if (elements.Count == 1 && elements[0] is Number Result) { return(Result); } else { throw new EquationNotSolvedProperlyException(elements.Recreate_Equation()); } }
/// <summary> /// Exchange given range of equation elements to single number. /// </summary> /// <param name="elements">Equation elements sorce list.</param> /// <param name="operand_index">The zero-based index at which item should be inserted.</param> /// <param name="count">The number of elements to remove.</param> /// <param name="Result_Number">Result to insert.</param> private void Exchange_Solved_Range_to_Calculated_Value( Equation_Elements elements, int operand_index, int count, Number Result_Number) { elements.ReplaceRange(operand_index, count, Result_Number); }
/// <summary> /// Exchange single operation to solve result. /// </summary> /// <param name="elements">Equation elements sorce list.</param> /// <param name="operand_index">Index of operator to exchange.</param> /// <param name="Result_Number">Result to insert.</param> private void Exchange_Solved_to_Simple_Calculated_Value( Equation_Elements elements, int operand_index, INumber Result_Number) { elements.ReplaceRange(operand_index - 1, 3, Result_Number); }
/// <summary> /// Constructs elements' enumerator of given coletion. /// </summary> public Elements_Enumerator(Equation_Elements Elements_Coletion) { Coletion = Elements_Coletion; Reset(); }