Exemplo n.º 1
0
        public void MinusValueAtStart(string input, MathToken token)
        {
            var calulatorNodes = sut.Parse(input);

            Assert.That(calulatorNodes[0].Index, Is.EqualTo(0));
            Assert.That(((CalulatorValueNode)calulatorNodes[0]).Value, Is.EqualTo(-1m));
        }
Exemplo n.º 2
0
 protected MathOperator(MathToken token, int precedence, ICalculatorProxy proxy, CalculatorProxy.SingleBinaryOperation operation)
     : base(token.Token, token.Index)
 {
     this.Precedence = precedence;
     this.proxy      = proxy;
     this.operation  = operation;
 }
Exemplo n.º 3
0
 protected MathOperator(MathToken token, int precedence, ICalculatorProxy proxy, CalculatorProxy.SingleBinaryOperation operation)
     : base(token.Token, token.Index)
 {
     this.Precedence = precedence;
     this.proxy = proxy;
     this.operation = operation;
 }
Exemplo n.º 4
0
        public void GetMaxPrecedence()
        {
            List <MathToken> tokens = _lexer.GetTokens("3 + 3 * 2");
            MathToken        token  = _precedence.GetMaxPrecedence(tokens);
            MathOperator     op     = OperatorFactory.Create(token.Token);

            Assert.AreEqual(op.Token, "*");
        }
Exemplo n.º 5
0
        public void MathSymbols(string input, MathToken token)
        {
            var calulatorNodes = sut.Parse(input);

            Assert.That(calulatorNodes[0].Index, Is.EqualTo(0));
            Assert.That(((CalulatorValueNode)calulatorNodes[0]).Value, Is.EqualTo(1m));
            Assert.That(calulatorNodes[1].Index, Is.EqualTo(1));
            Assert.That(((CalculatorOperationNode)calulatorNodes[1]).Token, Is.EqualTo(token));
            Assert.That(calulatorNodes[2].Index, Is.EqualTo(2));
            Assert.That(((CalulatorValueNode)calulatorNodes[2]).Value, Is.EqualTo(1m));
        }
Exemplo n.º 6
0
        public void IsOperator()
        {
            var add       = new MathToken("+", 0);
            var substract = new MathToken("-", 0);
            var multiply  = new MathToken("-", 0);
            var divide    = new MathToken("/", 0);

            Assert.IsTrue(add.IsOperator());
            Assert.IsTrue(substract.IsOperator());
            Assert.IsTrue(multiply.IsOperator());
            Assert.IsTrue(divide.IsOperator());
        }
Exemplo n.º 7
0
        public void IsOperator()
        {
            var add = new MathToken("+", 0);
            var substract = new MathToken("-", 0);
            var multiply = new MathToken("-", 0);
            var divide = new MathToken("/", 0);

            Assert.IsTrue(add.IsOperator());
            Assert.IsTrue(substract.IsOperator());
            Assert.IsTrue(multiply.IsOperator());
            Assert.IsTrue(divide.IsOperator());
        }
Exemplo n.º 8
0
        private List <int> GetIndexs(List <ICalulatorNode> calculatorOperationNodes, MathToken token)
        {
            var indexsOfElement = new List <int>();

            while (calculatorOperationNodes.OfType <CalculatorOperationNode>().Any(e => e.Token == token))
            {
                var index = calculatorOperationNodes.OfType <CalculatorOperationNode>().First(el => el.Token == token)
                            .Index;
                indexsOfElement.Add(index);
                ((CalculatorOperationNode)calculatorOperationNodes[index]).Token = MathToken.Blank;
            }

            return(indexsOfElement);
        }
Exemplo n.º 9
0
 public MathOperator Create(MathToken token)
 {
     switch (token.Token)
     {
         case "+":
             return new AddOperator(token, this.proxy, this.calculator);
         case "-":
             return new SubstractOperator(token, this.proxy, this.calculator);
         case "*":
             return new MultiplyOperator(token, this.proxy, this.calculator);
         case "/":
             return new DivideOperator(token, this.proxy, this.calculator);
         default:
             throw new ArgumentException("Not recognized operator");
     }
 }
Exemplo n.º 10
0
        private decimal PerformOperation(decimal result, MathToken operation, decimal operationValue)
        {
            switch (operation)
            {
            case MathToken.Add:
                result = math.Add(result, operationValue);
                break;

            case MathToken.Subtract:
                result = math.Sub(result, operationValue);
                break;

            case MathToken.Multiply:
                result = math.Multiply(result, operationValue);
                break;

            case MathToken.Divide:
                result = math.Divide(result, operationValue);
                break;
            }

            return(result);
        }
Exemplo n.º 11
0
        public List <ICalulatorNode> Parse(string input)
        {
            var calulatorNodes = new List <ICalulatorNode>();
            //macthes both : and / as divide symbol
            string Pattern = @"[\d]+|[\*\+\(\)\-\/\:]";
            Regex  rex     = new Regex(Pattern,
                                       RegexOptions.Compiled | RegexOptions.IgnoreCase);
            MatchCollection matches = rex.Matches(input);
            int             index   = 0;

            foreach (Match match in matches)
            {
                int parsedresult;
                var tryParse = int.TryParse(match.Value, out parsedresult);
                if (tryParse)
                {
                    if (CheckIFMinus(calulatorNodes))
                    {
                        calulatorNodes[index - 1] = new CalulatorValueNode(-parsedresult, index - 1);
                    }
                    else
                    {
                        calulatorNodes.Add(new CalulatorValueNode(parsedresult, index));
                        index++;
                    }
                }
                else
                {
                    MathToken token = ParseToken(match.Value);
                    calulatorNodes.Add(new CalculatorOperationNode(token, index));
                    index++;
                }
            }

            return(calulatorNodes);
        }
Exemplo n.º 12
0
 public void IntValue()
 {
     var token = new MathToken("7", 0);
     Assert.AreEqual(7, token.IntValue);
 }
Exemplo n.º 13
0
 public MultiplyOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 2, proxy, calculator.Multiply)
 {
 }
Exemplo n.º 14
0
 public ICalculatorBuilder AddSymbol(MathToken token)
 {
     _calculatorNodes.Add(new CalculatorOperationNode(token, _calculatorNodes.Count));
     return(this);
 }
Exemplo n.º 15
0
 public SubstractOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator)
     : base(token, 1, proxy, calculator.Substract)
 {
 }
Exemplo n.º 16
0
 public int Resolve(MathToken n1, MathToken n2)
 {
     var result = this.proxy.BinaryOperation(this.operation, n1.IntValue, n2.IntValue);
     return result;
 }
Exemplo n.º 17
0
        public void NumberIsNotOperator()
        {
            var token = new MathToken("33", 0);

            Assert.IsFalse(token.IsOperator());
        }
Exemplo n.º 18
0
 public MultiplyOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator)
     : base(token, 2, proxy, calculator.Multiply)
 {
 }
Exemplo n.º 19
0
 public AddOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 1, proxy, calculator.Add)
 {
 }
Exemplo n.º 20
0
 public DivideOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 2, proxy, calculator.Divide)
 {
 }
Exemplo n.º 21
0
        public int Resolve(MathToken n1, MathToken n2)
        {
            var result = this.proxy.BinaryOperation(this.operation, n1.IntValue, n2.IntValue);

            return(result);
        }
Exemplo n.º 22
0
 public SubstractOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 1, proxy, calculator.Substract)
 {
 }
Exemplo n.º 23
0
        public void IntValue()
        {
            var token = new MathToken("7", 0);

            Assert.AreEqual(7, token.IntValue);
        }
Exemplo n.º 24
0
 public void NumberIsNotOperator()
 {
     var token = new MathToken("33", 0);
     Assert.IsFalse(token.IsOperator());
 }
Exemplo n.º 25
0
 public AddOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator)
     : base(token, 1, proxy, calculator.Add)
 {
 }
Exemplo n.º 26
0
 public DivideOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator)
     : base(token, 2, proxy, calculator.Divide)
 {
 }
Exemplo n.º 27
0
 public CalculatorOperationNode(MathToken token, int index)
 {
     Token = token;
     Index = index;
 }