相对分子质量计算器

2018-03-16 17:04:29


#include<bits/stdc++.h>
using namespace std;

#define db double
map<string,double>p;
string s,o;
stack<db>t;
double ans;
int r;
db pp;

bool daz(char a)
{
    return (a <= 'Z' && a >= 'A');
}

bool xiz(char a)
{
    return (a <= 'z' && a >= 'a');
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);

    p["H"] = 1;
    p["He"] = 4;
    p["Li"] = 7;
    p["Be"] = 9;
    p["B"] = 11;
    p["C"] = 12;
    p["N"] = 14;
    p["O"] = 16;
    p["Na"] = 23;
    p["Mg"] = 24;
    p["Al"] = 27;
    p["Si"] = 28;
    p["P"] = 31;
    p["S"] = 32;
    p["Cl"] = 35.5;
    p["Ar"] = 40;
    p["K"] = 39;
    p["Ca"] = 40;
    p["Sc"] = 45;
    p["Ti"] = 48;
    p["V"] = 51;
    p["Cr"] = 52;
    p["Mn"] = 55;
    p["Fe"] = 56;
    p["Co"] = 59;
    p["Ni"] = 59;
    p["Cu"] = 64;
    p["Zn"] = 66;
    p["Ga"] = 70;
    p["Ge"] = 73;
    p["As"] = 75;
    p["Se"] = 79;
    p["Br"] = 80;
    p["Kr"] = 86;
    p["Rb"] = 85.5;
    p["Sr"] = 87.62;
    p["Y"] = 88.9;
    p["Zr"] = 91.2;
    p["Nb"] = 92.9;
    p["Mo"] = 96;
    p["Tc"] = 99;
    p["Ru"] = 101;
    p["Rh"] = 103;
    p["Pd"] = 106.4;
    p["Ag"] = 108;
    p["Cd"] = 112.4;
    p["In"] = 115;
    p["Sn"] = 118.6;
    p["Sb"] = 121.7;
    p["Te"] = 127.6;
    p["I"] = 127;
    p["Xe"] = 151.5;
    p["Cs"] = 132.9;
    p["Ba"] = 157.55;
    p["La"] = 139;
    p["Ce"] = 140;
    p["Pr"] = 141;
    p["Nd"] = 144;
    p["Pm"] = 147;
    p["Sm"] = 150.4;
    p["Eu"] = 152;
    p["Gd"] = 157.2;
    p["Tb"] = 159;
    p["Dy"] = 162.5;
    p["Ho"] = 165;
    p["Er"] = 167.2;
    p["Tm"] = 169;
    p["Yb"] = 173;
    p["Lu"] = 175;
    p["Hf"] = 178.4;
    p["Ta"] = 181;
    p["W"] = 186;
    p["Re"] = 186;
    p["Os"] = 190.2;
    p["Ir"] = 192.2;
    p["Pt"] = 195;
    p["Au"] = 197;
    p["Ra"] = 226;
    cin>>s;
    s += "#";
    t.push(0);
    for(int i = 0; i < s.size(); i ++)
    {
        //cout<<t.top()<<"\n";
        //cout<<i<<" "<<o<<" "<<r<<"\n";
        //cout<<i<<" "<<s[i]<<" "<<(s[i] == ')')<<"\n";
        if(s[i] == '(')
        {
            if(r == 0) r = 1;
            int rt = t.top();
            p["skser"] = pp;
            t.pop();
            t.push(rt + p[o]*(db)r);
            r = 0;
            o = s[i];
            t.push(0);
            continue;
        }

        if(daz(s[i]) || s[i] == '#')
        {
            if(r == 0) r = 1;
            int rt = t.top();
            //cout<<rt<<"??"<<r<<"\n";;
            p["skser"] = pp;
            t.pop();
            t.push(rt + p[o]*(db)r);
            r = 0;
            o = s[i];
        }
        else
        if(xiz(s[i]))
        {
            o += s[i];
        }
        else
        {
            if(s[i] <= '9' && s[i] >= '0')
            {
                r = 0;
                do
                {
                    r = r*10+s[i]-48;
                    i ++;
                }while(s[i] <= '9' && s[i] >= '0');
                i --;
            }
        }

        if(s[i] == ')')
        {   
            //cout<<"HAHA"<<"\n";
            if(r == 0) r = 1;
            int rt = t.top();
            p["skser"] = pp;
            t.pop();
            t.push(rt + p[o]*(db)r);
            //cout<<t.top()<<"?\n";
            r = 0;
            o = s[i];
            pp = t.top();
            t.pop();
            if(s[i+1] <= '9' && s[i+1] >= '0')
            {
                i ++;
                r = 0;
                do 
                {
                    r = r*10+s[i]-48;
                    i ++;
                }while(s[i] <= '9' && s[i] >= '0');
            i --;
            }
            //cout<<t.top()<<"?\n";
            o = "skser";
        }
    }
//  if(r == 0) r = 1;
//  p["skser"] = pp;
    ans = t.top();// + p[o]*(db)r;
    printf("%.2f",ans);
    return 0;
}
/*
NaCl
H2SO4
C60
Ca(OH)100

40 + (16+1)*2 = 74
*/