public static bool IsEquationValid(string _input) { if (string.IsNullOrEmpty(_input)) { return(false); } var hasInvalidCharacter = _input.Any(_character => !(EquationHelper.IsCharacterNumber(_character) || EquationHelper.IsCharacterOperator(_character) || (EquationHelper.IsBracketsValid(_input, EquationHelper.IsCharacterBracket(_character)) && EquationHelper.IsCharacterBracket(_character)))); if (hasInvalidCharacter) { return(false); } if (EquationHelper.IsNumberMultipleDecimal(_input)) { return(false); } if (EquationHelper.IsLastCharacterAOperator(_input)) { return(false); } if (EquationHelper.IsOperatorDuplicated(_input)) { return(false); } return(true); }
public double CalculateEquation(string equation) { var numberCharacterBuilder = new StringBuilder(); var numbers = new List <int>(); var operators = new List <char>(); double result = 0; int counter = 0; var Operation = new OperatorFactory(); calcManager = new CalculationManager(); var updatedPreviousResult = 0; var updatedCurrentValue = 0; foreach (var character in equation) { if (EquationHelper.IsCharacterNumber(character)) { numberCharacterBuilder.Append(character); } else { if (NumbersListValidator.IsListEmpty(numbers)) { updatedPreviousResult = UpdateCalcManagerPrevious(numbers, numberCharacterBuilder.ToString()); numberCharacterBuilder.Clear(); } else { updatedCurrentValue = UpdateCalcManagerCurrent(numbers, numberCharacterBuilder.ToString()); numberCharacterBuilder.Clear(); var NextOperation = Operation.GetOperator(operators[counter - 1]); result = NextOperation.DoMaths(updatedPreviousResult, updatedCurrentValue); updatedPreviousResult = UpdateCalcManagerPrevious(numbers, result.ToString()); } counter++; operators.Add(character); } } updatedCurrentValue = UpdateCalcManagerCurrent(numbers, numberCharacterBuilder.ToString()); var LastOperation = Operation.GetOperator(operators[operators.Count() - 1]); result = LastOperation.DoMaths(updatedPreviousResult, updatedCurrentValue); return(result); }