#include #include #include using namespace std; struct atom { string symbol, name; int atno; double atwt; }; void print(atom a) { cout << "Name: " << a.name << "\n" << "Symbol: " << a.symbol << "\n" << "atwt: " << a.atwt << "\n" << "atno: " << a.atno << "\n"; } bool lookup(atom table[], int N, string wanted, atom & result) { int pos = 0; while (pos < N) { if (table[pos].symbol == wanted) { result = table[pos]; return true; } pos += 1; } return false; } struct type { char what; string symbol; int value; }; type classify(string x) { type result; result.value = 0; for (int i = 0; i < x.length(); i += 1) if (x[i] < '0' || x[i] > '9') { result.what = 'S'; result.symbol = x; return result; } else result.value = result.value * 10 + x[i] - '0'; result.what = 'N'; return result; } int main() { atom table[120]; ifstream elf; elf.open("new.txt"); if (elf.fail()) { cout << "Can't open the file\n"; exit(1); } int pos = 0; while (true) { elf >> table[pos].symbol >> table[pos].name >> table[pos].atno >> table[pos].atwt; if (elf.fail()) break; pos += 1; } elf.close(); double molwt = 0.0, last_atwt = 0.0; while (true) { string wanted; cout << "Enter a symbol: "; cin >> wanted; if (cin.fail()) break; type t = classify(wanted); if (t.what == 'N') { cout << "The number " << t.value << "\n"; molwt += (t.value-1) * last_atwt; } else { atom x; bool ok = lookup(table, pos, wanted, x); if (ok) { print(x); molwt += x.atwt; last_atwt = x.atwt; } else cout << wanted << " not found\n"; } } cout << "\nMolecular weight = " << molwt << "\n"; }