Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <string>
- int expression ();
- int operation (int op_num);
- char operands (std::vector<int>& operands_list);
- int operand ();
- int my_or ();
- int my_and ();
- int my_not ();
- char readFromString ();
- std::string enteredString;
- int nowReading =-1;
- int main () {
- std::getline (std::cin, enteredString);
- int ans = expression ();
- char c;
- if (ans == 0) {
- std::cout << "False" << std::endl;
- }
- else if (ans == 1) {
- std::cout << "True" << std::endl;
- }
- }
- char readFromString () {
- ++nowReading;
- return enteredString[nowReading];
- }
- int expression () {
- int ans = 0;
- char c1 = ' ', c2 = ' ', c3 = ' ', c4 = ' ', c5 = ' ';
- c1 = readFromString ();
- if (c1 == 't') {
- c2 = readFromString ();
- c3 = readFromString ();
- c4 = readFromString ();
- if (c2 == 'r' && c3 == 'u' && c4 == 'e') {
- return 1;
- }
- else {
- ans = -1;
- }
- }
- else if (c1 == 'f') {
- c2 = readFromString ();
- c3 = readFromString ();
- c4 = readFromString ();
- c5 = readFromString ();
- if (c2 == 'a' && c3 == 'l' && c4 == 's' && c5 == 'e') {
- return 0;
- }
- else {
- ans = -1;
- }
- }
- else if (c1 == 'n') {
- c2 = readFromString ();
- c3= readFromString ();
- if (c2 == 'o' && c3 == 't') {
- return my_not ();
- }
- else {
- ans = -1;
- }
- }
- else if (c1 == 'a') {
- c2 = readFromString ();
- c3 = readFromString ();
- if (c2 == 'n' && c3 == 'd') {
- return my_and ();
- }
- else {
- ans = -1;
- }
- }
- else if (c1 == 'o') {
- c2 = readFromString ();
- if (c2 == 'r') {
- return my_or ();
- }
- else {
- ans = -1;
- }
- }
- else {
- ans = -1;
- }
- if (ans == -1) {
- std::cout << "ERROR: what a hell is this? Please make sure the cat is far enough away from keyboard" << std::endl;
- return -1;
- }
- }
- int operation (int op_num) {
- std::vector <int> operands_list;
- char c;
- c = readFromString ();
- if (c != '(') {
- std::cout << "ERROR: '(' has been expected" << std::endl;
- return -1;
- }
- else {
- c = operands (operands_list);
- if (c != ')') {
- std::cout << "ERROR: ')' has been expected" << std::endl;
- return -1;
- }
- else {
- if (op_num == 2) { // NOT operation
- if (operands_list.size () != 1) {
- std::cout << "ERROR: wrong number of operands. NOT must have only 1 "
- "operand" << std::endl;
- return -1;
- }
- else {
- if (operands_list[0] == -1) {
- return -1;
- }
- else {
- return !operands_list[0];
- }
- }
- }
- else if (op_num == 1) { // AND operation
- if (operands_list.size () < 2) {
- std::cout << "ERROR: wrong number of operands. AND must have 2 or more "
- "operands"
- << std::endl;
- return -1;
- }
- else {
- int ans = 1;
- for (int i = 0; i < operands_list.size (); ++i) {
- if (operands_list[i] == -1) {
- return -1;
- }
- else {
- ans = ans && operands_list[i];
- }
- }
- return ans;
- }
- }
- else if (op_num == 0) { // OR operation
- if (operands_list.size () < 2) {
- std::cout
- << "ERROR: wrong number of operands. OR must have 2 or more "
- "operands"
- << std::endl;
- return -1;
- }
- else {
- int ans = 0;
- for (int i = 0; i < operands_list.size (); ++i) {
- if (operands_list[i] == -1) {
- return -1;
- }
- else {
- ans = ans || operands_list[i];
- }
- }
- return ans;
- }
- }
- }
- }
- }
- char operands (std::vector<int> & operands_list) {
- operands_list.push_back (operand ());
- char c;
- c = readFromString();
- if (c == ',') {
- operands (operands_list);
- }
- else {
- return c;
- }
- }
- int operand () { return expression (); }
- int my_or /*0*/ () { return operation (0); }
- int my_and /*1*/ () { return operation (1); }
- int my_not /*2*/ () { return operation (2); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement