#include #include #include using namespace std; bool isnum(string s) { const int len = s.length(); int i = 0; while (i < len) { char c = s[i]; if (c < '0' || c > '9') return false; i = i + 1; } return true; } const int arrsize = 120; string names[arrsize], symbols[arrsize]; double atwts[arrsize]; int read_elements() { ifstream fin; fin.open("/home/www/class/een118/a3010b.txt"); if (fin.fail()) { cout << "Can't read file\n"; exit(1); } int atno = 0; while (atno < arrsize - 1) { string symbol, name; double atwt; fin >> symbol >> name >> atwt; if (fin.fail()) break; atno = atno + 1; symbols[atno] = symbol; names[atno] = name; atwts[atno] = atwt; } cout << atno << " Elements successfully read\n"; fin.close(); return atno; } int find_elem(string sym, int numels) { int i = 1; while (i <= numels) { if (sym == symbols[i]) return i; i = i + 1; } return 0; } // 0 means not found int main() { int numels = read_elements(); double molwt = 0.0; cout << "Enter a formula: "; bool ok = true; while (true) { string s; cin >> s; if (s == ".") break; int an = find_elem(s, numels); if (an == 0) { cout << s << ": No such element\n"; ok = false; continue; } cout << an << ", "; cout << symbols[an] << ", "; cout << names[an] << ", "; cout << atwts[an] << "\n"; molwt = molwt + atwts[an]; } if (ok) cout << "Molecular weight = " << molwt << "\n"; else cout << "There was an error in the input\n"; }