#include #include #include #include #include "chemlib.h" using namespace std; double process_formula(elreader & E, ptable & PT) { double total = 0, previous = 0; while (true) { elreader::item it = E.readpart(); if (it.type == '_' || it.type == ')') return total; else if (it.type == '(') { double subwt = process_formula(E, PT); total = total + subwt; previous = subwt; cout << " Total now " << total << "\n"; } else if (it.type == 'S') { string elem = it.s; cout << elem << " : "; element e = PT.find_sym(elem); e.print(); total = total + e.atwt; previous = e.atwt; cout << " Total now " << total << "\n"; } else if (it.type == 'N') { int n = it.i; cout << "number " << n << "\n"; total = total + previous * (n-1); previous = 0; cout << " Total now " << total << "\n"; } } } int main() { ptable PT; PT.read_table(); if (PT.get_number() < 80) { cerr << "not enough (" << PT.get_number() << ") elements\n"; exit(1); } while (true) { cout << "\n? "; string s; cin >> s; elreader E(s); double result = process_formula(E, PT); cout << "\nTotal molecular weight is " << result << "\n"; } }