Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <windows.h>
- #include <cstdio>
- using namespace std;
- void logic(string arg, vector <string> &stack, int* par){
- cin >> arg;
- if (arg.find("(") != string::npos){
- *par += arg.rfind("(") + 1;
- }
- else if(arg.find(")") != string::npos){
- *par -= (arg.length() - arg.find(")"));
- }
- if (arg.find("TRUE") != string::npos){
- stack.push_back("TRUE");
- }
- else if (arg.find("FALSE") != string::npos){
- stack.push_back("FALSE");
- }
- else if (arg.find("NOT") != string::npos){
- logic(arg, stack, par);
- if (stack.back() == "TRUE"){
- stack.pop_back();
- stack.push_back("FALSE");
- } else{
- stack.pop_back();
- stack.push_back("TRUE");
- }
- }
- else if(arg == "AND"){
- string stackBefore = stack.back();
- logic(arg, stack, par);
- if ((stackBefore == "FALSE") || (stack.back() == "FALSE")){
- stack.pop_back();
- stack.pop_back();
- stack.push_back("FALSE");
- }
- else if ((stackBefore == "TRUE") && (stack.back() == "TRUE")){
- stack.pop_back();
- stack.pop_back();
- stack.push_back("TRUE");
- }
- }
- else if(arg == "OR"){
- string stackBefore = stack.back();
- logic(arg, stack, par);
- if ((stackBefore == "TRUE") || (stack.back() == "TRUE")){
- stack.pop_back();
- stack.pop_back();
- stack.push_back("TRUE");
- }
- else if ((stackBefore == "FALSE") && (stack.back() == "FALSE")){
- stack.pop_back();
- stack.pop_back();
- stack.push_back("FALSE");
- }
- }
- }
- string logicValue(vector <string> &stack, int* par){
- logic("", stack, par);
- if (*par){
- return logicValue(stack, par);
- }
- else{
- return stack.back();
- }
- }
- int main(){
- SetConsoleOutputCP(CP_UTF8);
- ifstream file;
- vector <string> stack;
- int par = 0;
- int method;
- char locate[255];
- cout << "Укажите, каким образом будет считано простое логическое выражение (0 - из файла, 1 - из консоли):" << endl;
- cin >> method;
- if (method){
- cout << "Введите логическое выражение:" << endl;
- cout << logicValue(stack, &par) << endl;
- } else{
- cout << "Введите расположение файла:" << endl;
- cin >> locate;
- freopen(locate, "r", stdin);
- cout << logicValue(stack, &par) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement