Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //<Логическое выражение> ::= true|false|<операция>(операнды)
- //<операция> ::= and|or|not
- //<операнды> ::= <операнд>, <операнды>|<операнд>
- //<операнд> ::= <логическое выражение>
- #include <iostream>
- #include <string>
- using namespace std;
- bool logic_expression() {
- string operation = "";
- while (operation != "true" && operation != "false" && operation != "and" && operation != "or" && operation != "not") {
- char ch;
- cin >> ch;
- operation += ch;
- if (operation.length() > 5) throw runtime_error("Неизвестная операция");
- }
- if (operation == "true") return true;
- if (operation == "false") return false;
- if (operation == "and" || operation == "or" || operation == "not") {
- char ch;
- cin >> ch;
- if (ch != '(') throw runtime_error("Неправильный формат");
- bool a = logic_expression();
- int count_of_operands = 1;
- if (operation == "not") {
- char ch2;
- cin >> ch2;
- if (ch2 != ')') throw runtime_error("Слишком много!!!!!!");
- cin.putback(ch2);
- return !a;
- }
- cin >> ch;
- while (ch != ')') {
- if (ch != ',') throw runtime_error("Параметры через запятую!!!");
- if (operation == "and") a = a && logic_expression();
- if (operation == "or") a = a || logic_expression();
- cin >> ch;
- ++count_of_operands;
- }
- if (count_of_operands < 2) throw runtime_error("Недостаточно операндов!");
- return a;
- }
- throw runtime_error("Неизвестность!");
- }
- int main () {
- try {
- cout << (logic_expression()?"TRUE":"FALSE");
- } catch (runtime_error err) {
- cerr << err.what() << endl;
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement