Cod sursa(job #3348795)

Utilizator patrickunudoiBeres Patrick Stefan patrickunudoi Data 24 martie 2026 08:24:15
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.88 kb
#include <bits/stdc++.h>


using namespace std;

int is_operator(const string& op)
{
    if(op == "NOT")
        return 3;
    else if (op == "AND")
        return 2;
    else if (op == "OR")
        return 1;
    return -1;
}
vector<string> tokenize(const string& s)
{
    vector<string> tokens;
    for(int i = 0; i < (int)s.size(); ++i)
    {
        if((i == 0 || s[i - 1] == ' ' || s[i - 1] == '(') && isalnum(s[i]))
        {
            string token;
            while(i < (int)s.size() && isalnum(s[i]))
            {
                token += s[i];
                i++;
            }
            i--;
            tokens.push_back(token);
        }
        else if (s[i] == ' ')
        {
            continue;
        }
        else
        {
            tokens.push_back(string(1, s[i]));
        }
    }
    return tokens;
}

vector<string> infix_to_postfix(const vector<string>& tokens)
{
    vector<string> postfix_tokens;
    stack<string> opstack;
    for(const auto& token : tokens)
    {
       if(token == "(")
           opstack.push(token);
       else if(token == ")")
       {
           while(!opstack.empty() &&  opstack.top() != "(")
           {
               postfix_tokens.push_back(opstack.top());
               opstack.pop();
           }
           if(!opstack.empty())
               opstack.pop();
           // pop paranthesis

       }
       else if(is_operator(token) != -1)
       {
           int priority = is_operator(token);
           if(priority != 3)
           while(!opstack.empty() && is_operator(opstack.top()) >= priority)
           {
               postfix_tokens.push_back(opstack.top());
               opstack.pop();
           }
           opstack.push(token);
       }
       else
           postfix_tokens.push_back(token);
    }
    while(!opstack.empty())
    {
        postfix_tokens.push_back(opstack.top());
        opstack.pop();
    }
    return postfix_tokens;
}

bool compute_postfix(const vector<string> &postfix_tokens, unordered_map<string, bool> &values)
{
    //for(const auto& elem : values)
      //  cout << elem.first << ' ' << elem.second << endl;
    int result;
    stack<bool> st;
    for(const auto& token : postfix_tokens)
    {
        if(is_operator(token) == -1)
        {
            string temp = token;
            st.push(values[temp]);
        }
        else if(is_operator(token) == 3)
        {
            bool operand = st.top();
            st.pop();
            operand = !operand;
            st.push(operand);
            
        }
        else
        {
            bool operand1, operand2;
            bool temp;
            operand2 = st.top();
            st.pop();
            operand1 = st.top();
            st.pop();
            switch(is_operator(token))
            {
                case 2:
                    temp = operand1 && operand2;
                break;
                case 1:
                    temp = operand1 || operand2;
                break;
            }
            st.push(temp);
        }

    }
    result = st.top();
    return result;
}

int main()
{
    ifstream cin("bool.in");
    ofstream cout("bool.out");

    string s;
    int n;
    unordered_map<string, bool> values;

    getline(cin,s);

    vector<string> tokens = tokenize(s);
    vector<string> postfix_tokens = infix_to_postfix(tokens);


    cin >> n;
    string vars;
    cin >> vars;
    
    values["TRUE"] = 1;
    values["FALSE"] = 0;

    for(const auto& c : vars)
        values[string(1, c)] = 0;

    for(const auto& c : vars)
    {
        values[string(1, c)] =  !values[string(1,c)];
        cout << compute_postfix(postfix_tokens, values);
    }
    /**
    vector<string> tokens = tokenize(s);
    for(const auto& token : tokens)
        cout << token << ' ';
    cout << endl;
    
    vector<string> postfix_tokens = infix_to_postfix(tokens);

    for(const auto& token : postfix_tokens)
        cout << token << ' ';
    cout << endl;
    **/
    return 0;
}