#include #include #include struct element { string name, symbol; double weight; }; element table[105]; element make(string s, string n, double w) { element r; r.symbol=s; r.name=n; r.weight=w; return r; } void print(element e) { cout << e.name << " (" << e.symbol << ") w=" << e.weight << "\n"; } void read() { ifstream efile("elements.txt"); if (efile.fail()) { cerr << "Can't open 'elements.txt'\n"; exit(1); } for (int i=1; i<=104; i+=1) { string sy, nm; double wt; efile >> sy >> nm >> wt; if (efile.fail()) break; table[i]=make(sy, nm, wt); } efile.close(); } struct inputthing { string formula; int pos; }; void read(inputthing & x, string prompt="") { cout << prompt; cin >> x.formula; x.pos=0; } string combine(char a, char b=0) { string s=""; s+=a; if (b!=0) s+=b; return s; } char nextchar(inputthing & w) { if (w.pos>=w.formula.length()) return '*'; char c=w.formula[w.pos]; w.pos+=1; return c; } void putcharback(inputthing & w) { w.pos-=1; } string nextsymbol(inputthing x) { char c1=nextchar(x); if (c1=='*') return "*"; if (c1>='A' && c1<='Z') { char c2=nextchar(x); if (c2>='a' && c2<='z') return combine(c1, c2); putcharback(x); return combine(c1); } else if (c1>='0' && c1<='9') { string num=combine(c1); char c2=nextchar(x); while (c2>='0' && c2<='9') { num+=c2; c2=nextchar(x); } putcharback(x); return num; } } void test() { print(table[11]); print(table[57]); print(table[103]); } void main() { read(); inputthing reader; read(reader, "formula: "); while (true) { string s = nextsymbol(reader); cout << "'" << s << "'\n"; if (s=="*") break; } }