#include #include #include #include using namespace std; struct input { string line; int pos, len; char kind; // '.' for '.', 'N' for number, 'S' for symbol int which_number; string which_symbol; }; void prepare(input & i, string s) { i.line = s + "."; i.pos = 0; i.len = s.length(); } char next(input & i) { while (i.line[i.pos] <= ' ') i.pos += 1; if (i.line[i.pos] == '.') i.kind = '.'; else if (i.line[i.pos] >= 'A' && i.line[i.pos] <= 'Z') { i.kind = 'S'; i.which_symbol = i.line[i.pos]; i.pos += 1; if (i.line[i.pos] >= 'a' && i.line[i.pos] <= 'z') { i.which_symbol += i.line[i.pos]; i.pos += 1; } } else if (i.line[i.pos] >= '0' && i.line[i.pos] <= '9') { i.kind = 'N'; i.which_number = 0; while (i.line[i.pos] >= '0' && i.line[i.pos] <= '9') { i.which_number = i.which_number * 10 + i.line[i.pos] - '0'; i.pos += 1; } } else { cerr << "Incorrect input item beginning with " << i.line[i.pos] << "\n"; i.kind = '?'; } return i.kind; } string get_symbol(input i) { if (i.kind != 'S') cerr << "asking for symbol when not\n"; return i.which_symbol; } int get_number(input i) { if (i.kind != 'N') cerr << "asking for number when not\n"; return i.which_number; } struct element { string name, sym; int atno; double atwt; }; int read_data(element table[], int tabsize) { ifstream fi("elements.txt"); if (fi.fail()) { cerr << "Can not open elements.txt\n"; return 0; } table[0].atno = 0; table[0].atwt = 0.0; int n = 1; while (true) { string name, symbol; int number; double weight; fi >> number >> name >> symbol >> weight; if (fi.fail()) break; table[n].atno = number; table[n].name = name; table[n].sym = symbol; table[n].atwt = weight; n += 1; } fi.close(); return n; } element find_element(string sym, element table[], int tabsize) { for (int i = 1; i < tabsize; i += 1) if (table[i].sym == sym) return table[i]; return table[0]; } bool process_formula(element table[], int tabsize) { double molwt = 0.0, prev_atwt = 0.0; input in; while (true) { cout << "> "; string s; getline(cin, s); prepare(in, s); while (true) { char k = next(in); if (k == 'S') { cout << "Symbol " << get_symbol(in) << "\n"; element e = find_element(get_symbol(in), table, tabsize); if (e.atwt == 0) cout << "not found\n"; else cout << e.sym << ", wt = " << e.atwt << "\n"; prev_atwt = e.atwt; molwt += e.atwt; } else if (k == 'N') { cout << "Number " << get_number(in) << "\n"; molwt += (get_number(in) - 1) * prev_atwt; } else if (k == '.') { cout << "dot for end\n"; break; } else if (k == '?') break; else cout << "error return " << k << "\n"; } cout << "molecular weight now " << molwt << "\n"; } cout << "Total molecular weight is " << molwt << "\n"; return true; } int test() { input in; while (true) { cout << "> "; string s; getline(cin, s); prepare(in, s); while (true) { char k = next(in); if (k == 'S') cout << "Symbol " << get_symbol(in) << "\n"; else if (k == 'N') cout << "Number " << get_number(in) << "\n"; else if (k == '.') { cout << "dot for end\n"; break; } else if (k == '?') break; else cout << "error return " << k << "\n"; } } } int main() { const int tabsize = 120; element table[tabsize]; int num = read_data(table, tabsize); cout << num << " elements read\n"; if (num == 0) { cerr << "No elements have been discovered yet\n"; exit(1); } while (true) { bool ok = process_formula(table, tabsize); if (! ok) break; } return 0; }