MolWgt - A Molecular Weight Calculator
This program analyzes a chemical formula to calculate the molecular weight (more properly, the molar mass). The program is written in Tcl and is run within a terminal window. Currently, it assumes that tclsh is available.
- If two uppercase letters are next to each other, or an uppercase letter is followed by a "(" or ")", insert a "+" between them.
- If a lowercase letter is followed by an uppercase letter, or by a "(" or ")", insert a "+" between them.
- Precede each capital letter (the beginning of an element symbol) with a "$". This creates a variable out of symbol. For example, Ca becomes $Ca, which contains calcium's atomic mass value.
- Insert a "*" (for multiplication) before each number and a "+" (for addition) after each number.
- Change any "+)" sequences (created in the previous step) to ")".
- Trim off the trailing "+" sign (if any).
- Evaluate the resulting algebraic expression.
The above steps transform the original chemical formula into an algebraic expression which can be evaluated.
The program makes use of Tcl's regular expression substitution (regsub) command. Steps 2 - 7 are handled by one line of code!
- Result: H3PO4 97.995181
- Result: (NH4)2SO4 132.1406
Cool, isn't it?
Click here to download the program. It should run out-of-the-box on most distros of linux that have Tcl installed.
- Create an installation package for Windows.
- Implement a verbose mode to display the final algebraic expression which is being evaluated.
- Allow a chemical formula to be specified as an argument.
- Create a test framework to ensure correct results on all major platforms: Linux, Mac OS X, Windows XP, various other Unixes.
- Determine the best source for the atomic masses and update the values.
- Eliminate step 1 of the algorithm as a separate step; combine it into the same line in which steps 2 - 7 are handled.
- Do rounding to the correct number of significant figures.
Paul Takemura, firstname.lastname@example.org, Last updated: March 14, 2006