public void FunctionShuntTest() { // It'll be fun to see how it deals with Pi! var yard = new ShuntingYard(new Symbol[] { new Operator("sin", Operators.Function, 5), new Operator("(", Operators.OpenBracket, 0), new Operator("max", Operators.Function, 5), new Operator("(", Operators.OpenBracket, 0), new Operand(2), new Operand(3), new Operator(")", Operators.CloseBracket, 0), new Operator("/", Operators.Division, 3), new Operand(3), new Operator("*", Operators.Multiplication, 3), new Operand((float)Math.PI), new Operator(")", Operators.CloseBracket, 0) }); Assert.True(yard.ShuntValid); yard.Shunt(); Assert.IsEmpty(yard.OperatorStack); var expected = new Symbol[] { new Operand(2), new Operand(3), new Operator("max", Operators.Function, 5), new Operand(3), new Operator("/", Operators.Division, 3), new Operand((float)Math.PI), new Operator("*", Operators.Multiplication, 3), new Operator("sin", Operators.Function, 5) } .Select(s => s.ToString()); var actual = yard.Output .Select(s => s.ToString()); Assert.AreEqual(expected, actual); }
public void AdvancedShuntTest() { var yard = new ShuntingYard(new Symbol[] { new Operand(3), new Operator("+", Operators.Addition, 2), new Operand(4), new Operator("*", Operators.Multiplication, 3), new Operand(2), new Operator("/", Operators.Division, 3), new Operator("(", Operators.OpenBracket, 0), new Operand(1), new Operator("-", Operators.Subtraction, 2), new Operand(5), new Operator(")", Operators.CloseBracket, 0), new Operator("^", Operators.Power, 4, true), new Operand(2), new Operator("^", Operators.Power, 4, true), new Operand(3) }); Assert.True(yard.ShuntValid); yard.Shunt(); Assert.IsEmpty(yard.OperatorStack); var expected = new Symbol[] { new Operand(3), new Operand(4), new Operand(2), new Operator("*", Operators.Multiplication, 3), new Operand(1), new Operand(5), new Operator("-", Operators.Subtraction, 2), new Operand(2), new Operand(3), new Operator("^", Operators.Power, 4, true), new Operator("^", Operators.Power, 4, true), new Operator("/", Operators.Division, 3), new Operator("+", Operators.Addition, 2) } .Select(s => s.ToString()); var actual = yard.Output .Select(s => s.ToString()); Assert.AreEqual(expected, actual); }
public void BasicShuntTest() { var yard = new ShuntingYard(new Symbol[] { new Operand(3), new Operator("+", Operators.Addition, 2), new Operand(4) }); Assert.True(yard.ShuntValid); yard.Shunt(); Assert.IsEmpty(yard.OperatorStack); var expected = new Symbol[] { new Operand(3), new Operand(4), new Operator("+", Operators.Addition, 2) } .Select(s => s.ToString()); var actual = yard.Output .Select(s => s.ToString()); Assert.AreEqual(expected, actual); }
static void Main(string[] args) { Console.Write("Enter an infix-notation equation: "); var infix = Console.ReadLine(); var yard = new ShuntingYard(infix); if (!yard.ShuntValid) { Console.WriteLine("Input equation is empty"); return; } yard.Shunt(); var sb = new StringBuilder(); sb.AppendJoin(' ', yard.Output.Select(s => s.ToString()).ToArray()); var rpn = sb.ToString(); Console.WriteLine("Your Reverse Polish Notation / Postfix Notation equation is:\n" + rpn); }