public void Multiply(Summand summand) { Multiplier *= summand.Multiplier; foreach (var variable in summand.Variables.Keys) { AddVariable(variable, summand.Variables[variable]); } }
private List <Summand> ParseMultiply() { var leftSummands = ParseUnary(); while (true) { if (_tokenizer.Token != Token.Multiply && _tokenizer.Token != Token.Number && _tokenizer.Token != Token.Variable && _tokenizer.Token != Token.OpenParens) { return(leftSummands); } if (_tokenizer.Token == Token.Multiply) { _tokenizer.NextToken(); } var rightSummands = ParseUnary(); Summand localSummand = null; if (rightSummands.Count == 1) { localSummand = rightSummands.First(); } else if (leftSummands.Count == 1) { localSummand = leftSummands.First(); } // (x + y)(z + y) - for example. if (localSummand == null) { throw new Exception("Multiplying two parent expressions it not supported"); } if (leftSummands.Count == 1) { leftSummands = rightSummands; } foreach (var summand in leftSummands) { summand.Multiply(localSummand); } } }
public bool IsEqualent(Summand summand) { if (summand.Variables.Count != _variables.Count) { return(false); } foreach (var variable in summand.Variables.Keys) { if (!_variables.ContainsKey(variable)) { return(false); } if (_variables[variable] != summand.Variables[variable]) { return(false); } } return(true); }
private List <Summand> ParseSummands() { if (_tokenizer.Token != Token.Number && _tokenizer.Token != Token.Variable && _tokenizer.Token != Token.OpenParens) { // Upon the village, cross the sky // Creepy gizmo’s flying by // There’s excess in those days // of such gizmos anyways // http://chastushki.net.ru/texts/22 // P.S. Don't get me wrong... throw new Exception($"Unexpect token: {_tokenizer.Token}"); } if (_tokenizer.Token == Token.OpenParens) { _tokenizer.NextToken(); var summands = ParseAddAndSubstract(); if (_tokenizer.Token != Token.CloseParens) { throw new Exception("Invalid count of parens"); } _tokenizer.NextToken(); return(summands); } var result = new List <Summand>(); var summand = new Summand(); summand.Multiplier = 1; bool isValidSummand = false; while (true) { if (_tokenizer.Token == Token.Number) { summand.Multiplier *= _tokenizer.Number; isValidSummand = true; _tokenizer.NextToken(); continue; } // skip '*' if (_tokenizer.Token == Token.Multiply) { isValidSummand = false; _tokenizer.NextToken(); continue; } if (_tokenizer.Token == Token.Variable) { // TODO: make it with one parameter summand.AddVariable(_tokenizer.Variable.Name, _tokenizer.Variable.Power); isValidSummand = true; _tokenizer.NextToken(); continue; } if (isValidSummand) { result.Add(summand); } else { throw new Exception("Invalid multiplication"); } return(result); } }