Advertisement
cincout

Обратная польская запись (с числами)

May 7th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1.  
  2. namespace _opz
  3. {
  4. ll const NONE = 1e16;
  5. bool IsOp(string i)
  6. {
  7. return (i == "+" || i == "-" || i == "*" || i == "-");
  8. }
  9. ll GetPriority(string i)
  10. {
  11. if (i == "*" || i == "/") return 2;
  12. else if (i == "+" || i == "-") return 1;
  13. else if (i == "(") return 0;
  14. }
  15. string GetOpzOfString(string str)
  16. {
  17. string ans = "";
  18. stack <string> t;
  19. for (int j = 0; j < str.size(); )
  20. {
  21. string uu;
  22. uu += str[j];
  23. char i = str[j];
  24. if ('0' <= i && i <= '9')
  25. {
  26. int k = j;
  27. string v = "";
  28. while (k < str.size() && '0' <= str[k] && str[k] <= '9')
  29. {
  30. v += str[k++];
  31. }
  32. j = k;
  33. ans += v;
  34. ans += '#';
  35. }
  36. else if (t.empty() && IsOp(uu))
  37. {
  38. string te; te += i;
  39. t.push(te);
  40. j++;
  41. }
  42. else if (i == '(') {
  43. string te; te += i;
  44. t.push(te), j++;
  45. }
  46. else if (i == ')')
  47. {
  48. while (true)
  49. {
  50. string g = t.top();
  51. t.pop();
  52. if (g == "(") break;
  53. ans += g;
  54. }
  55. j++;
  56. }
  57. else
  58. {
  59. string ko;
  60. ko += i;
  61. while (true)
  62. {
  63. if (t.size() == 0) break;
  64. string g = t.top();
  65. if (GetPriority(g) < GetPriority(ko)) break;
  66. t.pop();
  67. ans += g;
  68. }
  69. t.push(ko);
  70. j++;
  71. }
  72. }
  73. while (!t.empty())
  74. {
  75. ans += t.top();
  76. t.pop();
  77. }
  78. return ans;
  79. }
  80. ll GetValueOfOpz(string str)
  81. {
  82. stack <ll> t;
  83. ll ans = 0;
  84. for (ll i = 0; i < str.size(); )
  85. {
  86. if (str[i] == '#') {
  87. i++;
  88. continue;
  89. }
  90. if (str[i] == '+')
  91. {
  92. ll x = t.top();
  93. t.pop();
  94. x += t.top();
  95. t.pop();
  96. t.push(x);
  97. ++i;
  98. }
  99. else if (str[i] == '-')
  100. {
  101. ll x = -t.top();
  102. t.pop();
  103. x += t.top();
  104. t.pop();
  105. t.push(x);
  106. ++i;
  107. }
  108. else if (str[i] == '*')
  109. {
  110. ll x = t.top();
  111. t.pop();
  112. x *= t.top();
  113. t.pop();
  114. t.push(x);
  115. ++i;
  116. }
  117. else if (str[i] == '/')
  118. {
  119. ll x = t.top();
  120. t.pop();
  121. ll y = t.top();
  122. t.pop();
  123. if (x == 0) return NONE;
  124. t.push(y / x);
  125. ++i;
  126. }
  127. else
  128. {
  129. int j = i;
  130. string vl;
  131. while (j < str.size() && '0' <= str[j] && str[j] <= '9') {
  132. vl += str[j++];
  133. }
  134. i = j;
  135. t.push(stoi(vl));
  136. }
  137. }
  138. return t.top();
  139. }
  140. char GetOp(ll x)
  141. {
  142. switch (x)
  143. {
  144. case 0:
  145. return '+';
  146. case 1:
  147. return '-';
  148. case 2:
  149. return '*';
  150. case 3:
  151. return '/';
  152. }
  153. }
  154. char GetOpForMax(ll x)
  155. {
  156. switch (x)
  157. {
  158. case 0:
  159. return '*';
  160. case 1:
  161. return '+';
  162. }
  163. }
  164. }
  165.  
  166. using namespace _opz;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement