/// <summary> /// Высчитывает ответ из текстового математического выражения /// <para>Допустимые символы:</para> /// <para>Цифры [0 - 9];</para> /// <para>Знак точки для обозначения дробной части</para> /// <para>Знаки арифметических операций: + - * / %</para> /// <para>Круглые скобочки для обозначения приоритета одной операции над другой</para> /// </summary> /// <param name="input"></param> /// <exception cref="Exception"></exception> /// <returns></returns> public decimal Evaluate(string input) { var unexpectedCharacter = InputValidationService.FindUnexpectedCharacters(input).FirstOrDefault(); if (unexpectedCharacter != null) { throw new Exception($"Unexpected character: \'{unexpectedCharacter.Character}\', position: \'{unexpectedCharacter.Index}\'"); } var invalidBracket = InputValidationService.FindInvalidBracket(input).FirstOrDefault(); if (invalidBracket != null) { throw new Exception($"Invalid bracket: \'{invalidBracket.Character}\', position: \'{invalidBracket.Index}\'"); } var normalizedInput = input.RemoveExcessSpacebar().RemoveExcessLeadingSign().AddSpacebars().AddBracers(); if (ExpressionUnit.TryParse(normalizedInput, out var expressionUnit)) { return(expressionUnit.GetResult()); } throw new Exception("Parsing Error"); }
public void TryParse_SecondValueIsValue_ExpressionAndValue() { var input = "(1 + 2) + 1"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(typeof(ValueUnit), expressionUnit.SecondValue.GetType()); }
public void TryParse_PlusOperation_ValueAndValue() { var input = "1 + 2"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(Operation.Plus, expressionUnit.Operation); }
public void TryParse_PlusOperation_ExpressionAndExpression() { var input = "(1 + 5) + (2 - 1)"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(Operation.Plus, expressionUnit.Operation); }
public void TryParse_MultiplyOperation_ExpressionAndValue() { var input = "(1 - 0) * 2"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(Operation.Multiply, expressionUnit.Operation); }
public void TryParse_MinusOperation_ExpressionAndValue() { var input = "(1 * 8) - 2"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(Operation.Minus, expressionUnit.Operation); }
public void TryParse_DivRemainderOperation_ValueAndValue() { var input = "1 % 2"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(Operation.DivRem, expressionUnit.Operation); }
public void TryParse_DivRemainderOperation_ExpressionAndExpression() { var input = "(1 + 5) % (2 - 1)"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(Operation.DivRem, expressionUnit.Operation); }
public void TryParse_DivideOperation_ExpressionAndValue() { var input = "(1 + 1) / 2"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(Operation.Div, expressionUnit.Operation); }
public void TryParse_ExpressionBothValuesIsValueUnit_ValueAndValue_Negative() { var input = "-1 + -2"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(typeof(ValueUnit), expressionUnit.FirstValue.GetType()); Assert.AreEqual(typeof(ValueUnit), expressionUnit.SecondValue.GetType()); }
public void TryParse_ExpressionBothValueIsExpression_ExpressionAndExpression() { var input = "(1 + 5) * (2 - 1)"; var isSuccessfull = ExpressionUnit.TryParse(input, out var expressionUnit); Assert.IsTrue(isSuccessfull); Assert.AreEqual(typeof(ExpressionUnit), expressionUnit.FirstValue.GetType()); Assert.AreEqual(typeof(ExpressionUnit), expressionUnit.SecondValue.GetType()); }