分子量计算器

时间:2021-08-09 14:48:26
<script type="text/javascript">
function Main()
    {
    ReadElements();
    FilterFormula();
    ReadFormula();
    Output();
    }

function ReadElements()
    {
    H = 1.00794;
    D = 2.01408;
    T = 3.01686;
    He = 4.002602;

    Li = 6.941;
    Be = 9.012182;
    B = 10.811;
    C = 12.011;
    N = 14.00674;
    O = 15.9994;
    F = 18.9984032;
    Ne = 20.1797;

    Na = 22.989768;
    Mg = 24.3050;
    Al = 26.981539;
    Si = 28.0855;
    P = 30.973762;
    S = 32.066;
    Cl = 35.4527;
    Ar = 39.948;

    K = 39.0983;
    Ca = 40.078;
    Sc = 44.95591;
    Ti = 47.867;
    V = 50.9415;
    Cr = 51.9961;
    Mn = 54.93805;
    Fe = 55.845;
    Co = 58.9332;
    Ni = 58.6934;
    Cu = 63.546;
    Zn = 65.39;
    Ga = 69.723;
    Ge = 72.61;
    As = 74.92159;
    Se = 78.96;
    Br = 79.904;
    Kr = 83.8;
    
    Rb = 85.4678;
    Sr = 87.62;
    Y = 88.90585;
    Zr = 91.224;
    Nb = 92.90638;
    Mo = 95.94;
    Tc = 98.9063;
    Ru = 101.07;
    Rh = 102.9055;
    Pd = 106.42;
    Ag = 107.8682;
    Cd = 112.411;
    In = 114.818;
    Sn = 118.71;
    Sb = 121.76;
    Te = 127.6;
    I = 126.90447;
    J = 126.90447;
    Xe = 131.29;

    Cs = 132.90543;
    Ba = 137.327;
    Hf = 178.49;
    Ta = 180.9479;
    W = 183.84;
    Re = 186.207;
    Os = 190.23;
    Ir = 192.217;
    Pt = 195.08;
    Au = 196.96654;
    Hg = 200.59;
    Tl = 204.3833;
    Pb = 207.2;
    Bi = 208.98037;
    Po = 208.9824;
    At = 209.9871;
    Rn = 222.0176;

    Fr = 223.0197;
    Ra = 226.0254;

    La = 138.9055;
    Ce = 140.115;
    Pr = 140.90765;
    Nd = 144.24;
    Pm = 146.9151;
    Sm = 150.36;
    Eu = 151.965;
    Gd = 157.25;
    Tb = 158.92534;
    Dy = 162.5;
    Ho = 164.93032;
    Er = 167.26;
    Tm = 168.93421;
    Yb = 173.04;
    Lu = 174.967;
    
    Ac = 227.0278;
    Th = 232.0381;
    Pa = 231.03588;
    U = 238.0289;
    Np = 237.0482;
    Pu = 244.0642;
    Am = 243.0614;
    Cm = 247.0703;
    Bk = 247.0703;
    Cf = 251.0796;
    Es = 252.0829;
    Fm = 257.0951;
    Md = 258.0986;
    No = 259.1009;
    Lr = 260.1053;
    }

function FilterFormula()
    {
    var input = document.Chem.Formula.value;
    var len = input.length;
    filtered_formula = "";     
    for (x=0; x<len; x++)
        {
        var ccode = input.charCodeAt(x);
        if (ccode >= 65 && ccode <= 90  ||
            ccode >= 97 && ccode <= 122 ||
                ccode >= 46 && ccode <= 57)
                    filtered_formula += input.charAt(x);
        if (ccode == 44)
             filtered_formula += ".";
        if (ccode == 40 || ccode == 91 || ccode == 123)
             filtered_formula += "(";
        if (ccode == 41 || ccode == 93 || ccode == 125)
             filtered_formula += ")";
        }    
    }


function ReadFormula()
    {
    var input = filtered_formula;
    var len = input.length;
    translated_formula = input.charAt(0);
    for (x=1; x<len; x++)
        {
        var ccode = input.charCodeAt(x);
        var ccodebef = input.charCodeAt(x-1);
        if (ccode >= 46 && ccode <= 57)
            {if (ccodebef < 46 || ccodebef > 57)
                translated_formula += "*";}
        if (ccode >= 65 && ccode <= 90 && ccodebef != 40)
             translated_formula += "+";
        if (ccode == 40 && ccodebef != 40)
             translated_formula += "+";
        translated_formula += input.charAt(x);
        }    
    }

function Output()
    {
    var mm = eval(translated_formula);
    var rec_mm = 1/mm;
    with(document)
        {
        with(Math)
            {
            if (mm > 1000)
                Chem.Weight.value = round(100*mm)/100 + " g/mol";
            if (mm <= 1000)
                Chem.Weight.value = round(1000*mm)/1000 + " g/mol";    
            if (rec_mm <= 1 && rec_mm > 0.1)
                Chem.Reciproc.value = round(10000*rec_mm)/10 + " mmol/g";
            if (rec_mm <= 0.1 && rec_mm > 0.01)
                Chem.Reciproc.value = round(100000*rec_mm)/100 + " mmol/g";
            if (rec_mm <= 0.01 && rec_mm > 0.001)
                Chem.Reciproc.value = round(1000000*rec_mm)/1000 + " mmol/g";
            if (rec_mm <= 0.001)
                Chem.Reciproc.value = round(10000000*rec_mm)/10000 + " mmol/g";
            }
        }
    }

</SCRIPT>
</FONT><B><FONT color=#ff0000 size=6>分子量在线计算器</FONT><FONT size=5>&nbsp;&nbsp;&nbsp; </FONT></B>
<FORM name=Chem>
<TABLE border=0 width=521 height=180>
<TBODY>
<TR>
<TD height=44 width=114><FONT color=#0000ff size=4><FONT face=宋体><FONT size=5>化学式</FONT><B>:</B></FONT></FONT></TD>
<TD style="FONT-FAMILY: Times New Roman; FONT-SIZE: 30pt" height=44 colSpan=2><FONT size=5><INPUT style="WIDTH: 257px; HEIGHT: 38px" size=38 name=Formula><FONT face=宋体></FONT></FONT></TD></TR>
<TR>
<TD height=44 width=114><FONT size=4><FONT face=宋体><FONT color=#ff0000 size=5>分子量</FONT><B>: </B></FONT></FONT></TD>
<TD height=44 width=400 colSpan=2><FONT size=5><INPUT style="HEIGHT: 32px" size=15 name=Weight><FONT face=宋体>&nbsp; </FONT></FONT><FONT color=#0000ff size=4 face=宋体><SPAN style="BACKGROUND-COLOR: #ffff00">化学式要区分大小写!</SPAN></FONT></TD></TR>
<TR>
<TD width=114><FONT size=4><FONT size=5 face=宋体>倒数</FONT><B><FONT face=宋体>:</FONT> </B></FONT></TD>
<TD height=44 width=400 colSpan=2><FONT size=5><INPUT style="HEIGHT: 32px" size=15 name=Reciproc></FONT></TD></TR>
<TR>
<TD width=114> </TD>
<TD width=75><FONT color=#ff0000 size=4><INPUT onclick=Main() value=计算 type=button></FONT></TD>
<TD width=321><FONT color=#0000ff size=4><INPUT value="清除! 重算" type=reset></FONT></TD></TR></TBODY></TABLE></FORM>