Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. #include "pch.h"
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6.  
  7. int expression ();
  8. int operation (int op_num);
  9. char operands (std::vector<int>& operands_list);
  10. int operand ();
  11. int my_or ();
  12. int my_and ();
  13. int my_not ();
  14. char readFromString ();
  15.  
  16. std::string enteredString;
  17. int nowReading =-1;
  18.  
  19. int main () {
  20. std::getline (std::cin, enteredString);
  21. int ans = expression ();
  22. char c;
  23. if (ans == 0) {
  24. std::cout << "False" << std::endl;
  25. }
  26. else if (ans == 1) {
  27. std::cout << "True" << std::endl;
  28. }
  29. }
  30.  
  31. char readFromString () {
  32. ++nowReading;
  33. return enteredString[nowReading];
  34. }
  35.  
  36. int expression () {
  37. int ans = 0;
  38. char c1 = ' ', c2 = ' ', c3 = ' ', c4 = ' ', c5 = ' ';
  39. c1 = readFromString ();
  40. if (c1 == 't') {
  41. c2 = readFromString ();
  42. c3 = readFromString ();
  43. c4 = readFromString ();
  44. if (c2 == 'r' && c3 == 'u' && c4 == 'e') {
  45. return 1;
  46. }
  47. else {
  48. ans = -1;
  49. }
  50. }
  51. else if (c1 == 'f') {
  52. c2 = readFromString ();
  53. c3 = readFromString ();
  54. c4 = readFromString ();
  55. c5 = readFromString ();
  56. if (c2 == 'a' && c3 == 'l' && c4 == 's' && c5 == 'e') {
  57. return 0;
  58. }
  59. else {
  60. ans = -1;
  61. }
  62. }
  63. else if (c1 == 'n') {
  64. c2 = readFromString ();
  65. c3= readFromString ();
  66. if (c2 == 'o' && c3 == 't') {
  67. return my_not ();
  68. }
  69. else {
  70. ans = -1;
  71. }
  72. }
  73. else if (c1 == 'a') {
  74. c2 = readFromString ();
  75. c3 = readFromString ();
  76. if (c2 == 'n' && c3 == 'd') {
  77. return my_and ();
  78. }
  79. else {
  80. ans = -1;
  81. }
  82. }
  83. else if (c1 == 'o') {
  84. c2 = readFromString ();
  85. if (c2 == 'r') {
  86. return my_or ();
  87. }
  88. else {
  89. ans = -1;
  90. }
  91. }
  92. else {
  93. ans = -1;
  94. }
  95. if (ans == -1) {
  96. std::cout << "ERROR: what a hell is this? Please make sure the cat is far enough away from keyboard" << std::endl;
  97. return -1;
  98. }
  99. }
  100.  
  101. int operation (int op_num) {
  102. std::vector <int> operands_list;
  103. char c;
  104. c = readFromString ();
  105. if (c != '(') {
  106. std::cout << "ERROR: '(' has been expected" << std::endl;
  107. return -1;
  108. }
  109. else {
  110. c = operands (operands_list);
  111. if (c != ')') {
  112. std::cout << "ERROR: ')' has been expected" << std::endl;
  113. return -1;
  114. }
  115. else {
  116. if (op_num == 2) { // NOT operation
  117. if (operands_list.size () != 1) {
  118. std::cout << "ERROR: wrong number of operands. NOT must have only 1 "
  119. "operand" << std::endl;
  120. return -1;
  121. }
  122. else {
  123. if (operands_list[0] == -1) {
  124. return -1;
  125. }
  126. else {
  127. return !operands_list[0];
  128. }
  129. }
  130. }
  131. else if (op_num == 1) { // AND operation
  132. if (operands_list.size () < 2) {
  133. std::cout << "ERROR: wrong number of operands. AND must have 2 or more "
  134. "operands"
  135. << std::endl;
  136. return -1;
  137. }
  138. else {
  139. int ans = 1;
  140. for (int i = 0; i < operands_list.size (); ++i) {
  141. if (operands_list[i] == -1) {
  142. return -1;
  143. }
  144. else {
  145. ans = ans && operands_list[i];
  146. }
  147. }
  148. return ans;
  149. }
  150. }
  151. else if (op_num == 0) { // OR operation
  152. if (operands_list.size () < 2) {
  153. std::cout
  154. << "ERROR: wrong number of operands. OR must have 2 or more "
  155. "operands"
  156. << std::endl;
  157. return -1;
  158. }
  159. else {
  160. int ans = 0;
  161. for (int i = 0; i < operands_list.size (); ++i) {
  162. if (operands_list[i] == -1) {
  163. return -1;
  164. }
  165. else {
  166. ans = ans || operands_list[i];
  167. }
  168. }
  169. return ans;
  170. }
  171. }
  172. }
  173. }
  174. }
  175.  
  176. char operands (std::vector<int> & operands_list) {
  177. operands_list.push_back (operand ());
  178. char c;
  179. c = readFromString();
  180. if (c == ',') {
  181. operands (operands_list);
  182. }
  183. else {
  184. return c;
  185. }
  186. }
  187.  
  188. int operand () { return expression (); }
  189.  
  190. int my_or /*0*/ () { return operation (0); }
  191.  
  192. int my_and /*1*/ () { return operation (1); }
  193.  
  194. int my_not /*2*/ () { return operation (2); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement