Advertisement
Racknoss

Анализатор Арифметических выражений V2.1

Dec 23rd, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. void pro_del(string &s)
  8. {
  9.     for (int i = 0; i < s.size(); i++)
  10.     {
  11.         if (s[i] == ' ') { s.erase(i, 1); i--; };
  12.     }
  13. }
  14.  
  15. string NO_SKOBKI(string s)
  16. {
  17.     for (int i = 0; i < s.size(); i++)
  18.     {
  19.         if (s[i] == ')' || s[i] == '(') { s.erase(i, 1); i--; };
  20.     }
  21.     return s;
  22. }
  23.  
  24.  
  25. int str_in_int(string s)
  26. {
  27.     int count = 1;
  28.     int res = 0;
  29.     for (int i = s.size() - 1; i >= 0; i--)
  30.     {
  31.         if (s[i] >= '0' && s[i] <= '9')
  32.         {
  33.             res = res + (s[i] - '0')*count;
  34.             count*=10;
  35.         }
  36.     }
  37.     return res;
  38. }
  39.  
  40. int power_of_God(string s)
  41. {
  42.     //cout << s << endl;
  43.     if (s.find_first_of("+-*") == string::npos) return str_in_int(s);
  44.     int pr = 0;
  45.     while (true)
  46.     {
  47.         int c = 0;
  48.         for (int i = s.size() - 1; i >= 0; i--)
  49.         {
  50.             if (s[i] == ')') c++; else if (s[i] == '(') c--;
  51.             if (c == pr && s[i] == '+') return power_of_God(s.substr(0, i)) + power_of_God(s.substr(i + 1));
  52.         }
  53.         c = 0;
  54.         for (int i = s.size() - 1; i >= 0; i--)
  55.         {
  56.             if (s[i] == ')') c++; else if (s[i] == '(') c--;
  57.             if (c == pr && s[i] == '-') return power_of_God(s.substr(0, i)) - power_of_God(s.substr(i + 1));
  58.         }
  59.         c = 0;
  60.         for (int i = s.size() - 1; i >= 0; i--)
  61.         {
  62.             if (s[i] == ')') c++; else if (s[i] == '(') c--;
  63.             if (c == pr && s[i] == '*') return power_of_God(s.substr(0, i)) * power_of_God(s.substr(i + 1));
  64.         }
  65.         pr++;
  66.     }
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72.     string s1;
  73.     cin >> s1;
  74.     //if (power_of_God(s1) == power_of_God(NO_SKOBKI(s1))) cout << 1; else cout << 0;
  75.     cout << power_of_God(s1);
  76.     system("pause");
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement