Cod sursa(job #2445168)

Utilizator ViAlexVisan Alexandru ViAlex Data 2 august 2019 21:55:07
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.7 kb

#include <iostream>
#include <string>
#include <stack>
#include <fstream>

bool values[27];

using namespace std;


bool is_letter(char a) {

    return std::isalpha(a);

}

bool is_operator(string &to_check) {
    return to_check == "AND" || to_check == "OR" || to_check == "NOT";
}

bool is_operator(char to_check){

    return to_check=='A' ||to_check=='O'||to_check=='N';

}

int get_priority(char a) {

    switch (a) {
        case 'A':
            return 2;
        case 'O':
            return 1;
        case 'N':
            return 3;
        default:
            return 0;
    }


}


bool apply(char operation,bool a,bool b){

    if(operation=='A')
        return a&&b;
    if(operation=='O')
        return a||b;

}




bool get_value(string&p){
    if(p=="TRUE")
        return true;
    if(p=="FALSE")
        return false;
    return  values[p[0]-'A'];



}

bool get_value(char x){

    if(x=='1')
        return 1;
    if(x=='0')
        return 0;



}


string get_next_string(unsigned &cursor, string &source) {
    string result;

    while (cursor<source.length() &&is_letter(source[cursor])) {
        result += source[cursor++];
    }


    cursor--;
    return result;
}


string get_postfix(string&infix) {
    stack<char> operators;
    string to_return;


    for (unsigned i = 0; i < infix.length(); i++) {
        char here = infix[i];

        if (is_letter(here)) {
            string next_string = get_next_string(i, infix);

            if (is_operator(next_string)) {


                char p=next_string[0];

                while(operators.size()){

                    if(p!='N'){
                        if(get_priority(p)>get_priority(operators.top()))
                            break;
                    }
                    else{
                        if(get_priority(p)>=get_priority(operators.top()))
                            break;
                    }

                    to_return+=operators.top();
                    operators.pop();

                }

                operators.push(p);


            }

            else{

                bool value=get_value(next_string);
                to_return+=to_string(value);

            }

        } else {

            if (here == '(') {

                operators.push(here);

            } else if (here == ')') {

                while (operators.size() && operators.top() != '(') {
                    to_return += operators.top();
                    operators.pop();

                }


                if (operators.size())
                    operators.pop();

            }
        }

    }

    while(operators.size()){
        to_return+=operators.top();
        operators.pop();
    }

    return to_return;
}




bool evaluate_postfix(string postfix){

    stack<bool> values;

    for(int i=0;i<postfix.length();i++){
        char here=postfix[i];


        if(is_operator(here)){

            if(here!='N') {

                bool a = values.top();
                values.pop();
                bool b = values.top();
                values.pop();

                values.push(apply(here, a, b));
            }
            else{
                bool a=values.top();
                values.pop();

                values.push(!a);

            }


        }
        else{

            values.push(get_value(here));
        }



    }


    return values.top();




}



void solve(){

    std::ifstream in("bool.in");
    std::ofstream out("bool.out");
    string a,b,c;
    std::getline(in,a);
    std::getline(in,b);
    std::getline(in,c);

    for(int i=0;i<c.length();i++){

        values[c[i]-'A']=!values[c[i]-'A'];
        string postfix=get_postfix(a);
        out<<evaluate_postfix(postfix);

    }







}



int main() {
    solve();
    return 0;
}