Esempio n. 1
0
 public void EvaluateReturnLastAnswer()
 {
     Evaluate test1 = new Evaluate();
     int expected = 4;
     int actual = test1.last("2 + 2");
     Assert.AreEqual(expected, actual);
 }
Esempio n. 2
0
 public void EvaluateReturnLastQuestion()
 {
     Evaluate test1 = new Evaluate();
     string expected = "2 + 2";
     string actual = test1.lastq("2 + 2");
     Assert.AreEqual(expected, actual);
 }
Esempio n. 3
0
 public void EvaluateCountWorks()
 {
     Evaluate test1 = new Evaluate();
     test1.DoMath("4 + 2");
     test1.DoMath("4 + 4");
     Assert.AreEqual(2, test1.count);
 }
Esempio n. 4
0
 public void EvalCanAddTwoNumbers()
 {
     Parse input = new Parse("3 + 4");
     Evaluate eval = new Evaluate(input);
     int expected = eval.result;
     Assert.AreEqual(expected, 7);
 }
Esempio n. 5
0
 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 StackCanReturnLast()
 {
     Evaluate oldStack = new Evaluate("2 * 8");
     Evaluate newStack = new Evaluate("2+5");
     Stack myStack = new Stack(newStack);
     Assert.AreEqual("2+5", myStack.Lastq());
 }
 public void EvaluateCanExecuteBasicOperation()
 {
     Parse parse = new Parse("1 + 5");
     Evaluate eval = new Evaluate(parse);
     eval.Execute();
     Assert.AreEqual("6", eval.Answer);
 }
Esempio n. 8
0
 public void stackEvaluate(Evaluate eval)
 {
     lastQ = eval.lastQ;
     lastAns = eval.result;
     currentAnswer = eval.result;
     listOfConstantsUsed = eval.ListOfConstantsUsed;
 }
 public void EvaluateEnsureICanGetARemainder()
 {
     Parse a_parse = new Parse("20 % 3");
     Evaluate eval = new Evaluate(a_parse);
     int actual = eval.result;
     Assert.AreEqual(2, actual);
 }
        static void Main(string[] args)
        {
            Console.WriteLine("What is your name?");
            string userName = Console.ReadLine();
            Parse p = new Parse();
            Evaluate e = new Evaluate();
            bool run = true;

            while (run)
            {
                Console.WriteLine(userName + " Define your mathematical operation");
                string input = Console.ReadLine();
                if (input.ToUpper() == "LASTQ")
                {
                    Console.WriteLine(e.lastExpression());
                    run = false;
                    break;
                }
                string oper = p.getOperator(input);
                int num1 = p.getNumberOne(input);
                int num2 = p.getNumberTwo(input);
                int answer = e.ExecuteMethod(num1, num2, oper);
                Console.WriteLine("The answer to your operation is: " + answer);
            }
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            int count = 0;
            Constant currentConstant = new Constant();
            Evaluate expression;

            while (true)
            {
                Console.Write("[{0}]> ", count);
                string input = Console.ReadLine();
                expression = new Evaluate(input, currentConstant);
                if (input.ToLower() == "exit" || input.ToLower() == "quit")
                {
                    break;
                }
                else if (input.IndexOf("=")> -1)
                {
                    //set variable

                    Parse addConstant = new Parse(input, currentConstant);
                    addConstant.setOperatorIndex();
                    currentConstant.addConst(addConstant.StringFirst(), addConstant.secondNum());
                    // = saved 'x' as '3'
                    Console.WriteLine("= saved '" + addConstant.StringFirst() + "' as '" + addConstant.secondNum() + "'");

                }
                else
                {
                    int answer = expression.doMath();
                    Console.WriteLine("   = {0}", answer);
                    count++;

                }
            }
        }
Esempio n. 12
0
 public void StackEnsureICanCreateInstance()
 {
     Parse input = new Parse("10%4");
     Evaluate eval = new Evaluate(input);
     Stack stack = new Stack(eval);
     Assert.IsNotNull(stack);
 }
 public void EvaluateCanAssignConstant()
 {
     Parse parse = new Parse("q= 7");
     Evaluate eval = new Evaluate(parse);
     eval.Execute();
     Assert.AreEqual("Stored: q = 7", eval.Answer);
 }
 public void EvaluateCanReturnBlankExecuteCommand()
 {
     Parse parse = new Parse("lastq");
     Evaluate eval = new Evaluate(parse);
     eval.ExecuteCommand();
     Assert.AreEqual("There are no commands stored.", eval.Answer);
 }
 public void EvaluateEnsureICanDoDivision()
 {
     Parse a_parse = new Parse("21 / 3");
     Evaluate eval = new Evaluate(a_parse);
     int actual = eval.result;
     Assert.AreEqual(7, actual);
 }
 public void ConstantCanAdd()
 {
     Constant newConstant = new Constant();
     newConstant.addConst("a", 2);
     Evaluate twoPlusThree = new Evaluate("a + 3", newConstant);
     Assert.AreEqual(5, twoPlusThree.doMath());
 }
 public void EvaluateEnsureICanDoAddition()
 {
     Parse a_parse = new Parse("21 + 3");
     Evaluate eval = new Evaluate(a_parse);
     int actual = eval.result;
     Assert.AreEqual(24, actual);
 }
 public void EvaluateEnsureICanDoSubtraction()
 {
     Parse a_parse = new Parse("21 - 3");
     Evaluate eval = new Evaluate(a_parse);
     int actual = eval.result;
     Assert.AreEqual(18, actual);
 }
 public void EvaluateEnsureICanDoMultiplication()
 {
     Parse a_parse = new Parse("2 * 3");
     Evaluate eval = new Evaluate(a_parse);
     int actual = eval.result;
     Assert.AreEqual(6, actual);
 }
Esempio n. 20
0
 public void EvaluateInputWorksForConstantAddition()
 {
     string input = "a+5";
     Constant constant = new Constant();
     constant.AddConstant('a', 1);
     Evaluate newSession = new Evaluate();
     Assert.AreEqual(6, newSession.EvaluateInput(input, constant));
 }
 public void EvaluateInputConstantSubtract()
 {
     string input = "a-8";
     Constant constant = new Constant();
     constant.AddConstant('a', 7);
     Evaluate newSession = new Evaluate();
     Assert.AreEqual(2, newSession.EvaluateInput(input, constant));
 }
 public void EvaluateInputConstantMultiply()
 {
     string input = "a*8";
     Constant constant = new Constant();
     constant.AddConstant('a', 1);
     Evaluate newSession = new Evaluate();
     Assert.AreEqual(5, newSession.EvaluateInput(input, constant));
 }
 public void EvaluateInputConstantModulo()
 {
     string input = "a%5";
     Constant constant = new Constant();
     constant.AddConstant('a', 11);
     Evaluate newSession = new Evaluate();
     Assert.AreEqual(1, newSession.EvaluateInput(input, constant));
 }
 public void EvaluateInputConstantDivision()
 {
     string input = "a/5";
     Constant constant = new Constant();
     constant.AddConstant('a', 10);
     Evaluate newSession = new Evaluate();
     Assert.AreEqual(2, newSession.EvaluateInput(input, constant));
 }
Esempio n. 25
0
 public void StackCanRetrieveCurrentAnswer()
 {
     Parse input = new Parse("20 / 5");
     Evaluate eval = new Evaluate(input);
     Stack stack = new Stack(eval);
     int expected = stack.currentAnswer;
     Assert.AreEqual(expected, 4);
 }
 public void EvaluateCanInstantiateWithParseObj()
 {
     Parse parse = new Parse("1 / 3");
     parse.Compute();
     Evaluate eval = new Evaluate(parse);
     Assert.AreEqual(parse, eval.ParsedInput);
     Assert.AreEqual(1, eval.ParsedInput.FirstNumArg);
 }
Esempio n. 27
0
 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 });
 }
        public void EvaluateTestOperateWithSubtract()
        {
            string input = "2 - 1";
            ProcessInput PI = new ProcessInput(input);
            Evaluate ev = new Evaluate(PI.GetFirstInputNumber(), PI.GetOperator(), PI.GetSecondInputNumber());
            int expected = 1;

            Assert.AreEqual(expected, ev.Operate());
        }
Esempio n. 29
0
 public void StackCanAssignConstant()
 {
     Parse input = new Parse("x = 3");
     Evaluate eval = new Evaluate(input);
     Stack stack = new Stack(eval);
     char expected = 'x';
     List<char> actual_constants = stack.listOfConstantsUsed;
     CollectionAssert.Contains(actual_constants, expected);
 }
 public void EvaluateCanSubstitueConstants()
 {
     Parse parse = new Parse("q= 7");
     Evaluate eval = new Evaluate(parse);
     eval.Execute();
     Parse parseTwo = new Parse("q + 5");
     eval.ParsedInput = parseTwo;
     eval.SubstituteConstants();
     Assert.AreEqual(7, eval.ParsedInput.FirstNumArg);
 }
Esempio n. 31
0
        static void Main(string[] args)
        {
            Parser parser = new Parser();
            Stack  stack  = new Stack();
            Dictionary <char, int> constants = new Dictionary <char, int>();

            // initialize Evaluator HERE
            Evaluate evaluate = new Evaluate();
            int      counter  = 0;

            List <string> Escape = new List <string>()
            {
                "quit", "exit", "escape", "stop"
            };                                                                             // exit commands
            string lastq      = "lastq";
            string lastanswer = "last";

            // ask the user for a basic mathematical equation
            Console.WriteLine("Please type a basic expression you would like me to find the answer to.");
            Console.WriteLine("Include one of the following operators: + - * / %.");
            Console.WriteLine("Get your last answer by typing last");
            Console.WriteLine("Get your last question by typing lastq");
            Console.WriteLine("You can also define constants to use in your expressions. ( x = 1 )");
            Console.WriteLine("Get a list of constants by typing constants");

            Console.WriteLine("Exit at anytime by typing exit / quit / escape / stop");

            while (true)
            {
                string userInput = ConsoleReadLineWithDefault($"[{counter}]> ");
                counter++;
                if (Escape.Contains(userInput))
                {
                    Console.WriteLine("Bye!!");
                    System.Threading.Thread.Sleep(1000);
                    Environment.Exit(0);
                }
                else if (userInput.Equals(lastq))
                {
                    // lastq from stack
                    Console.WriteLine(stack.LastQuery);
                }
                else if (userInput.Equals(lastanswer))
                {
                    // calls lastanswer from stack
                    Console.WriteLine(stack.LastAnswer);
                }
                else if (userInput.Equals("constants"))
                {
                    foreach (KeyValuePair <char, int> kvp in constants)
                    {
                        Console.WriteLine("Constant = {0}, Value = {1}", kvp.Key, kvp.Value);
                    }
                }
                else
                {
                    //evaluate input
                    try
                    {
                        // send user input to parser function=
                        stack.LastQuery = userInput;
                        parser.ParseInput(userInput);
                        int firstTerm  = 0;
                        int secondTerm = 0;
                        if (parser.Operator.Equals('='))
                        {
                            int  n;
                            bool isNumeric = int.TryParse(parser.FirstTerm, out n);
                            if (isNumeric == false)
                            {
                                try
                                {
                                    constants.Add(char.Parse(parser.FirstTerm.ToLower()), int.Parse(parser.SecondTerm));
                                }
                                catch (ArgumentException)
                                {
                                    Console.WriteLine($"A constant with key \"{parser.FirstTerm}\" already exists.");
                                }
                            }
                            else
                            {
                                Console.WriteLine($"Invalid constant! Please use lowercase letters for constants.");
                            }
                        }
                        else
                        {
                            if (parser.FirstTerm.Length > 1)
                            {
                                if (parser.SecondTerm.Length > 1)
                                {
                                    firstTerm  = int.Parse(parser.FirstTerm);
                                    secondTerm = int.Parse(parser.SecondTerm);
                                }
                                else
                                {
                                    firstTerm = int.Parse(parser.FirstTerm);
                                    if (constants.ContainsKey(char.Parse(parser.SecondTerm)))
                                    {
                                        secondTerm = constants[char.Parse(parser.SecondTerm)];
                                    }
                                    else
                                    {
                                        secondTerm = int.Parse(parser.SecondTerm);
                                    }
                                }
                            }
                            else if (parser.SecondTerm.Length > 1 && parser.FirstTerm.Length == 1)
                            {
                                if (constants.ContainsKey(char.Parse(parser.FirstTerm)))
                                {
                                    firstTerm = constants[char.Parse(parser.FirstTerm)];
                                }
                                else
                                {
                                    firstTerm = int.Parse(parser.FirstTerm);
                                }

                                secondTerm = int.Parse(parser.SecondTerm);
                            }
                            else if (constants.ContainsKey(char.Parse(parser.FirstTerm.ToLower())) && constants.ContainsKey(char.Parse(parser.SecondTerm)))
                            {
                                firstTerm  = constants[char.Parse(parser.FirstTerm.ToLower())];
                                secondTerm = constants[char.Parse(parser.SecondTerm)];
                            }
                            else if (constants.ContainsKey(char.Parse(parser.FirstTerm.ToLower())))
                            {
                                firstTerm  = constants[char.Parse(parser.FirstTerm.ToLower())];
                                secondTerm = int.Parse(parser.SecondTerm);
                            }
                            else if (constants.ContainsKey(char.Parse(parser.SecondTerm)))
                            {
                                firstTerm  = int.Parse(parser.FirstTerm);
                                secondTerm = constants[char.Parse(parser.SecondTerm.ToLower())];
                            }
                            else
                            {
                                firstTerm  = int.Parse(parser.FirstTerm);
                                secondTerm = int.Parse(parser.SecondTerm);
                            }
                            int calculatedResult = evaluate.Calculate(firstTerm, secondTerm, parser.Operator); //test code -- fix when parser updated
                            stack.LastAnswer = calculatedResult;
                            Console.WriteLine($" = {calculatedResult}");
                        }
                    }
                    catch (Exception)
                    {
                        Console.WriteLine($"Sorry, { userInput } is not a valid request.");
                        Console.WriteLine("Try again, something more simple like: 2 + 1. Take it easy on me");
                    }
                }
            }
        }