Thread: Periodic system - Mapping chemicals to their respective molar masses

  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477

    Periodic system - Mapping chemicals to their respective molar masses

    As the title indicates, I'm building a simple program as an exercise in GUI programming with the API, and for some extra points in chem class, which can calculate the molar mass of a chemical substance.

    Eg, you input something like NaCl, and you'd get: M(NaCl) = 58,4527.

    Anyway, the hard part is mapping each substance to its respective mass. First I thought about just creating two arrays - one double and one char. However, that'd mean extra, extra bloated code due to the annoyance of strcpying into the buffer when hard-coding the masses.

    So I thought I'd just use strings(Which is why this is in the C++ board), and a struct with the mass and name of the substance(Not name, but you know.. Na and Cl and He etc..) and I'd initialize the struct as such:

    Code:
    struct molmass MolMass[] =
    	{
    		{ 1.00794,    "H"  },
    		{ 4.0026,     "He" },
    		{ 6.941,      "Li" },
    		{ 9.01218,    "Be" },
    		{ 10.811,     "B"  },
    		{ 12.011,     "C"  },
                    // ....
    ... and so on.

    However, then I can't help but think that coding up an algorithm that would parse the the text input by the user and then find the mass in the struct would be somewhat annoying, because of different substances with similar names, such as "CO2", which is Carbondioxide, and then "Co" which is cobalt.

    So I had another idea: Just a switch statement, for every letter in the alphabet, then nested if and switch statements to determine if the substance has a second letter in its name, and if so, add the respective mass to the sum of the result.

    Basically, what I'm asking is - Can someone come up with a better idea? Those ideas are all doable, but they all mean a LOT of bloated code which would be hard to maintain, to be honest, but IMO, the switch statement sounds best right now.

    All hints and suggestions will be greatly appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One possible idea would be to build a tokenizer -- tokens stop at the next capital letter (or end of input), so "Co" would be one token, "CO2" would split into two tokens, "C", and "O2". (Optionally, you could make numbers their own token, and remember to multiply the most previous mass by that number, but either way.) You could maybe do a find_if using isupper, or something similar, to find the breaks.

    You could also set up a std::map between element abbreviations and their masses to do the lookup.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Nested if and switch statements sounds ugly.

    It doesn't seem to me like parsing the names would be that difficult. After all, the chemical formulas follow a pretty strict convention (just pay attention to uppercase/lowercase)...I would think the hardest thing would be parsing chemicals like Ca(OH)2 where you have to take into account the parenthesis.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by JaWiB View Post
    Nested if and switch statements sounds ugly.

    It doesn't seem to me like parsing the names would be that difficult. After all, the chemical formulas follow a pretty strict convention (just pay attention to uppercase/lowercase)...I would think the hardest thing would be parsing chemicals like Ca(OH)2 where you have to take into account the parenthesis.
    Yeah, I was going to tackle that by finding closing and opening parenthesis in the text and then parse the text in between.

    Either way, thanks for the suggestions so far, I'll give it a thought for a day or two.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  3. Replies: 3
    Last Post: 06-13-2005, 07:28 AM