#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; } 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; reset(in, x); while (true) { string sym = ""; char c1 = shownext(in); if (c1 == '\n') break; sym += c1; moveup(in); if (c1 >= 'A' && c1 <= 'Z') { char c2 = shownext(in); if (c2 >= 'a' && c2 <= 'z') { moveup(in); sym += c2; } } cout << "symbol read: '" << sym << "'\n"; element r = lookup(sym, table, num_els); cout << r.atno << " " << r.sym << " " << r.name << " " << r.atwt << "\n"; if (r.atno == 0) cout << "Bad symbol '" << x << "'\n"; } } }