// Evaluates the operator, calls the right math method, prints answer, and passes answer to it's recall method public void Calculate(string Operator, string UserInput) { if (Operator == "+") // Addition { Answer = Calculator.Add(First, Second); Console.WriteLine("Answer: " + Answer); Stack.LastAnswer(UserInput, Answer); } else if (Operator == "-") // Subtraction { Answer = Calculator.Subtract(First, Second); Console.WriteLine("Answer: " + Answer); Stack.LastAnswer(UserInput, Answer); } else if (Operator == "*") // Multiplication { Answer = Calculator.Multiply(First, Second); Console.WriteLine("Answer: " + Answer); Stack.LastAnswer(UserInput, Answer); } else if (Operator == "/") // Division { if (Second == 0) // Throws error message if trying to divide by zero { Console.WriteLine("You can't divide by zero, Holmes."); } else { Answer = Calculator.Divide(First, Second); Console.WriteLine("Answer: " + Answer); Stack.LastAnswer(UserInput, Answer); } } else if (Operator == "%") // Modulus Function { Answer = Calculator.Modulus(First, Second); Console.WriteLine("Answer: " + Answer); Stack.LastAnswer(UserInput, Answer); } }
static void Main(string[] args) { // Prompt Variables String Prompt1 = "["; String Prompt2 = "]> "; int Counter = 0; bool GoForth = true; // Instantiate Classes Expression Expression = new Expression(); Handler Handler = new Handler(); Stack Stack = new Stack(); Constants Constants = new Constants(); // Print an introduction to the program Console.WriteLine("*** Welcome to Simple Calculator! ***\r\nPlease enter an equation using two numbers.\r\nThis calculator can add, subtract, multiply, divide, and use the modulus.\r\nYou can make any letter a variable using '=', but they can only be set once.\r\nType 'lastq' to see the last input you entered.\r\nType 'last' to repeat the last answer.\r\nType 'exit' or 'quit' when you are done.\r\n_______________________________________\r\n"); // Runs program in a loop while (GoForth == true) { Console.Write(Prompt1 + Counter + Prompt2); // Prints prompt String UserInput = Console.ReadLine().ToLower(); // Collects input in lower case Handler.Exit(UserInput); // Handles exit commands, if present Stack.LastQ(UserInput); // Prints last input, if user types 'lastq' if (Stack.DoThis == true) { Stack.LastAnswer(UserInput, Expression.Answer); // Print last answer, if user types 'last' } if (Stack.DoThis == true) { Constants.SliceConstant(UserInput); // Checks to see if user is trying to set a constant } if ((Constants.KeepGoing == true) && (Stack.DoThis == true)) { Expression.Slicer(UserInput); // Runs splicer code, which evaluates and parses an expression if (Expression.DoMath == true) { Expression.Calculate(Expression.Operator, UserInput); // Runs calculate code, which determines and executes arithmatic } } Counter++; // Increases prompt counter Stack.LastInput = UserInput; // Save the user input into a new variable before repeating so it can be recalled } Console.Read(); }