// Constructor public LinearPolynomial(string linearpolynomial) { // Determine count of unknown members _UnknownCount = 0; // Linear polynomial patterns // in canonical form string leftSidePattern = @"\G((\-?|\+?)((\d+\.{1}\d+)|\d*))[a-z]{1}"; string rigthSidePattern = @"(\-?((\d+\.{1}\d+)|\d+))\Z"; // Try to parse input string PolynomialParser polyParser = new PolynomialParser(leftSidePattern, rigthSidePattern); List<string> members = polyParser.TryParse(linearpolynomial); try { // Format result values if (members.Count != 0) { foreach (string item in members) { if (item.Contains("!!!")) _Members.Add("!!!", item.Substring(0, item.Length - 3)); else if (item.Length == 1) _Members.Add(item, "1"); else _Members.Add(item[item.Length - 1].ToString(), item.Substring(0, item.Length - 1)); } List<string> keys = new List<string>(); foreach (var item in _Members) if (item.Value == "+" || item.Value == "-") keys.Add(item.Key); foreach (string key in keys) _Members[key] = _Members[key] + "1"; // Determine count of unknown members _UnknownCount = _Members.Count - 1; } } catch (Exception) { _Members.Clear(); _UnknownCount = 0; } }
public void TryParseValidStr_2_roots(string validInputStr, string root_1, string root_2, string free) { // Linear polynomial patterns string leftSidePattern = @"\G((\-?|\+?)((\d+\.{1}\d+)|\d*))[a-z]{1}"; string rigthSidePattern = @"(\-?((\d+\.{1}\d+)|\d+))\Z"; PolynomialParser polyParser = new PolynomialParser(leftSidePattern, rigthSidePattern); // Actual results of parsing List<string> actualResult = polyParser.TryParse(validInputStr); // Asserts Assert.AreEqual(3, actualResult.Count); Assert.AreEqual(root_1, actualResult[0]); Assert.AreEqual(root_2, actualResult[1]); Assert.AreEqual(free, actualResult[2]); }
public void TryParseNotValidStr(string notValidInputStr) { // Linear polynomial patterns string leftSidePattern = @"\G((\-?|\+?)((\d+\.{1}\d+)|\d*))[a-z]{1}"; string rigthSidePattern = @"(\-?((\d+\.{1}\d+)|\d+))\Z"; PolynomialParser polyParser = new PolynomialParser(leftSidePattern, rigthSidePattern); // Actual results of parsing List<string> actualResult = polyParser.TryParse(notValidInputStr); // Assert Assert.AreEqual(0, actualResult.Count); }