#include #include #include using namespace std; struct element { int atno; string name, sym; double atwt; }; int read_table(element table[], int maxe) { ifstream ef("/home/www/class/een118/a3010b.txt"); if (ef.fail()) { cout << "Can't read file\n"; exit(1); } table[0].sym = "XXX"; table[0].name = "Doesn'texistium"; table[0].atno = 0; table[0].atwt = -451; int i = 0; while (i < maxe-1) { ef >> table[i+1].sym >> table[i+1].name >> table[i+1].atwt; if (ef.fail()) break; i += 1; table[i].atno = i; } ef.close(); return i; } element lookup(string sy, element table[], int num) { for (int i = 1; i <= num; i += 1) if (sy == table[i].sym) return table[i]; return table[0]; } struct inputgetter { string formula; int pos; }; void reset(inputgetter & ig, string newf) { ig.formula = newf; ig.pos = 0; } char shownext(inputgetter ig) { if (ig.pos >= ig.formula.length()) return '\n'; return ig.formula[ig.pos]; } void moveup(inputgetter & ig) { ig.pos += 1; } string getsymbol(inputgetter & ig) { string sym = ""; char c1 = shownext(ig); if ( ! (c1 >= 'A' && c1 <= 'Z')) { cout << "Error! '" << c1 << "' can not begin a symbol\n"; return "XXX"; } sym += c1; moveup(ig); char c2 = shownext(ig); if (c2 >= 'a' && c2 <= 'z') { moveup(ig); sym += c2; } return sym; } int char_to_int(char c) { return c - '0'; } int string_to_int(string s) { // does not check for errors, does not need to in this prog int result = 0; for (int i = 0; i < s.length(); i += 1) { char c = s[i]; int digval = char_to_int(c); result = result * 10 + digval; } return result; } string getnumber(inputgetter & ig) { string num = ""; char c1 = shownext(ig); if ( ! (c1 >= '0' && c1 <= '9')) { cout << "Error! '" << c1 << "' can not begin a number\n"; return "99999"; } while (c1 >= '0' && c1 <= '9') { num += c1; moveup(ig); c1 = shownext(ig); } return num; } void printelement(element r) { cout << r.atno << " " << r.sym << " " << r.name << " " << r.atwt << "\n"; if (r.atno == 0) cout << "Bad Element '" << r.sym << "'\n"; } struct answer { string formula; double weight; }; answer make_answer(string f, double d) { answer a; a.formula = f; a.weight = d; return a; } answer calculate(inputgetter & in, element table[], int num_els) { string formula = ""; double molwt = 0.0; while (true) { char c1 = shownext(in); if (c1 == '\n' || c1 == ')') { cout << formula << ": " << molwt << "\n"; return make_answer(formula, molwt); } else if (c1 >= 'A' && c1 <= 'Z' || c1 == '(') { double atwt; string sym; if (c1 >= 'A' && c1 <= 'Z') { sym = getsymbol(in); element r = lookup(sym, table, num_els); atwt = r.atwt; } else { moveup(in); answer a = calculate(in, table, num_els); sym = a.formula; atwt = a.weight; if (shownext(in) != ')') cout << "Error in formula\n"; moveup(in); sym = "(" + sym + ")"; } formula += sym; c1 = shownext(in); if (c1 >= '0' && c1 <= '9') { string num = getnumber(in); formula += num; int rep = string_to_int(num); molwt += atwt * rep; cout << sym << ": " << atwt << " * " << rep << " = " << atwt * rep << "\n"; } else { molwt += atwt; cout << sym << ": " << atwt << "\n"; } } else { cout << "Error character '" << c1 << "'\n"; return make_answer("XXXX", -9999); } } } int main() { const int max_elements = 120; element table[max_elements]; int num_els = read_table(table, max_elements); while (true) { string x; inputgetter in; cin >> x; reset(in, x); answer a = calculate(in, table, num_els); if (shownext(in) != '\n') cout << "Error: Unprocessed input '" << shownext(in) << "'\n"; cout << "Final result for " << a.formula << ", weight = " << a.weight << "\n\n"; } }