Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _opz
- {
- ll const NONE = 1e16;
- bool IsOp(string i)
- {
- return (i == "+" || i == "-" || i == "*" || i == "-");
- }
- ll GetPriority(string i)
- {
- if (i == "*" || i == "/") return 2;
- else if (i == "+" || i == "-") return 1;
- else if (i == "(") return 0;
- }
- string GetOpzOfString(string str)
- {
- string ans = "";
- stack <string> t;
- for (int j = 0; j < str.size(); )
- {
- string uu;
- uu += str[j];
- char i = str[j];
- if ('0' <= i && i <= '9')
- {
- int k = j;
- string v = "";
- while (k < str.size() && '0' <= str[k] && str[k] <= '9')
- {
- v += str[k++];
- }
- j = k;
- ans += v;
- ans += '#';
- }
- else if (t.empty() && IsOp(uu))
- {
- string te; te += i;
- t.push(te);
- j++;
- }
- else if (i == '(') {
- string te; te += i;
- t.push(te), j++;
- }
- else if (i == ')')
- {
- while (true)
- {
- string g = t.top();
- t.pop();
- if (g == "(") break;
- ans += g;
- }
- j++;
- }
- else
- {
- string ko;
- ko += i;
- while (true)
- {
- if (t.size() == 0) break;
- string g = t.top();
- if (GetPriority(g) < GetPriority(ko)) break;
- t.pop();
- ans += g;
- }
- t.push(ko);
- j++;
- }
- }
- while (!t.empty())
- {
- ans += t.top();
- t.pop();
- }
- return ans;
- }
- ll GetValueOfOpz(string str)
- {
- stack <ll> t;
- ll ans = 0;
- for (ll i = 0; i < str.size(); )
- {
- if (str[i] == '#') {
- i++;
- continue;
- }
- if (str[i] == '+')
- {
- ll x = t.top();
- t.pop();
- x += t.top();
- t.pop();
- t.push(x);
- ++i;
- }
- else if (str[i] == '-')
- {
- ll x = -t.top();
- t.pop();
- x += t.top();
- t.pop();
- t.push(x);
- ++i;
- }
- else if (str[i] == '*')
- {
- ll x = t.top();
- t.pop();
- x *= t.top();
- t.pop();
- t.push(x);
- ++i;
- }
- else if (str[i] == '/')
- {
- ll x = t.top();
- t.pop();
- ll y = t.top();
- t.pop();
- if (x == 0) return NONE;
- t.push(y / x);
- ++i;
- }
- else
- {
- int j = i;
- string vl;
- while (j < str.size() && '0' <= str[j] && str[j] <= '9') {
- vl += str[j++];
- }
- i = j;
- t.push(stoi(vl));
- }
- }
- return t.top();
- }
- char GetOp(ll x)
- {
- switch (x)
- {
- case 0:
- return '+';
- case 1:
- return '-';
- case 2:
- return '*';
- case 3:
- return '/';
- }
- }
- char GetOpForMax(ll x)
- {
- switch (x)
- {
- case 0:
- return '*';
- case 1:
- return '+';
- }
- }
- }
- using namespace _opz;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement