Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- void pro_del(string &s)
- {
- for (int i = 0; i < s.size(); i++)
- {
- if (s[i] == ' ') { s.erase(i, 1); i--; };
- }
- }
- string NO_SKOBKI(string s)
- {
- for (int i = 0; i < s.size(); i++)
- {
- if (s[i] == ')' || s[i] == '(') { s.erase(i, 1); i--; };
- }
- return s;
- }
- int str_in_int(string s)
- {
- int count = 1;
- int res = 0;
- for (int i = s.size() - 1; i >= 0; i--)
- {
- if (s[i] >= '0' && s[i] <= '9')
- {
- res = res + (s[i] - '0')*count;
- count*=10;
- }
- }
- return res;
- }
- int power_of_God(string s)
- {
- //cout << s << endl;
- if (s.find_first_of("+-*") == string::npos) return str_in_int(s);
- int pr = 0;
- while (true)
- {
- int c = 0;
- for (int i = s.size() - 1; i >= 0; i--)
- {
- if (s[i] == ')') c++; else if (s[i] == '(') c--;
- if (c == pr && s[i] == '+') return power_of_God(s.substr(0, i)) + power_of_God(s.substr(i + 1));
- }
- c = 0;
- for (int i = s.size() - 1; i >= 0; i--)
- {
- if (s[i] == ')') c++; else if (s[i] == '(') c--;
- if (c == pr && s[i] == '-') return power_of_God(s.substr(0, i)) - power_of_God(s.substr(i + 1));
- }
- c = 0;
- for (int i = s.size() - 1; i >= 0; i--)
- {
- if (s[i] == ')') c++; else if (s[i] == '(') c--;
- if (c == pr && s[i] == '*') return power_of_God(s.substr(0, i)) * power_of_God(s.substr(i + 1));
- }
- pr++;
- }
- }
- int main()
- {
- string s1;
- cin >> s1;
- //if (power_of_God(s1) == power_of_God(NO_SKOBKI(s1))) cout << 1; else cout << 0;
- cout << power_of_God(s1);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement