Chemical formula parser C++
<p>I am currently working on a program that can parse a chemical formula and return molecular weight and percent composition. The following code works very well with compounds such as H<sub>2</sub>O, LiOH, CaCO<sub>3</sub>, and even C<sub>12</sub>H<sub>22</sub>O<sub>11</sub>. However, it is not capable of understanding compounds with polyatomic ions that lie within parenthesis, such as (NH<sub>4</sub>)<sub>2</sub>SO<sub>4</sub>.</p>

<p>I am not looking for someone to necessarily write the program for me, but just give me a few tips on how I might accomplish such a task.</p>

<p>Currently, the program iterates through the inputted string, <code>raw_molecule</code>, first finding each element's atomic number, to store in a vector (I use a <code>map&lt;string, int&gt;</code> to store names and atomic #). It then finds the quantities of each element.</p>

<pre><code>bool Compound::parseString() {
map&lt;string,int&gt;::const_iterator search;
string s_temp;
int i_temp;

for (int i=0; i&lt;=raw_molecule.length(); i++) {
    if ((isupper(raw_molecule[i]))&amp;&amp;(i==0))
        s_temp=raw_molecule[i];
    else if(isupper(raw_molecule[i])&amp;&amp;(i!=0)) {
        // New element- so, convert s_temp to atomic # then store in v_Elements
        search=ATOMIC_NUMBER.find (s_temp);
        if (search==ATOMIC_NUMBER.end()) 
            return false;// There is a problem
        else
            v_Elements.push_back(search-&gt;second); // Add atomic number into vector

        s_temp=raw_molecule[i]; // Replace temp with the new element

    }
    else if(islower(raw_molecule[i]))
        s_temp+=raw_molecule[i]; // E.g. N+=a which means temp=="Na"
    else
        continue; // It is a number/parentheses or something
}
// Whatever's in temp must be converted to atomic number and stored in vector
search=ATOMIC_NUMBER.find (s_temp);
if (search==ATOMIC_NUMBER.end()) 
    return false;// There is a problem
else
    v_Elements.push_back(search-&gt;second); // Add atomic number into vector

// --- Find quantities next --- // 
for (int i=0; i&lt;=raw_molecule.length(); i++) {
    if (isdigit(raw_molecule[i])) {
        if (toInt(raw_molecule[i])==0)
            return false;
        else if (isdigit(raw_molecule[i+1])) {
            if (isdigit(raw_molecule[i+2])) {
                i_temp=(toInt(raw_molecule[i])*100)+(toInt(raw_molecule[i+1])*10)+toInt(raw_molecule[i+2]);
                v_Quantities.push_back(i_temp);
            }
            else {
                i_temp=(toInt(raw_molecule[i])*10)+toInt(raw_molecule[i+1]);
                v_Quantities.push_back(i_temp);
            }

        }
        else if(!isdigit(raw_molecule[i-1])) { // Look back to make sure the digit is not part of a larger number
            v_Quantities.push_back(toInt(raw_molecule[i])); // This will not work for polyatomic ions
        }
    }
    else if(i&lt;(raw_molecule.length()-1)) {
        if (isupper(raw_molecule[i+1])) {
            v_Quantities.push_back(1);
        }
    }
    // If there is no number, there is only 1 atom. Between O and N for example: O is upper, N is upper, O has 1.
    else if(i==(raw_molecule.length()-1)) {
        if (isalpha(raw_molecule[i]))
            v_Quantities.push_back(1);
    }
}

return true;
}
</code></pre>

<p>This is my first post, so if I have included too little (or maybe too much) information, please forgive me. </p>
