Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. //<Логическое выражение> ::= true|false|<операция>(операнды)
  2. //<операция> ::= and|or|not
  3. //<операнды> ::= <операнд>, <операнды>|<операнд>
  4. //<операнд> ::= <логическое выражение>
  5.  
  6. #include <iostream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. bool logic_expression() {
  11. string operation = "";
  12. while (operation != "true" && operation != "false" && operation != "and" && operation != "or" && operation != "not") {
  13. char ch;
  14. cin >> ch;
  15. operation += ch;
  16. if (operation.length() > 5) throw runtime_error("Неизвестная операция");
  17. }
  18. if (operation == "true") return true;
  19. if (operation == "false") return false;
  20. if (operation == "and" || operation == "or" || operation == "not") {
  21. char ch;
  22. cin >> ch;
  23. if (ch != '(') throw runtime_error("Неправильный формат");
  24. bool a = logic_expression();
  25. int count_of_operands = 1;
  26. if (operation == "not") {
  27. char ch2;
  28. cin >> ch2;
  29. if (ch2 != ')') throw runtime_error("Слишком много!!!!!!");
  30. cin.putback(ch2);
  31. return !a;
  32. }
  33. cin >> ch;
  34. while (ch != ')') {
  35. if (ch != ',') throw runtime_error("Параметры через запятую!!!");
  36. if (operation == "and") a = a && logic_expression();
  37. if (operation == "or") a = a || logic_expression();
  38. cin >> ch;
  39. ++count_of_operands;
  40. }
  41. if (count_of_operands < 2) throw runtime_error("Недостаточно операндов!");
  42. return a;
  43. }
  44. throw runtime_error("Неизвестность!");
  45. }
  46.  
  47. int main () {
  48. try {
  49. cout << (logic_expression()?"TRUE":"FALSE");
  50. } catch (runtime_error err) {
  51. cerr << err.what() << endl;
  52. return 1;
  53. }
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement