Ejemplo n.º 1
0
 public int ModuloConstants(string input, Constant constant)
 {
     Parser parse = new Parser();
     int constant_value;
     int number_value;
     Char result1;
     Char result2;
     int result3;
     int result4;
     bool test1 = Char.TryParse(parse.GetFirst(input), out result1);
     bool test2 = Char.TryParse(parse.GetSecond(input), out result2);
     bool test3 = int.TryParse(parse.GetFirst(input), out result3);
     bool test4 = int.TryParse(parse.GetSecond(input), out result4);
     if (test1 && !test3 && test4)
     {
         constant_value = constant.GetConstant(result1);
         number_value = result4;
     }
     else if (test2 && !test4 && test3)
     {
         constant_value = constant.GetConstant(result2);
         number_value = result3;
     }
     else if (test1 && test2 && !test3 && !test4)
     {
         constant_value = constant.GetConstant(result1);
         number_value = constant.GetConstant(result2);
     }
     else
     {
         throw new FormatException();
     }
     return constant_value % number_value;
 }
 public int EvaluateInput(string input, Constant constant)
 {
     Parse parse = new Parse();
     Add add_input = new Add();
     Subtract subtract_input = new Subtract();
     Multiply multiply_input = new Multiply();
     Divide divide_input = new Divide();
     Modulo modulo_input = new Modulo();
     if (parse.OperatorInput(input) == '+')
     {
         return add_input.AddConstants(input, constant);
     }
     if (parse.OperatorInput(input) == '-')
     {
         return subtract_input.SubtractConstants(input, constant);
     }
     if (parse.OperatorInput(input) == '*')
     {
         return multiply_input.MultiplyConstants(input, constant);
     }
     if (parse.OperatorInput(input) == '/')
     {
         return divide_input.DivideConstants(input, constant);
     }
     if (parse.OperatorInput(input) == '%')
     {
         return modulo_input.ModuloConstants(input, constant);
     }
     throw new FormatException();
 }
 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 ConstantTestIfCanGetValueOfConstant()
        {
            Constant constant = new Constant();
            constant.AddToConstants('x', 5);

            Assert.AreEqual(5, constant.GetValueOf('x'));
        }
Ejemplo n.º 5
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++;

                }
            }
        }
Ejemplo n.º 6
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 ConstantCanAddMultipleConstantsToList()
 {
     Constant myConstant = new Constant();
     myConstant.addConst("a", 5);
     myConstant.addConst("b", 8);
     Assert.AreEqual(5, myConstant.getNum("a"));
     Assert.AreEqual(8, myConstant.getNum("b"));
 }
Ejemplo n.º 8
0
 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 ConstantTestIfCanAddToDictionaryIfConstantAlreadyExists()
        {
            Constant constant = new Constant();
            constant.AddToConstants('x', 5);
            constant.AddToConstants('x', 10);

            Assert.AreEqual(5, constant.Constants['x']);
        }
Ejemplo n.º 10
0
 public void ParseAllowsConstantsFirst()
 {
     Constant newConstant = new Constant();
     Parse aplus3 = new Parse("a + 3", newConstant);
     aplus3.addConst("a", 5);
     aplus3.setOperatorIndex();
     Assert.AreEqual(5, aplus3.firstNum());
 }
 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 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 ConstantTestGetExpressionWithIncorrectValues()
        {
            string input = "x = y + 1";
            Constant constant = new Constant(input);
            int expected = 5;

            Assert.AreEqual(expected, constant.GetExpression());
        }
        public void ConstantTestGetExpressionWithOneThingOneRightSide()
        {
            Constant constant = new Constant();
            string input = "x = 12";
            constant.ChangeExpression(input);
            int expected = 12;

            Assert.AreEqual(expected, constant.GetExpression());
        }
        public void ConstantTestGetExpression()
        {
            string input = "x = 12 + 12";
            Constant constant = new Constant();
            constant.ChangeExpression(input);
            int expected = 24;

            Assert.AreEqual(expected, constant.GetExpression());
        }
        public void ConstantTestAddToListMethod()
        {
            Constant constant = new Constant();
            char expectedConst = 'x';
            int expectedNum = 5;
            constant.AddToConstants(expectedConst, expectedNum);

            Assert.AreEqual(expectedNum, constant.Constants[expectedConst]);
        }
        public void ConstantTestGetExpressionWithOtherConstants()
        {
            Constant constant = new Constant();
            string input = "x = 12 + 12";
            constant.ChangeExpression(input);
            constant.GetExpression();
            constant.ChangeExpression("y = x + 2");
            int expected = 26;

            Assert.AreEqual(expected, constant.GetExpression());
        }
Ejemplo n.º 20
0
 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));
 }
Ejemplo n.º 21
0
 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));
 }
Ejemplo n.º 22
0
 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));
 }
Ejemplo n.º 23
0
 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));
 }
Ejemplo n.º 24
0
 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));
 }
Ejemplo n.º 25
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++;
            }
        }
 static void Main(string[] args)
 {
     Console.WriteLine("Calculator");
     int count = 0;
     string input;
     Constant constant = new Constant();
     while (true)
     {
         Console.Write("[{0}]> ", count);
         input = Console.ReadLine();
         if(input == "exit" || input == "quit")
         {
             Console.WriteLine("loggin out");
             break;
         }
         ProcessInput PI = new ProcessInput(input);
         try
         {
             Evaluate ev = new Evaluate(PI.GetFirstInputNumber(), PI.GetOperator(), PI.GetSecondInputNumber());
             Console.WriteLine("   = " + ev.Operate());
         }
         catch
         {
             constant.ChangeExpression(input);
             try
             {
                 Console.WriteLine(constant.GetExpression());
             }
             catch
             {
                 Console.WriteLine("Error! Try again . . .");
             }
         }
         count++;
     }
 }
Ejemplo n.º 27
0
 public void ConstantEnsureICanCreateConstant()
 {
     Constant constant = new Constant();
     Assert.IsNotNull(constant);
 }
Ejemplo n.º 28
0
        static void Main(string[] args)
        {
            int    counter     = 0;
            string lastAnswer  = "";
            string lastCommand = "";

            while (true)
            {
                Console.Write("[{0}]>", counter);

                string inputString = Console.ReadLine();

                //all the patterns

                //exit pattern
                string exitPattern = @"^(exit|quit|EXIT|QUIT)$";
                Match  exit        = Regex.Match(inputString.ToLower(), exitPattern);

                //lastAnswer Pattern
                string lastAnswerPattern = @"^last$";
                Match  lastAnswerP       = Regex.Match(inputString.ToLower(), lastAnswerPattern);

                //lastCommand Pattern
                string lastCommandPattern = @"^lastq$";
                Match  lastCommandP       = Regex.Match(inputString.ToLower(), lastCommandPattern);

                //regular calculation pattern
                string RegurlarCalculationPattern = @"^(?<digit1>[0-9]+)(?<operation>(\+|\-|\*|\/))(?<digit2>[0-9]+)$";
                Match  m1 = Regex.Match(inputString, RegurlarCalculationPattern);

                //modulus pattern
                string modulusPattern   = @"^\|(?<sign>\-?)(?<number>\d+)\|$";
                Match  modulusOperation = Regex.Match(inputString, modulusPattern);

                //x=5 pattern
                string constSetPattern   = @"^(?<constantLetter>[a-zA-Z])(\s?)\=(\s?)(?<constantNumber>\d+)$";
                Match  constantOperation = Regex.Match(inputString, constSetPattern);

                //x pattern
                string constGetPattern = @"^(?<constLetter>[a-zA-Z])$";
                Match  constantGet     = Regex.Match(inputString, constGetPattern);

                //x+1 pattern
                string letterPlusNumPattern = @"^(?<const>[a-zA-Z])(?<operation>(\+|\-|\*|\/))(?<digit>[0-9]+)$";
                Match  constOperation       = Regex.Match(inputString, letterPlusNumPattern);

                //1+x pattern
                string numPlusLetterPattern = @"^(?<digit>[0-9]+)(?<operation>(\+|\-|\*|\/))(?<const>[a-zA-Z])$";
                Match  numPlusLetterP       = Regex.Match(inputString, numPlusLetterPattern);

                //x+y pattern
                string letterPlusLetterPattern = @"^(?<const1>[a-zA-Z])(?<operation>(\+|\-|\*|\/))(?<const2>[a-zA-Z])$";
                Match  letterPlusLetterP       = Regex.Match(inputString, letterPlusLetterPattern);



                //exit pattern
                if (exit.Success == true)
                {
                    break;
                }

                //lastAnswer Pattern
                else if (lastAnswerP.Success == true)
                {
                    Console.WriteLine(lastAnswer);
                }

                //lastCommand Pattern
                else if (lastCommandP.Success == true)
                {
                    Console.WriteLine(lastCommand);
                }

                //regular calculation pattern
                else if (m1.Success == true)
                {
                    lastCommand = inputString;

                    int    number1   = int.Parse(m1.Groups["digit1"].Value);
                    int    number2   = int.Parse(m1.Groups["digit2"].Value);
                    string operation = m1.Groups["operation"].Value;
                    if (operation == "+")
                    {
                        Addition addition       = new Addition();
                        int      additionResult = addition.AdditionOperation(number1, number2);
                        Console.WriteLine("=" + additionResult);
                        lastAnswer = "=" + additionResult;
                        counter++;
                        continue;
                    }
                    else if (operation == "-")
                    {
                        Subtraction subtraction       = new Subtraction();
                        int         subtractionResult = subtraction.SubtractionOperation(number1, number2);
                        Console.WriteLine("=" + subtractionResult);
                        lastAnswer = "=" + subtractionResult;
                        counter++;
                        continue;
                    }
                    else if (operation == "*")
                    {
                        Multiplication multi       = new Multiplication();
                        int            multiResult = multi.MultiplicationOperation(number1, number2);
                        Console.WriteLine("=" + multiResult);
                        lastAnswer = "=" + multiResult;
                        counter++;
                        continue;
                    }
                    else if (operation == "/")
                    {
                        Division divi       = new Division();
                        string   diviResult = divi.DivisionOperation(number1, number2);
                        Console.WriteLine("=" + diviResult);
                        lastAnswer = "=" + diviResult;
                        counter++;
                        continue;
                    }
                }

                //modulus pattern
                else if (modulusOperation.Success == true)
                {
                    lastCommand = inputString;

                    int     modulusNum    = int.Parse(modulusOperation.Groups["number"].Value);
                    string  ifSign        = modulusOperation.Groups["sign"].Value;
                    Modulus modulus       = new Modulus();
                    int     modulusResult = modulus.ModulusFunction(ifSign, modulusNum);
                    Console.WriteLine(modulusResult);
                    lastAnswer = Convert.ToString(modulusResult);
                    counter++;
                    continue;
                }

                //x=5 pattern
                else if (constantOperation.Success == true)
                {
                    lastCommand = inputString;

                    char     consLetter = char.Parse(constantOperation.Groups["constantLetter"].Value.ToLower());
                    double   consNumber = int.Parse(constantOperation.Groups["constantNumber"].Value);
                    Constant consta     = new Constant();
                    lastAnswer = consta.ConstantSetFunction(consLetter, consNumber);
                    counter++;
                    continue;
                }

                //x pattern
                else if (constantGet.Success == true)
                {
                    lastCommand = inputString;

                    char     consLetter = char.Parse(constantGet.Groups["constLetter"].Value.ToLower());
                    Constant consGet    = new Constant();
                    lastAnswer = consGet.ConstantGetFunction(consLetter);
                    counter++;
                    continue;
                }

                //x+1 pattern
                else if (constOperation.Success == true)
                {
                    lastCommand = inputString;

                    char     consLetter = char.Parse(constOperation.Groups["const"].Value.ToLower());
                    string   operation  = constOperation.Groups["operation"].Value;
                    int      number     = int.Parse(constOperation.Groups["digit"].Value);
                    Constant constOper  = new Constant();
                    lastAnswer = constOper.constOperationFunction(consLetter, operation, number);
                    counter++;
                    continue;
                }

                //1+x pattern
                else if (numPlusLetterP.Success == true)
                {
                    lastCommand = inputString;

                    char     consLetter = char.Parse(numPlusLetterP.Groups["const"].Value.ToLower());
                    string   operation  = numPlusLetterP.Groups["operation"].Value;
                    int      number     = int.Parse(numPlusLetterP.Groups["digit"].Value);
                    Constant constOper  = new Constant();
                    lastAnswer = constOper.constOperationFunction(consLetter, operation, number);
                    counter++;
                    continue;
                }

                //x+y pattern
                else if (letterPlusLetterP.Success == true)
                {
                    lastCommand = inputString;

                    char     consLetter1 = char.Parse(letterPlusLetterP.Groups["const1"].Value.ToLower());
                    string   operation   = letterPlusLetterP.Groups["operation"].Value;
                    char     consLetter2 = char.Parse(letterPlusLetterP.Groups["const2"].Value.ToLower());
                    Constant constOper   = new Constant();
                    lastAnswer = constOper.constOperationFunction(consLetter1, operation, consLetter2);
                    counter++;
                    continue;
                }

                else
                {
                    Console.WriteLine("Input Format should be '1+2','|-2|', 'x=1', 'x', 'x+1', '1+x', or 'x+y'");
                    counter++;
                    continue;
                }

                //3. catch error
            }
            Console.WriteLine("End");
            //Console.ReadLine();
        }
        public void ConstantTestIfReturnsValueOfConstant()
        {
            Constant constant = new Constant();
            constant.AddToConstants('x', 12);
            constant.ChangeExpression("x");

            Assert.AreEqual(12, constant.GetExpression());
        }
Ejemplo n.º 30
0
 public void ConstantEnsureICanRetrieveFromDictionary()
 {
     Constant constant = new Constant();
     constant.AddConstant('x', 5);
     Assert.AreEqual(5, constant.GetConstant('x'));
 }
 public void ConstantTestToMakeSureExceptionThrown()
 {
     Constant constant = new Constant();
     Assert.AreEqual(5, constant.GetValueOf('x'));
 }