Beispiel #1
0
 public void StackCanHoldLastQuestion()
 {
     Stack test1 = new Stack();
     test1.lastq = "1 + 1";
     string expected = "1 + 1";
     Assert.AreEqual(expected, test1.lastq);
 }
 public string DoMath(Stack stack)
 {
     if (stack.GetDictionary().ContainsKey(InputArray[0]))
     {
         Dictionary<string, string> dictionary = stack.GetDictionary();
         string value = dictionary[InputArray[0]];
         InputArray[0] = value;
     }
     if (stack.GetDictionary().ContainsKey(InputArray[2]))
     {
         Dictionary<string, string> dictionary = stack.GetDictionary();
         string value = dictionary[InputArray[2]];
         InputArray[2] = value;
     }
     switch (InputArray[1])
     {
         case "+" :
             return (int.Parse(InputArray[0]) + int.Parse(InputArray[2])).ToString();
         case "-":
             return (int.Parse(InputArray[0]) - int.Parse(InputArray[2])).ToString();
         case "*":
             return (int.Parse(InputArray[0]) * int.Parse(InputArray[2])).ToString();
         case "/":
             return (int.Parse(InputArray[0]) / int.Parse(InputArray[2])).ToString();
         case "%":
             return (int.Parse(InputArray[0]) % int.Parse(InputArray[2])).ToString();
         default:
                 throw new ArgumentException("no good");
     }
 }
 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 StackEnsureConstantsArentCaseSensitive()
 {
     Stack expression = new Stack();
     expression.AddConstant("A", 2);
     var expected = expression.ReturnConstant("a");
     Assert.AreEqual(2, expected);
 }
Beispiel #5
0
 public void StackEnsureICanCreateInstance()
 {
     Parse input = new Parse("10%4");
     Evaluate eval = new Evaluate(input);
     Stack stack = new Stack(eval);
     Assert.IsNotNull(stack);
 }
Beispiel #6
0
 public void StackCanHoldLastAnswer()
 {
     Stack test1 = new Stack();
     test1.last = 2;
     int expected = 2;
     Assert.AreEqual(expected, test1.last);
 }
 public void StackEnsureICanSetAndGetLastQuestion()
 {
     Stack expression = new Stack();
     expression.SetLastQ("2+2");
     string expectedStr = "2+2";
     string actualStr = expression.GetLastQ();
     Assert.AreEqual(expectedStr, actualStr);
 }
Beispiel #8
0
 public void StackConstantsAreCaseInsensitive()
 {
     Stack test1 = new Stack();
     test1.MyAdd('A', 2);
     int value;
     bool expected = test1.constants.TryGetValue('a', out value);
     Assert.AreEqual(2, value);
 }
Beispiel #9
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 StackEnsureICanSetAndGetLastAnswer()
 {
     Stack expression = new Stack();
     expression.SetLastA(2);
     int expectedInt = 2;
     int actualInt = expression.GetLastA();
     Assert.AreEqual(expectedInt, actualInt);
 }
 public void StackEnsureICanSetandGetConstant()
 {
     Stack expression = new Stack();
     expression.AddConstant("A", 2);
     var actual = expression.ReturnConstant("A");
     var expected = 2;
     Assert.AreEqual(expected, actual);
 }
Beispiel #12
0
 public void StackConstantAlreadyDefined()
 {
     Stack test1 = new Stack();
     test1.MyAdd('a', 9);
     test1.MyAdd('A', 2);
     int value;
     bool expected = test1.constants.TryGetValue('a', out value);
     Assert.AreEqual(2, value);
 }
Beispiel #13
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 StackEnsureICanAddKeysAndValuesToTheDictionary()
 {
     Stack test = new Stack();
     string user_key = "x";
     int user_value = 27;
     test.AddVariables(user_key, user_value);
     List<string> expected = test.FindAllKeysAndValues();
     Assert.AreEqual(expected, "x=27");
 }
Beispiel #15
0
 public void StackACanBeAConstant()
 {
     Stack test1 = new Stack();
     test1.constants.Add('a', 1);
     int value;
     bool expected = test1.constants.TryGetValue('a', out value);
     Assert.IsTrue(expected);
     Assert.AreEqual(1, value);
 }
 public string AddConstant(Stack stack)
 {
     if (stack.GetDictionary().ContainsKey(InputArray[0]))
     {
         stack.dictionary[InputArray[0]] = InputArray[2];
         return InputArray[0] + " " + InputArray[1] + " " + InputArray[2];
     }
     stack.AddToDictionary(InputArray[0], InputArray[2]);
     return InputArray[0] + " " + InputArray[1] + " " + InputArray[2];
 }
Beispiel #17
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");
 }
 public string DoMathOrAddConstant(Stack stack)
 {
     if (InputArray[1] == "=")
     {
         return AddConstant(stack);
     }
     else
     {
        return DoMath(stack);
     }
 }
 public void StackEnsureGettersAndSettersWork()
 {
     Stack test = new Stack();
     decimal number = 5;
     test.LastAnswer = number;
     string expression = "1+2";
     test.LastExpression = expression;
     string expected_expression = test.LastExpression;
     decimal expected_number = test.LastAnswer;
     Assert.AreEqual(expected_expression, expression);
     Assert.AreEqual(expected_number, number);
 }
Beispiel #20
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 #21
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();
                    }

                }
            }
        }
 public static string Eval(string input, Stack stack)
 {
     Parse StringToEvaluate = new Parse(input);
     StringToEvaluate.ParseInput();
     try
     {
         return StringToEvaluate.DoMathOrAddConstant(stack);
     }
     catch (Exception)
     {
         return "invalid input";
     }
 }
        static void Main(string[] args)
        {
            Parse userInput = new Parse();
            Evaluate expression = new Evaluate();
            Stack stack = new Stack();

            bool runProgram = true;
            int counter = 0;

            while (runProgram)
            {
                Console.Write("[" + counter + "]> ");

                string response = Console.ReadLine();

                if (response == "exit" || response == "quit")
                {
                    Console.WriteLine("Bye!! \nPress any key to close program...");
                    runProgram = false;
                    Console.ReadKey();
                }
                else if(response == "lastq")
                {
                    Console.WriteLine("     " + stack.LastExpression);
                }
                else if (response == "last")
                {
                    Console.WriteLine("     " + stack.LastAnswer);

                }
                else
                {
                    string splitResponse = userInput.formatInput(response);
                    if (splitResponse == "Invalid Expression. Please try again!")
                    {
                        Console.WriteLine(splitResponse);
                    }
                    else
                    {
                        userInput.splitString(splitResponse);
                        decimal user_answer =
                            expression.calculate(userInput.FirstNumber, userInput.Operator, userInput.SecondNumber);
                        counter += 1;
                        Console.WriteLine("   " + "=" + " " + user_answer);
                        stack.LastExpression = response;
                        stack.LastAnswer = user_answer;
                    }
                }
            }
        }
 static void Main(string[] args)
 {
     Stack stack = new Stack();
     Console.Write("0" + '>');
     string input="";
     int counter = 0;
     while (true)
     {
         input = Console.ReadLine();
         if (!input.Contains("=")) { counter++; };
         Console.WriteLine(Evaluate.Eval(input, stack));
         Console.Write(counter.ToString() + '>');
         if (input.ToLower() == "exit" || input.ToLower() == "quit") { break; }
     }
 }
 public void StackEnsureCanInstantiate()
 {
     Stack stack = new Stack();
     Assert.IsNotNull(stack);
 }
Beispiel #26
0
 public void StackExists()
 {
     Stack test1 = new Stack();
 }
 public void StackEnsureICanCreateInstance()
 {
     Stack stack1 = new Stack();
     Assert.IsNotNull(stack1);
 }
 public void StackEnsureICannotOverrideConstant()
 {
     Stack expression = new Stack();
     expression.AddConstant("A", 2);
     expression.AddConstant("A", 4);
 }
 public void StackEnsureICannotReturnAConstanThatHasNotBeenSet()
 {
     Stack expression = new Stack();
     expression.ReturnConstant("A");
 }
 public void StackEnsureICanCreateAClassInstance()
 {
     Stack test = new Stack();
     Assert.IsNotNull(test);
 }