#pragma TextEncoding = "UTF-8" #pragma rtGlobals=3 #pragma version=1.01 #pragma module=molarmass // by tony.withers@uwo.ca // How to use Molar Mass Calculator // As a calculator: Select Molar Mass Calculator from the macros menu // to create a Molar Mass notebook. Enter formulae on the last line // of the notebook. // From the command line: print molWt("H2O") where H2O is replaced // by the formula of interest. Please use correctly capitalized // element symbols! // Details // No sanity check for charge balance. Parentheses should work, masses // are multiplied by trailing numbers. Non-integers are accepted in // formulae. Prepended multipliers won't work. To include water of // crystallization, rewrite formula so that CaCl2·2H2O becomes // CaCl2(H2O)2 // Edit the line below to set the option to export the calculated mass to // the clipboard when using the calculator static constant kSendToClip=1 function molWt(formula) string formula variable verbose=0 make /free/n=0 AtWt make /free /n=0 /T Element Element = {"H","He","Li","Be","B","C","N","O","F","Ne"} Element[10] = {"Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca"} Element[20] = {"Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn"} Element[30] = {"Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr"} Element[40] = {"Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn"} Element[50] = {"Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd"} Element[60] = {"Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb"} Element[70] = {"Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg"} Element[80] = {"Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th"} Element[90] = {"Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm"} Element[100] = {"Md","No","Lr","Rf","Db","Sg","Bh","Hs","Mt","Ds"} Element[110] = {"Rg","Cn","Uut","Fl","Uup","Lv","Uus","Uuo"} AtWt = {1.00794,4.0026,6.941,9.01218,10.811,12.011,14.0067,15.9994,18.9984,20.1797} AtWt[10] = {22.98977,24.305,26.98154,28.0855,30.97376,32.066,35.4527,39.948,39.0983,40.078} AtWt[20] = {44.9559,47.88,50.9415,51.996,54.938,55.847,58.9332,58.6934,63.546,65.39} AtWt[30] = {69.723,72.61,74.9216,78.96,79.904,83.8,85.4678,87.62,88.9059,91.224} AtWt[40] = {92.9064,95.94,98,101.07,102.9055,106.42,107.868,112.41,114.82,118.71} AtWt[50] = {121.757,127.6,126.9045,131.29,132.9054,137.33,138.9055,140.12,140.9077,144.24} AtWt[60] = {145,150.36,151.965,157.25,158.9253,162.5,164.9303,167.26,168.9342,173.04} AtWt[70] = {174.967,178.49,180.9479,183.85,186.207,190.2,192.22,195.08,196.9665,200.59} AtWt[80] = {204.383,207.2,208.9804,209,210,222,223,226.0254,227,232.0381} AtWt[90] = {231.0359,238.029,237.0482,244,243,247,247,251,252,257} AtWt[100] = {258,259,262,265,268,271,272,270,276,281} AtWt[110] = {280,285,284,289,288,293,294,294} variable level=0 // level of parentheses variable i, mass, amount, lastcount string name, s_num, cmd="" make /free /n=1 w_total // p corresponds to level do mass=0 lastcount=i if (GrepString(formula[i],"[A-Za-z0-9()]")==0) i+=1 // eat up whitespace, etc endif if (cmpstr(formula[i], "(")==0) level+=1 insertpoints numpnts(w_total), 1, w_total if (grepstring(formula[i-1],"[A-Za-z0-9]")) cmd+= "+(" else cmd+= "(" endif i+=1 endif if(cmpstr(formula[i], ")")==0) // transfer total mass from closed parenthesis level to mass variable // so that it can be multiplied by any trailing number mass=w_total[level] w_total[level]=0 name="" level-=1 cmd+= ")" i+=1 if (level<0) return NaN endif elseif( GrepString(formula[i],"[A-Z]") ) name=formula[i] i+=1 do if ( grepstring(formula[i],"[a-z]")) name+=formula[i] i+=1 else break endif while(i1 && strlen(cmd)) sprintf cmd, "%s = %g"cmd, w_total[0] endif if (kSendToClip) putscraptext num2str(w_total[0]) endif return cmd end