/// <summary> /// For explicit use from the command line. This constructor relies on a formula being supplied on the command line. /// </summary> /// <param name="equation"></param> public Calculator(string userInput) { _isDisposed = false; Equation equation = Equation.Parse(userInput); string result = Calculate(equation); Console.WriteLine("{0}", result); }
/// <summary> /// This gets the static instance of Calculator for use as an interactive shell. /// </summary> public static void Run() { try { using (Calculator calculator = new Calculator()) { while (true) { Console.Write("\n> "); string userInput = Console.ReadLine(); userInput = userInput.Trim(); userInput = userInput.Replace(" ", ""); if (!String.IsNullOrEmpty(userInput)) { try { switch (userInput) { default: Equation equation = Equation.Parse(userInput); string result = Calculate(equation); Console.WriteLine("{0}", result); break; case "h": DisplayHelp(); break; case "help": DisplayHelp(); break; case "?": DisplayHelp(); break; case "q": throw new UserQuitException(); case "quit": throw new UserQuitException(); } } catch (TokenUnrecognizedException ex0) { Console.WriteLine("An internal error occurred: TokenUnrecognizedException was caught. {0}", ex0.Message); } catch (FormatException ex0) { Console.WriteLine("An internal error occurred: Probably you attempted unary operations on a binary value. {0}", ex0.Message); } catch (InvalidOperationException ex0) { Console.WriteLine("An internal error occurred: Probably the stack was empty for some reason. {0}", ex0.Message); } } } } } catch (UserQuitException) { Console.WriteLine("Thanks for using PC.NET."); } }