Exemple #1
0
        /// <summary>
        /// Method used to display the result of a valid mathematical expression in reverse polish notaion.
        /// </summary>
        /// <param name="userInput">To be evaluated as a boolean decision.</param>
        /// <returns>Boolean representing user's decision to continue or exit the program.</returns>
        public static bool DisplayResult(ref ReversePolishExpressionBuilder expressionBuilder, string userInput)
        {
            // Validate, build, and evaluate an expression from user input to be stored in the expressionBuilder.
            expressionBuilder.ExpressionString = userInput;

            // Display result and prompt for user to continue calculating other expressions or exiting.
            bool first = true;

            while (true)
            {
                if (first)
                {
                    Console.WriteLine($"  Result: {expressionBuilder.Result}");
                }                                                                       // Only show result once.
                Console.Write("\n  Evaluate another reverse polish expression? (y/n)"); // Can repeatedly display.
                Console.CursorLeft = 53;                                                // Adjust cursor position.
                string userDecision = Console.ReadKey().KeyChar.ToString();             // Detect user keystroke.

                switch (userDecision.ToLower())                                         // Ensuring userDecision filtered to detect capital letters.
                {
                case "y":                                                               // Return to prompt user for another expression (main prompt).
                    return(true);

                case "n":
                    return(false); // Return to exit the program.

                default:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("\n  INVALID KEYSTROKE; TRY AGAIN.\n");
                    Console.ForegroundColor = ConsoleColor.White;
                    first = false; // Prompt user for another decision when keystroke is invalid.
                    break;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Main method for this console program allows users to input mathematical expressions in reverse polish
        /// (postfix) notation with integer constants, acceptable operators {+, -, *, /, ^}, and whitespace. This program
        /// has a feature using some additional logic here, which allows user inputs with whitespace ignored around operators.
        /// </summary>
        /// <param name="args">Not used.</param>
        public static void Main(string[] args)
        {
            // Create a expression visitor/builder to be passed by reference.
            ReversePolishExpressionBuilder expressionBuilder = new ReversePolishExpressionBuilder();

            Console.WindowWidth = 79;
            bool   running = true; // Flag used to remain in loop displaying options.
            string userInput;      // Declaring the user input to be read from console.

            // Loop until user enters 'exit' in expression prompt or chooses option 'n' to finish evaluating expressions.
            while (running)
            {
                try
                {
                    Console.Clear();
                    userInput = PromptForExpression(); // Get user input.

                    // Condition to either exit program or DisplayResult with prompt - following PromptForExpression.
                    if (userInput.ToLower().Equals("exit") || !DisplayResult(ref expressionBuilder, userInput))
                    {
                        running = false;
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        Console.WriteLine("\n\tEXITING REVERSE POLISH CALCULATOR... GOODBYE!\n");
                        Console.ResetColor();
                    }
                }
                catch (FormatException ex) // Thrown when attempted expression build has invalid characters or formatting.
                {
                    Console.Clear();
                    Console.Beep();
                    Console.WriteLine($"\n\tError: {ex.Message}");

                    expressionBuilder.Reset(); // Resets the builder after an error to accept other user input.
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n\t" +
                                      "\n\n\tMUST INPUT VALID EXPRESSION IN REVERSE POLISH NOTATION!" +
                                      "\n\n\tPRESS ANY KEY TO CONTINUE...");
                    Console.ResetColor();
                    Console.ReadKey();
                }
                catch (ArgumentOutOfRangeException ex) // Thrown whenever provided an integer too large.
                {
                    Console.Clear();
                    Console.Beep();
                    Console.WriteLine($"\n\tError: {ex.Message}");

                    expressionBuilder.Reset(); // Resets the builder after an error to accept other user input.
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n\t" +
                                      "\n\n\tMUST INPUT VALID EXPRESSION WITH INTEGERS FROM -2,147,438,648 TO 2,147,483,647" +
                                      "\n\n\tPRESS ANY KEY TO CONTINUE...");
                    Console.ResetColor();
                    Console.ReadKey();
                }
            }
        }