コード例 #1
0
        private static void Main(string[] args)
        {
            //Initialized objects and variables
            BinaryExpression    ExpressionTree      = null;
            bool                IsValidExpression   = false;
            bool                ResultIsValid       = false;
            MyExpressionVisitor myExpressionVisitor = new MyExpressionVisitor();
            Validation          ValidateInput       = new Validation();
            CalculateExpression GoToCalculation     = new CalculateExpression();

            //Prompts the user until valid expression
            do
            {
                var StringExpression = GoToCalculation.PromptUser();

                ResultIsValid = ValidateInput.IsValid(StringExpression, IsValidExpression);

                //Each character in the expression is being validated one at a time
                if (ResultIsValid)
                {
                    ExpressionTree = GoToCalculation.CalculateValidExpression(StringExpression, ExpressionTree, myExpressionVisitor);

                    try
                    {
                        var results = Expression.Lambda(ExpressionTree).Compile().DynamicInvoke();

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"The Reverse Polish Notation of this expression is: {results:f}");
                        Console.ResetColor();

                        IsValidExpression = true;
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine($"\n{StringExpression} is NOT a valid Reverse Polish Notation expression, please enter a valid expression");
                        Console.ResetColor();
                        IsValidExpression = false;
                    }
                }
            } while (!IsValidExpression);

            Console.ReadKey();
        }
コード例 #2
0
 /// <summary>
 /// This method takes in parameters after validation is complete
 /// Of ValidExpression, ExpressionTree, And using MyExpressionVisitor Class
 /// This allows the ExpressionTree Variable to access the MyExpressionVisitor class
 /// To add to the stack of binary expressions and constants
 /// And returns the ExpressionTree complete expression
 /// </summary>
 /// <param name="ValidExpression"></param>
 /// <param name="ExpressionTree"></param>
 /// <param name="myExpressionVisitor"></param>
 /// <returns></returns>
 public BinaryExpression CalculateValidExpression(string ValidExpression, BinaryExpression ExpressionTree, MyExpressionVisitor myExpressionVisitor)
 {
     foreach (var token in ValidExpression.Split(' '))
     {
         //Checks for operand
         if (double.TryParse(token, out var result))
         {
             ExpressionTree = (BinaryExpression)myExpressionVisitor.Visit(Expression.Constant(result));
         }
         //Operator otherwise
         else
         {
             ExpressionTree = (BinaryExpression)myExpressionVisitor.Visit(Expression.Constant(token));
         }
     }
     return(ExpressionTree);
 }