public void Tree_Test() { var initialEquation = "3+2*4/5"; var checkPostfix = ShuntingYardAlgorithm.PostfixString(initialEquation); checkPostfix.Should().Be("324*5/+"); var arithmeticTree = ExpressionTree.BuildArithmeticTree(checkPostfix); arithmeticTree.Data.Should().Be('+'); }
public void PostfixEvaluatorTest() { // 3 + 4 * 5 = 23 var queue = ShuntingYardAlgorithm.ShuntingYard("3+4*5"); PostfixEvaluator.EvaluatePostfix(queue).Should().Be(23); // 3 * (4 + 5) = 27 queue = ShuntingYardAlgorithm.ShuntingYard("3*(4+5)"); PostfixEvaluator.EvaluatePostfix(queue).Should().Be(27); }
public void InfixToPostfixTest() { var queue = ShuntingYardAlgorithm.ShuntingYard("3+2*4/5"); queue.Dequeue().Should().Be('3'); queue.Dequeue().Should().Be('2'); queue.Dequeue().Should().Be('4'); queue.Dequeue().Should().Be('*'); queue.Dequeue().Should().Be('5'); queue.Dequeue().Should().Be('/'); queue.Dequeue().Should().Be('+'); queue = ShuntingYardAlgorithm.ShuntingYard("3+4*5"); queue.Dequeue().Should().Be('3'); queue.Dequeue().Should().Be('4'); queue.Dequeue().Should().Be('5'); queue.Dequeue().Should().Be('*'); queue.Dequeue().Should().Be('+'); queue = ShuntingYardAlgorithm.ShuntingYard("3*(4+5)"); queue.Dequeue().Should().Be('3'); queue.Dequeue().Should().Be('4'); queue.Dequeue().Should().Be('5'); queue.Dequeue().Should().Be('+'); queue.Dequeue().Should().Be('*'); queue = ShuntingYardAlgorithm.ShuntingYard("3+4*2/(1-5)^2^3"); // 3 4 2 × 1 5 − 2 3 ^ ^ ÷ + queue.Dequeue().Should().Be('3'); queue.Dequeue().Should().Be('4'); queue.Dequeue().Should().Be('2'); queue.Dequeue().Should().Be('*'); queue.Dequeue().Should().Be('1'); queue.Dequeue().Should().Be('5'); queue.Dequeue().Should().Be('-'); queue.Dequeue().Should().Be('2'); queue.Dequeue().Should().Be('3'); queue.Dequeue().Should().Be('^'); queue.Dequeue().Should().Be('^'); queue.Dequeue().Should().Be('/'); queue.Dequeue().Should().Be('+'); queue = ShuntingYardAlgorithm.ShuntingYard("3-2+1"); queue.Dequeue().Should().Be('3'); queue.Dequeue().Should().Be('2'); queue.Dequeue().Should().Be('-'); queue.Dequeue().Should().Be('1'); queue.Dequeue().Should().Be('+'); }
static void Main(string[] args) { while (true) { string input = PromptGetResponse(Messages.Prompt_EnterExpression); if (input.ToLower() == QuitKeyword.ToLower()) { break; } string postfix; int result; try { postfix = ShuntingYardAlgorithm.Convert(input); } catch (Exception) { DisplayMessage(Messages.ErrorMessage_PostfixParseFailed, isError: true); continue; } try { result = ReversePolishNotation.Evaluate(postfix); } catch (Exception ex) { // special case in that it's very clear what the problem is if (ex is DivideByZeroException) { DisplayMessage(Messages.ErrorMessage_EvaluationGeneratesDivideByZero, isError: true); } else { DisplayMessage(Messages.ErrorMessage_EvaluationFailed, isError: true); } continue; } DisplayMessage($"{Messages.Message_Result} {result}"); } }
private static void Main() { const string infix = "3+2*4/5"; Console.WriteLine($"Initial infix: {infix}"); // 324*5/+ var postfix = ShuntingYardAlgorithm.PostfixString(infix); Console.WriteLine($"Postfix by Shunting-yard: {postfix}"); var arithmeticTree = ExpressionTree.BuildArithmeticTree(postfix); Console.WriteLine("Infix by In-Order traversal: "); DisplayTree.InOrderTraversal(arithmeticTree); Console.WriteLine(); Console.WriteLine("Postfix notation by Post-Order traversal: "); DisplayTree.PostOrderTraversal(arithmeticTree); Console.WriteLine(); Console.WriteLine("Prefix notation by Pre-Order traversal: "); DisplayTree.PreOrderTraversal(arithmeticTree); Console.WriteLine(); Console.WriteLine("Infix: "); ExpressionTree.PrintInfixExpression(arithmeticTree); }
public void WhenConvert_InvalidExpression_ThrowsException(string input) { Assert.ThrowsException <FormatException>(() => ShuntingYardAlgorithm.Convert(input)); }
public void WhenConvert_ValidExpression_Returns_CorrectResult(string input, string expectedOutput) { var output = ShuntingYardAlgorithm.Convert(input); Assert.AreEqual(expectedOutput, output); }
private void Given(string expression) { var test = new ShuntingYardAlgorithm(); _result = test.Transform(expression); }