Пример #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));
        }
Пример #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;
 }
Пример #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;
 }
Пример #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, "*");
        }
Пример #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));
        }
Пример #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());
        }
Пример #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());
        }
Пример #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);
        }
Пример #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");
     }
 }
Пример #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);
        }
Пример #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);
        }
Пример #12
0
 public void IntValue()
 {
     var token = new MathToken("7", 0);
     Assert.AreEqual(7, token.IntValue);
 }
Пример #13
0
 public MultiplyOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 2, proxy, calculator.Multiply)
 {
 }
Пример #14
0
 public ICalculatorBuilder AddSymbol(MathToken token)
 {
     _calculatorNodes.Add(new CalculatorOperationNode(token, _calculatorNodes.Count));
     return(this);
 }
Пример #15
0
 public SubstractOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator)
     : base(token, 1, proxy, calculator.Substract)
 {
 }
Пример #16
0
 public int Resolve(MathToken n1, MathToken n2)
 {
     var result = this.proxy.BinaryOperation(this.operation, n1.IntValue, n2.IntValue);
     return result;
 }
Пример #17
0
        public void NumberIsNotOperator()
        {
            var token = new MathToken("33", 0);

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

            return(result);
        }
Пример #22
0
 public SubstractOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 1, proxy, calculator.Substract)
 {
 }
Пример #23
0
        public void IntValue()
        {
            var token = new MathToken("7", 0);

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