static ElementTerm[] FindElements(Compound[] terms) { // I actually do not know how this works or how I wrote it, but it works so // Let's figure out how this works // Create a list of elements that will have the elements added to it as we pass through the terms List<ElementTerm> elements = new List<ElementTerm>(); // Loop through each term for (int termPosition = 0; termPosition < terms.Length; termPosition++) { string term = terms[termPosition].term; // Loop through each character of the string of the term for (int counter = 0; counter < term.Length; counter++) { // If the character is uppercase, marks start of a new element if (char.IsUpper(term, counter)) { // Check if next character is also a letter, then it is a 2 letter element // Ensure that the next letter is still within the limits of the string if (counter + 1 < term.Length && char.IsLower(term, counter + 1)) { // Calculate the coefficient of the element by starting with a 0 string coefficient = "0"; int position = counter + 2; // While the next character is a number and haven't hit end of string yet while (position < term.Length && char.IsNumber(term[position])) { // Add each digit onto the coefficient string coefficient += term[position]; // Move onto the next character position++; } // Add a new element with the calculated coefficient, element symbol and use the position of the term as the position for the element if (coefficient == "0") coefficient = "1"; ElementTerm elementAdd = new ElementTerm(int.Parse(coefficient), term.Substring(counter, 2), termPosition); elements.Add(elementAdd); terms[termPosition].Elements.Add(elementAdd); // Make the next character to check, the position after all the numbers // Need to decrease by one since for loop will increment counter by 1 counter = position - 1; } // else the element is only one letter long else { // Calculate the coefficient of the element by starting with a 0 string coefficient = "0"; int position = counter + 1; // While the next character is a number and haven't hit end of string yet while (position < term.Length && char.IsNumber(term[position])) { // Add each digit onto the coefficient string coefficient += term[position]; // Move onto the next character position++; } if (coefficient == "0") coefficient = "1"; // Add a new element with the calculated coefficient, element symbol and use the position of the term as the position for the element ElementTerm elementAdd = new ElementTerm(int.Parse(coefficient), term.Substring(counter, 1), termPosition); elements.Add(elementAdd); terms[termPosition].Elements.Add(elementAdd); // Make the next character to check, the position after all the numbers // Need to decrease by one since for loop will increment counter by 1 counter = position - 1; } } } } //Return the array of elements return elements.ToArray(); }
public bool Equals(ElementTerm elementTerm) { return Element == elementTerm.Element; }