/// <summary> /// Add the given multiple of formulaToAdd into targetFormula /// </summary> /// <param name="targetFormula"></param> /// <param name="formulaToAdd"></param> /// <param name="multiple"></param> private void Add(MolecularFormula targetFormula, MolecularFormula formulaToAdd, int multiple) { foreach (var item in formulaToAdd.elementCounts) { targetFormula.Add(item.Key, item.Value * multiple); } }
/// <summary> /// Add the given multiple of mf into to. /// </summary> /// <param name="to"></param> /// <param name="mf"></param> /// <param name="multiple"></param> private void Add(MolecularFormula to, MolecularFormula mf, int multiple) { foreach (String s in mf.elementCounts.Keys) { to.Add(s, ((int)mf.elementCounts[s]) * multiple); } }
public static MolecularFormula Parse(string formula, string name, bool allowNegatives) { if (formula == null) { throw new ArgumentNullException(nameof(formula)); } var match = parsingRegex.Match(formula); if (!match.Success) { throw new ApplicationException("Formula can not be parsed"); } var mf = new MolecularFormula(); var matched = match.Groups["element"].Captures.Count; //Console.WriteLine("Num Groups {0}", matched); // Iterate through the matched groups, updating element counts for (var i = 0; i < matched; i++) { var element = match.Groups["element"].Captures[i].Value; var count = match.Groups["count"].Captures[i].Value; //Console.WriteLine("Element {0}, Count {1}", element, count); // If the symbol is unknown, throw an exception. // The multiple defaults to 1 if not found. So CHHCHH is C2 H4. var multiple = 1; if (count != "") { try { multiple = int.Parse(count); if (multiple < 0 && !allowNegatives) { throw new ApplicationException("Negative multiple " + multiple + " for element " + element + " is not allowed."); } } // This can never actually happen, because the regex makes sure that // only integer values which can be parsed make it to this stage catch (Exception ex) { throw new ApplicationException("The multiple for element " + element + ", " + count + ", was not parseable", ex); } } mf.Add(element, multiple); } mf.formulaString = formula; mf.name = name; return(mf); }
public MolecularFormula Translate(string input, string partRegex) { try { var regex = new Regex(partRegex); var formula = new MolecularFormula(); var matches = regex.Matches(input); foreach (Match match in matches) { if (!partFormulas.TryGetValue(match.Value, out var partFormula)) { throw new ApplicationException("Formula for part " + match.Value + " is unknown."); } formula = formula.Add(partFormula, 1); } return(formula); } catch (Exception e) { throw new ApplicationException("Translation of " + input + " failed. " + e.Message, e); } }
public MolecularFormula Translate(String input, String partRegex) { try { var regex = new Regex(partRegex); var formula = new MolecularFormula(); var matches = regex.Matches(input); foreach (Match match in matches) { var partFormula = (MolecularFormula)this.partFormulas[match.Value]; if (partFormula == null) { throw new ApplicationException("Formula for part " + match.Value + " is unknown."); } formula = formula.Add(partFormula, 1); } return(formula); } catch (Exception e) { throw new ApplicationException("Translation of " + input + " failed. " + e.Message, e); } }