#include #include #include struct element { string symbol, name; int atno; double atwt; }; void print(element e) { cout << e.symbol << ", " << e.name << ", number = " << e.atno << ", atomic weight = " << e.atwt << "\n"; } struct input { string line; int pos; }; input read_input() { input i; i.pos = 0; getline(cin, i.line); return i; } char get_next(input & isys) { isys.pos += 1; return isys.line[isys.pos - 1]; } void give_back(input & isys) { isys.pos -= 1; } string read_symbol(input & in) { string s = ""; char c = get_next(in); s += c; c = get_next(in); if (c>='a' && c<='z') s += c; else give_back(in); return s; } int read_table(element array[]) // don't type the &, it is there automatically for arrays { ifstream fi("atwt.txt"); if (fi.fail()) { cout << "Can't open the file\n"; exit(1); } array[0].symbol = "Nx"; array[0].name = "Nonexistium"; array[0].atno = 0; array[0].atwt = 0; int n = 1; while (true) { string sym, name; double wt; fi >> sym >> name >> wt; if (fi.fail()) break; array[n].symbol = sym; array[n].name = name; array[n].atno = n; array[n].atwt = wt; n += 1; } n -= 1; fi.close(); return n; } element find(string symb, element array[], int maxpos) { for (int i=1; i<=maxpos; i+=1) if (array[i].symbol == symb) return array[i]; return array[0]; } void main() { element table[110]; int n = read_table(table); cout << n << " elements read\n"; cout << "Enter molecular formulae:\n"; input in = read_input(); while (true) { string s = read_symbol(in); element e = find(s, table, n); if (e.atno == 0) cout << "no such element\n"; else print(e); } cout << "Bye then.\n"; }