Untitled
unknown
plain_text
a month ago
1.9 kB
0
Indexable
Never
#include <iostream> #include <stack> #include <fstream> using namespace std; int main() { const int H = 1; const int C = 12; const int O = 16; ifstream inputF("MASS.IN"); string temp; if (inputF.good()) { getline(inputF, temp); } else { cout << 0 << endl; } stack<char> st; int i = 0, count = 0, sum = 0, mult = 1; while (i < temp.length()) { if (temp[i] == '(') { sum += count; count = 0; st.push('('); i++; } if (temp[i] == ')') { st.pop(); i++; if (temp[i] >= '2' && temp[i] <= '9' && i < temp.length()) { if (temp[0] == '(' && temp[temp.length()-2] == ')') { sum += count; sum *= temp[i]-'0'; count = 0; } else { mult = temp[i]-'0'; } } sum+=mult*count; mult = 1; count = 0; } if (temp[i] == 'H') { count += H; i++; } else if (temp[i] == 'C') { count += C; i++; } else if (temp[i] == 'O') { count += O; i++; } else if (temp[i] >= '2' && temp[i] <= '9') { switch (temp[i-1]) { case 'H': count += H*(temp[i]-'0')-H; break; case 'C': count += C*(temp[i]-'0')-C; break; case 'O': count += O*(temp[i]-'0')-O; break; } i++; } } if (!st.empty()) { sum = 0; } if (sum == 0) { sum = count; } ofstream outputF("MASS.OUT"); outputF << sum; outputF.close(); return 0; }
Leave a Comment