#include #include #include using namespace std; struct element { int atno; string sym, name; double atwt; element(int an, string sy, string nm, double aw) { atno = an; sym = sy; name = nm; atwt = aw; } element() { atno = 0; sym = ""; name = ""; atwt = 0; } void print() { cout << "(" << atno << ", " << atno << ", '" << sym << "', '" << name << "', " << atwt << ")"; } }; element makeel(int an, string sy, string nm, double aw) { element e; e.atno = an; e.sym = sy; e.name = nm; e.atwt = aw; return e; } int read_table(element pt[]) { ifstream inf; inf.open("/home/www/class/een118/a3010b.txt"); if (inf.fail()) { cerr << "Can't open file\n"; exit(1); } int atno = 0; while (true) { string sym, name; double atwt; inf >> sym >> name >> atwt; if (inf.fail()) break; atno += 1; element e(atno, sym, name, atwt); pt[atno] = e; } pt[0] = makeel(0, "XXX", "XXXXXXXXX", 0.0); inf.close(); return atno; } element findel(string sy, element pt[], int n) { for (int i = 1; i <= n; i += 1) if (pt[i].sym == sy) return pt[i]; return pt[0]; } string get_symbol() // must only be called is next char is known to be a capital letter { char c1 = cin.get(); char c2 = cin.peek(); if (c2 < 'a' || c2 > 'z') return string("") + c1; cin.get(); return string("") + c1 + c2; } int get_number() // must only be called if next char is know to be a digit { int value = 0; while (true) { char c = cin.peek(); if (c < '0' || c > '9') return value; cin.get(); value = value * 10 + c - '0'; } } int main() { string sym, name; double atwt; element pt[120]; int atno = read_table(pt); const int entries = atno; for (atno = 1; atno <= entries; atno += 1) { pt[atno].print(); cout << "\n"; } while (true) // spaces become a nuisance { bool bad = false; double molwt = 0; cout << "> "; while (true) { if (cin.peek() == '\n') { cin.get(); break; } char nxt = cin.peek(); if (c < 'A' || c > 'Z') { cout << "Element symbol expected but '" << c << "' seen\n"; bad = true; break; } sym = get_symbol(); int mult = 1; nxt = cin.peek(); if (nxt >= '0' && nxt <= '9') mult = get_number(); element e = findel(sym, pt, entries); if (e.atno == 0) { cout << "No element has symbol '" << sym << "'\n"; bad = true; break; } molwt += e.atwt * mult; } if (! bad) cout << "Molecular weight " << molwt << "\n"; } } \\