#include using namespace std; template inline T toInteger(const string&); template<> inline int toInteger(const string& s) { return stoi(s); } template<> inline long toInteger(const string& s) { return stol(s); } template<> inline long long toInteger(const string& s) { return stoll(s); } template inline T toInteger(const string& s, int n) { T res = 0; for (char c : s) { if (isdigit(c)) res = res * n + c - '0'; else if (isalpha(c)) res = res * n + tolower(c) - 'a' + 10; } return s[0] == '-' ? -res : res; } template class Parser { private: string s; int p; unordered_map> unary_operator; vector>> binary_operator; unordered_map brackets; T term() { if (brackets.count(s[p])) { const char end = brackets[s[p]]; ++p; T res = expr(0); if (s[p] != end) throw "mismatch brackets"; ++p; return res; } if (unary_operator.count(s[p])) { auto f = unary_operator[s[p]]; ++p; return f(term()); } // TODO check term is valid or not string res = ""; for (; isdigit(s[p]); ++p) res += s[p]; return toInteger(res); } T expr(size_t level) { if (level == binary_operator.size()) return term(); T res = expr(level + 1); for (char c; c = s[p++], binary_operator[level].count(c);) res = binary_operator[level][c](res, expr(level + 1)); --p; return res; } public: void addUnaryOperator(function func, char c) { unary_operator[c] = func; } void addBinaryOperator(function func, char c, size_t level) { if (binary_operator.size() <= level) binary_operator.resize(level + 1); binary_operator[level][c] = func; } void addBrackets(char begin, char end) { brackets[begin] = end; } long long parse(const string& s) { this->s = s + '@'; p = 0; return expr(0); } }; template inline T parse(const string& s) { Parser parser; parser.addUnaryOperator([](T t){return +t;}, '+'); parser.addUnaryOperator(negate(), '-'); parser.addBinaryOperator(plus(), '+', 0); parser.addBinaryOperator(minus(), '-', 0); parser.addBinaryOperator(multiplies(), '*', 1); parser.addBinaryOperator(divides(), '/', 1); parser.addBrackets('(', ')'); return parser.parse(s); } int main() { Parser parser; parser.addUnaryOperator([](long long t){return +t;}, '+'); parser.addUnaryOperator(negate(), '-'); parser.addBinaryOperator(minus(), '+', 0); parser.addBinaryOperator(plus(), '-', 0); string s; cin >> s; cout << parser.parse(s) << endl; }