#include #include #include #include #include using namespace std; struct element { string symbol; double atwt; string name; }; int initialise(element t[]); double find_at_wt(string sym, element tab[], int max); string find_name(string sym, element tab[], int max); struct mystream { string line; int pos; }; void initialise(mystream & ms, string x) { ms.line = x; ms.pos = 0; } void report(string info, mystream ms) { cout << info << ", Mystream: already gone '"; for (int i=0; i= ms.line.length()) return true; else return false; } char sneak_next_char(mystream ms) { if (empty(ms)) return '.'; else return ms.line[ms.pos]; } char get_next_char(mystream & ms) { if (empty(ms)) return '.'; else { char answer = ms.line[ms.pos]; ms.pos += 1; return answer; } } string tostring(char c) { string answer = ""; answer += c; return answer; } string get_next_symbol(mystream & str) { if (isupper(sneak_next_char(str))) { string answer = tostring(get_next_char(str)); if (islower(sneak_next_char(str))) answer += get_next_char(str); return answer; } else return "Error!!!!!!!!"; } int get_next_number(mystream & str) { int total = 0; while (isdigit(sneak_next_char(str))) { char c = get_next_char(str); total = total * 10 + digittoint(c); } return total; } double find_mol_wt(mystream & mystr, element tab[], int max) { double total = 0.0; report("before loop", mystr); while (! empty(mystr)) { string atom = get_next_symbol(mystr); report("in loop", mystr); double aw = find_at_wt(atom, tab, max); cout << atom << "(" << find_name(atom, tab, max) << "): " << aw; if (isdigit(sneak_next_char(mystr))) { int num = get_next_number(mystr); aw *= num; cout << " x " << num << " = " << aw; } cout << "\n"; total+=aw; } return total; } void main() { element table[110]; int numelems = initialise(table); while (true) { cout << "Formula? "; string e; cin >> e; if (cin.fail()) break; mystream min; initialise(min, e); cout << find_mol_wt(min, table, numelems) << "\n"; } } void set(element & e, string s, double a, string n) { e.symbol=s; e.atwt=a; e.name=n; } double find_at_wt(string sym, element tab[], int max) { for (int en=1; en<=max; en+=1) { if (tab[en].symbol == sym) return tab[en].atwt; } cout << "error symbol " << sym << "\n"; return -99999.9; } string find_name(string sym, element tab[], int max) { for (int en=1; en<=max; en+=1) { if (tab[en].symbol == sym) return tab[en].name; } cout << "error symbol " << sym << "\n"; return "Error!!!"; } int initialise(element t[]) { set(t[1], "H", 1.00794, "Hydrogen"); set(t[2], "He", 4.002602, "Helium"); set(t[3], "Li", 6.941, "Lithium"); set(t[4], "Be", 9.012182, "Beryllium"); set(t[5], "B", 10.811, "Boron"); set(t[6], "C", 12.0107, "Carbon"); set(t[7], "N", 14.0067, "Nitrogen"); set(t[8], "O", 15.9994, "Oxygen"); set(t[9], "F", 18.9984032, "Fluorine"); set(t[10], "Ne", 20.1797, "Neon"); set(t[11], "Na", 22.98976928, "Sodium"); set(t[12], "Mg", 24.3050, "Magnesium"); set(t[13], "Al", 26.9815386, "Aluminium"); set(t[14], "Si", 28.0855, "Silicon"); set(t[15], "P", 30.973762, "Phosphorus"); set(t[16], "S", 32.065, "Sulfur"); set(t[17], "Cl", 35.453, "Chlorine"); set(t[18], "Ar", 39.948, "Argon"); set(t[19], "K", 39.0983, "Potassium"); set(t[20], "Ca", 40.078, "Calcium"); set(t[21], "Sc", 44.955912, "Scandium"); set(t[22], "Ti", 47.867, "Titanium"); set(t[23], "V", 50.9415, "Vanadium"); set(t[24], "Cr", 51.9961, "Chromium"); set(t[25], "Mn", 54.938045, "Manganese"); set(t[26], "Fe", 55.845, "Iron"); set(t[27], "Co", 58.933195, "Cobalt"); set(t[28], "Ni", 58.6934, "Nickel"); set(t[29], "Cu", 63.546, "Copper"); set(t[30], "Zn", 65.38, "Zinc"); set(t[31], "Ga", 69.723, "Gallium"); set(t[32], "Ge", 72.64, "Germanium"); set(t[33], "As", 74.92160, "Arsenic"); set(t[34], "Se", 78.96, "Selenium"); set(t[35], "Br", 79.904, "Bromine"); set(t[36], "Kr", 83.798, "Krypton"); set(t[37], "Rb", 85.4678, "Rubidium"); set(t[38], "Sr", 87.62, "Strontium"); set(t[39], "Y", 88.90585, "Yttrium"); set(t[40], "Zr", 91.224, "Zirconium"); set(t[41], "Nb", 92.90638, "Niobium"); set(t[42], "Mo", 95.96, "Molybdenum"); set(t[43], "Tc", 98, "Technetium"); set(t[44], "Ru", 101.07, "Ruthenium"); set(t[45], "Rh", 102.90550, "Rhodium"); set(t[46], "Pd", 106.42, "Palladium"); set(t[47], "Ag", 107.8682, "Silver"); set(t[48], "Cd", 112.411, "Cadmium"); set(t[49], "In", 114.818, "Indium"); set(t[50], "Sn", 118.710, "Tin"); set(t[51], "Sb", 121.760, "Antimony"); set(t[52], "Te", 127.60, "Tellurium"); set(t[53], "I", 126.90447, "Iodine"); set(t[54], "Xe", 131.293, "Xenon"); set(t[55], "Cs", 132.9054519, "Caesium"); set(t[56], "Ba", 137.327, "Barium"); set(t[57], "La", 138.90547, "Lanthanum"); set(t[58], "Ce", 140.116, "Cerium"); set(t[59], "Pr", 140.90765, "Praseodymium"); set(t[60], "Nd", 144.242, "Neodymium"); set(t[61], "Pm", 145, "Promethium"); set(t[62], "Sm", 150.36, "Samarium"); set(t[63], "Eu", 151.964, "Europium"); set(t[64], "Gd", 157.25, "Gadolinium"); set(t[65], "Tb", 158.92535, "Terbium"); set(t[66], "Dy", 162.500, "Dysprosium"); set(t[67], "Ho", 164.93032, "Holmium"); set(t[68], "Er", 167.259, "Erbium"); set(t[69], "Tm", 168.93421, "Thulium"); set(t[70], "Yb", 173.054, "Ytterbium"); set(t[71], "Lu", 174.9668, "Lutetium"); set(t[72], "Hf", 178.49, "Hafnium"); set(t[73], "Ta", 180.94788, "Tantalum"); set(t[74], "W", 183.84, "Tungsten"); set(t[75], "Re", 186.207, "Rhenium"); set(t[76], "Os", 190.23, "Osmium"); set(t[77], "Ir", 192.217, "Iridium"); set(t[78], "Pt", 195.084, "Platinum"); set(t[79], "Au", 196.966569, "Gold"); set(t[80], "Hg", 200.59, "Mercury"); set(t[81], "Tl", 204.3833, "Thallium"); set(t[82], "Pb", 207.2, "Lead"); set(t[83], "Bi", 208.98040, "Bismuth"); set(t[84], "Po", 209, "Polonium"); set(t[85], "At", 210, "Astatine"); set(t[86], "Rn", 222, "Radon"); set(t[87], "Fr", 223, "Francium"); set(t[88], "Ra", 226, "Radium"); set(t[89], "Ac", 227, "Actinium"); set(t[90], "Th", 232.03806, "Thorium"); set(t[91], "Pa", 231.03588, "Protactinium"); set(t[92], "U", 238.02891, "Uranium"); set(t[93], "Np", 237, "Neptunium"); set(t[94], "Pu", 244, "Plutonium"); set(t[95], "Am", 243, "Americium"); set(t[96], "Cm", 247, "Curium"); set(t[97], "Bk", 247, "Berkelium"); set(t[98], "Cf", 251, "Californium"); set(t[99], "Es", 252, "Einsteinium"); set(t[100], "Fm", 257, "Fermium"); set(t[101], "Md", 258, "Mendelevium"); set(t[102], "No", 259, "Nobelium"); set(t[103], "Lr", 262, "Lawrencium"); set(t[104], "Rf", 267, "Rutherfordium"); return 104; }