예제 #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;
 }