public void ConstantEnsureICanAddConstantToDictionary()
 {
     Constant constant = new Constant();
     constant.AddConstant('x', 5);
     int result;
     constant.constant_dictionary.TryGetValue('x', out result);
     Assert.AreEqual(5, result);
 }
 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 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 EvaluateInputConstantDivision()
 {
     string input = "a/5";
     Constant constant = new Constant();
     constant.AddConstant('a', 10);
     Evaluate newSession = new Evaluate();
     Assert.AreEqual(2, 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 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 AddWorksWithConstantsBackwards()
 {
     string mock_input = "22 + a";
     Parser parse = new Parser();
     int toBeKey = int.Parse(parse.GetFirst(mock_input));
     char toBeKeyValue = char.Parse(parse.GetSecond(mock_input));
     Constant constant = new Constant();
     constant.AddConstant(toBeKeyValue, 1);
     Add items = new Add();
     Assert.AreEqual(23, items.AddConstants(mock_input, constant));
 }
 public void MultiplyWorksWithConstants()
 {
     string mock_input = "a * 2";
     Parser parse = new Parser();
     char toBeKey = char.Parse(parse.GetFirst(mock_input));
     int toBeKeyValue = int.Parse(parse.GetSecond(mock_input));
     Constant constant = new Constant();
     constant.AddConstant(toBeKey, 1);
     Multiply items = new Multiply();
     Assert.AreEqual(2, items.MultiplyConstants(mock_input, constant));
 }
 public void ModuloWorksWithConstants()
 {
     string mock_input = "a % 2";
     Parser parse = new Parser();
     char toBeKey = char.Parse(parse.GetFirst(mock_input));
     int toBeKeyValue = int.Parse(parse.GetSecond(mock_input));
     Constant constant = new Constant();
     constant.AddConstant(toBeKey, 5);
     Modulo items = new Modulo();
     Assert.AreEqual(1, items.ModuloConstants(mock_input, constant));
 }
 public void DivideWorksWithConstants()
 {
     string mock_input = "a / 2";
     Parser parse = new Parser();
     char toBeKey = char.Parse(parse.GetFirst(mock_input));
     int toBeKeyValue = int.Parse(parse.GetSecond(mock_input));
     Constant constant = new Constant();
     constant.AddConstant(toBeKey, 2);
     Divide items = new Divide();
     Assert.AreEqual(1, items.DivideConstants(mock_input, constant));
 }
 public void SubtractWorksWithConstants()
 {
     string mock_input = "a - 2";
     Parser parse = new Parser();
     char toBeKey = char.Parse(parse.GetFirst(mock_input));
     int toBeKeyValue = int.Parse(parse.GetSecond(mock_input));
     Constant constant = new Constant();
     constant.AddConstant(toBeKey, 1);
     Subtract items = new Subtract();
     Assert.AreEqual(-1, items.SubtractConstants(mock_input, constant));
 }
Exemple #12
0
        static void Main(string[] args)
        {
            int count = 0;
            Parser parse = new Parser();
            Constant constant = new Constant();
            Evaluate evaluate_input = new Evaluate();
            Char result1;
            Char result2;
            int result3;
            int result4;

            while (true)
            {
                Console.Write("[{0}]", count);
                string input = Console.ReadLine();
                if (input.ToLower() == "exit" || input.ToLower() == "quit")
                {
                    break;
                }
                bool testIfFirstIsChar = Char.TryParse(parse.GetFirst(input), out result1);
                bool testIfSecondIsChar = Char.TryParse(parse.GetSecond(input), out result2);
                bool testIfFirstIsNum = int.TryParse(parse.GetFirst(input), out result3);
                bool testIfSecondIsNum = int.TryParse(parse.GetSecond(input), out result4);
                if (input.Length == 1)
                {
                    Console.WriteLine(" = {0} is {1}", input, constant.GetConstant(char.Parse(input)));
                }
                else if (testIfFirstIsChar && !testIfFirstIsNum && parse.GetOperator(input) == '=')
                {
                    constant.AddConstant(result1, result4);
                    Console.WriteLine("= '{0}' is '{1}' ", result1, result4);
                }
                else if (testIfFirstIsChar && !testIfFirstIsNum && testIfSecondIsNum || testIfSecondIsChar && !testIfSecondIsNum && testIfFirstIsNum || !testIfFirstIsNum && !testIfSecondIsNum)
                {
                    Console.WriteLine("= {0}", evaluate_input.EvaluateInput(input, constant));
                }
                else if (testIfFirstIsNum && testIfSecondIsNum)
                {
                    Console.WriteLine("= {0}", evaluate_input.EvaluateInput(input));
                }
                count++;
            }
        }
 public void ConstantEnsureICanRetrieveFromDictionary()
 {
     Constant constant = new Constant();
     constant.AddConstant('x', 5);
     Assert.AreEqual(5, constant.GetConstant('x'));
 }