#include #include #include string combine(char a, char b=0) { string s=""; s+=a; if (b!=0) s+=b; return s; } const int max_stack = 100; struct stack { double memories[max_stack]; int nextfree; stack() { nextfree=0; } void push(double x) { if (nextfree>=max_stack) { cerr << "stack overflow\n"; exit(1); } memories[nextfree]=x; nextfree+=1; } double pop() { nextfree-=1; if (nextfree<0) { cerr << "stack underflow\n"; exit(1); } return memories[nextfree]; } }; struct element { string name, symbol; double weight; void make(string s, string n, double w) { symbol=s; name=n; weight=w; } void print() { cout << name << " (" << symbol << ") w=" << weight << "\n"; } }; struct item { string str; int num; char which; /* 'S' means string, 'N' means number */ item(char w) { which=w; } item(string s) { str=s; which='S'; } item(int n) { num=n; which='N'; } }; struct formula { string str; int pos; void read(string prompt="") { cout << prompt; cin >> str; pos=0; } char nextchar() { if (pos>=str.length()) { pos+=1; return '*'; } char c=str[pos]; pos+=1; return c; } void putcharback() { pos-=1; } item nextsymbol() { char c1=nextchar(); if (c1=='*') return item('*'); else if (c1=='(') return item('('); else if (c1==')') return item(')'); else if (c1>='A' && c1<='Z') { char c2=nextchar(); if (c2>='a' && c2<='z') return item(combine(c1, c2)); putcharback(); return item(combine(c1)); } else if (c1>='0' && c1<='9') { int value = c1 - '0'; char c2=nextchar(); while (c2>='0' && c2<='9') { value = value * 10 + c2 - '0'; c2=nextchar(); } putcharback(); return item(value); } else return item('?'); } }; const int max_table_size = 110; struct periodic_table { element table[max_table_size]; int num_elements; void make() { num_elements=0; } void read() { ifstream efile("elements.txt"); if (efile.fail()) { cerr << "Can't open 'elements.txt'\n"; exit(1); } for (int i=0; i> sy >> nm >> wt; if (efile.fail()) break; table[i].make(sy, nm, wt); num_elements=i; } efile.close(); } element find_symbol(string s) { for (int i=0; i