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)); }
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; }
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, "*"); }
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)); }
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()); }
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); }
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"); } }
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); }
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); }
public void IntValue() { var token = new MathToken("7", 0); Assert.AreEqual(7, token.IntValue); }
public MultiplyOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 2, proxy, calculator.Multiply) { }
public ICalculatorBuilder AddSymbol(MathToken token) { _calculatorNodes.Add(new CalculatorOperationNode(token, _calculatorNodes.Count)); return(this); }
public SubstractOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 1, proxy, calculator.Substract) { }
public int Resolve(MathToken n1, MathToken n2) { var result = this.proxy.BinaryOperation(this.operation, n1.IntValue, n2.IntValue); return result; }
public void NumberIsNotOperator() { var token = new MathToken("33", 0); Assert.IsFalse(token.IsOperator()); }
public AddOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 1, proxy, calculator.Add) { }
public DivideOperator(MathToken token, ICalculatorProxy proxy, ICalculator calculator) : base(token, 2, proxy, calculator.Divide) { }
public int Resolve(MathToken n1, MathToken n2) { var result = this.proxy.BinaryOperation(this.operation, n1.IntValue, n2.IntValue); return(result); }
public CalculatorOperationNode(MathToken token, int index) { Token = token; Index = index; }