bool CompareLetters(Node aNode, Node bNode) { HashSet <string> aLetters = new HashSet <string>(aNode.GetDict().Keys); HashSet <string> bLetters = new HashSet <string>(bNode.GetDict().Keys); return(aLetters.SetEquals(bLetters)); }
public Polynom(string expression, bool calculatingPowers = true) { string[] unprocessedNodes = Regex.Split(Regex.Replace(expression, @"(?<=[0-9a-zA-Z]+)([\+-])", " $1"), @"(?<=[0-9a-zA-Z]+)\s+(?=[\+-])").Select(x => x.Trim()).ToArray(); unprocessedNodes = unprocessedNodes.Select(x => Regex.Replace(x, @"\s+", "")).ToArray(); Regex regex = new Regex(@"([\+-]?\d*\.?\d*)\*?([a-zA-Z]?)\^?([\+-]?\d*)"); foreach (string node in unprocessedNodes) { Node temp = new Node(); MatchCollection matches = regex.Matches(node); for (int i = 0; i < matches.Count - 1; i++) { Match match = matches[i]; if (match.Groups[1].Value == "" && match.Groups[2].Value == "" || unprocessedNodes.Any(x => x == "")) { MessageBox.Show("Parse error"); break; } double tempK = (match.Groups[1].Value == "" || match.Groups[1].Value == "+") ? 1 : (match.Groups[1].Value == "-" ? -1 : StringToDouble(match.Groups[1].Value)); int tempPower = match.Groups[3].Value == "" ? (match.Groups[2].Value == "" ? 1 : (match.Groups[2].Value == "-" ? -1 : 1)) : StringToInt(match.Groups[3].Value); string tempLetter = match.Groups[2].Value; if (match.Groups[2].Value != "" && match.Groups[3].Value == "0") { if (temp.GetDict().Count > 0) { continue; } tempLetter = ""; tempPower = 0; } if (calculatingPowers && match.Groups[2].Value == "") { tempK = Math.Sign(tempK) * Math.Pow(Math.Abs(tempK), tempPower); tempPower = 0; } if (temp.GetDict().Count == 0) { temp = new Node(tempK, tempPower, tempLetter); } else { if (temp.GetDict().Count == 1 && temp.GetDict().ContainsKey("") && temp.GetDict()[""] == 0) { temp.GetDict().Remove(""); } temp.GetDict()[tempLetter] = tempPower; } } Add(temp); } }
int ComparePowers(Node aNode, Node bNode) { return(aNode.GetDict().Values.Sum() - bNode.GetDict().Values.Sum()); }
bool CompareSets(Node aNode, Node bNode) { return(CompareLetters(aNode, bNode) && aNode.GetDict().Keys.All(x => aNode.GetPower(x) == bNode.GetPower(x))); }