/// <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> /// Converts the string representation of a number in given numeral system to its /// double-precision floating-point number equivalent. /// </summary> /// <param name="work_Text">String to convert</param> public Number Parse(string work_Text) { int comma_Position = Comma_Position(work_Text); bool is_Negative = false; double parsed_Output = 0; if (work_Text[0] == '-') { is_Negative = true; } for (int i = (is_Negative ? 1 : 0); i < comma_Position; i++) //parsing numbers highier than zero { try { parsed_Output += Digit_Parse(work_Text[i], Numeral_System) * Math.Pow( Numeral_System.System_Type, comma_Position - i - 1); } catch (Exception ex) { parsed_Output = parsed_Output * Math.Pow(Numeral_System.System_Type, i - comma_Position); parsed_Output = Recognize_Negative_Sign(parsed_Output, is_Negative); IMessage_Box Message_Box = Factory.Get_Message_Box; Message_Box.Pop_Up(ex.Message + $", {work_Text} parsed as {parsed_Output}."); return(new Number(parsed_Output)); } } for (int i = comma_Position + 1; i < work_Text.Length; i++) //parsing numbers lower than zero { try { parsed_Output += Digit_Parse(work_Text[i], Numeral_System) * Math.Pow( Numeral_System.System_Type, comma_Position - i); } catch (Exception ex) { parsed_Output = Recognize_Negative_Sign(parsed_Output, is_Negative); IMessage_Box Message_Box = Factory.Get_Message_Box; Message_Box.Pop_Up(ex.Message + $", {work_Text} parsed as {parsed_Output}."); return(new Number(parsed_Output)); } } parsed_Output = Recognize_Negative_Sign(parsed_Output, is_Negative); return(new Number(parsed_Output)); }
/// <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); }