public void EvalCanEvaluateLastQPrompt()
 {
     //Arrange
     Parse input = new Parse("-10 - 2");
     Parse input2 = new Parse("5*5");
     Parse input3 = new Parse("lastq");
     /// Act
     Evaluate eval = new Evaluate(input); // Initializes session
     eval.EvaluateExpression(input2);
     eval.EvaluateExpression(input3);
     //Assert
     Assert.AreEqual(eval.lastQ, "5*5");
 }
 public void EvalAssignUsedConstantThrowsException()
 {
     Parse input = new Parse("x = 3");
     Parse input2 = new Parse("x = 10");
     Evaluate eval = new Evaluate(input);
     eval.EvaluateExpression(input2);
 }
 public void EvalAddsMultipleToResultHistory()
 {
     Parse input = new Parse("-10 - 2");
     Parse input2 = new Parse("5*5");
     Evaluate eval = new Evaluate(input); // Initializes session
     eval.EvaluateExpression(input2); // Every subsequent parse/equation gets passed to evaluateExpression method
     List<int> expected = eval.resultHistory;
     CollectionAssert.AreEqual(expected, new List<int>() { -12, 25 });
 }
Beispiel #4
0
 public void StackCanRetrieveLastQ()
 {
     Parse input = new Parse("10%4");
     Evaluate eval = new Evaluate(input);
     Parse input2 = new Parse("lastq");
     eval.EvaluateExpression(input2);
     Stack stack = new Stack(eval);
     string expected = stack.lastQ;
     Assert.AreEqual(expected, "10%4");
 }
Beispiel #5
0
 public void StackCanEvalExpressionWithPrevDeclaredConstant()
 {
     Parse input = new Parse("x = 3");
     Parse input2 = new Parse("x*5");
     Evaluate eval = new Evaluate(input);
     eval.EvaluateExpression(input2);
     Stack stack = new Stack(eval);
     // Act
     int expected = stack.currentAnswer;
     //Assert
     Assert.AreEqual(expected, 15);
 }
Beispiel #6
0
        static void Main(string[] args)
        {
            int count = 0;
            string prompt = "[" + count.ToString() + "]>";
            string instructions = "Type in a binary equation, then press enter." + System.Environment.NewLine + "Type 'quit' or 'exit' to exit the program." + System.Environment.NewLine + prompt;
            Console.Write(instructions);
            string userInput = Console.ReadLine();
            Parse firstInput = new Parse(userInput);
            Evaluate eval = new Evaluate(firstInput);
            Stack stack = new Stack(eval);
            count += 1;
            prompt = "[" + count.ToString() + "]>";
            Console.Write(" = " + stack.currentAnswer.ToString() + System.Environment.NewLine + prompt);
            userInput = Console.ReadLine();

            //ProgramInitializer PI = new ProgramInitializer();
            //string userInput = Console.ReadLine();
            //PI.EvaluateFirstExpression(userInput);
            //Evaluate eval = PI.FirstEval;
            //Stack stack = PI.ProgramStack;

            //Console.Write(" = " + stack.currentAnswer.ToString() + System.Environment.NewLine);
            //userInput = Console.ReadLine();

            while (userInput != "quit" || userInput != "exit")
            {
                try
                {
                    Parse input = new Parse(userInput);
                    eval.EvaluateExpression(input);
                    stack.stackEvaluate(eval);
                    count += 1;
                    prompt = "[" + count.ToString() + "]>";
                    Console.Write(" = " + stack.currentAnswer.ToString() + System.Environment.NewLine + prompt);
                    userInput = Console.ReadLine();
                }
                catch (Exception ex)
                {
                    if (userInput == "quit" || userInput == "exit")
                    {
                        break;
                    }
                    else
                    {
                        prompt = "[" + count.ToString() + "]>";
                        Console.Write("There was an error. Please try again." + System.Environment.NewLine + prompt);
                        userInput = Console.ReadLine();
                    }

                }
            }
        }
Beispiel #7
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Please enter a simple arithmetic expression.");

            Terminal   user_term       = new Terminal();
            Expression user_expression = new Expression();
            Evaluate   math_ops        = new Evaluate();
            EvalStack  eval_stack      = new EvalStack();
            bool       loop_runner     = true;

            while (loop_runner)
            {
                Console.Write(user_term.PromptInitialUserInput());
                string user_input = Console.ReadLine();

                if (user_input == "lastq")
                {
                    Console.WriteLine(user_term.ReturnLastExpression(eval_stack));
                }
                else if (user_input == "last")
                {
                    Console.WriteLine(user_term.DisplayExpressionResult(eval_stack.last));
                }
                else if (user_input == "exit" || user_input == "quit")
                {
                    Environment.Exit(0);
                }
                else if (user_input.Contains('='))
                {
                    object[] parsed_expression = user_expression.Parse(user_input, eval_stack);
                    user_term.counter++;
                }
                else
                {
                    object[] parsed_expression = user_expression.Parse(user_input, eval_stack);
                    double   expression_output = math_ops.EvaluateExpression(parsed_expression, eval_stack);
                    user_term.counter++;
                    Console.WriteLine(user_term.DisplayExpressionResult(expression_output));
                }
            }
        }