Cod sursa(job #1829400)

Utilizator leraValeria lera Data 14 decembrie 2016 21:53:09
Problema Bool Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <iostream>
#include <fstream>
#include <cctype>
#include <stack>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
string str[1001];
int i,k,j,x2,semn,n,lit[27];
char car;
stack<int>st;
void rez()
{
    if(str[j]!=")")
    {
        x2=st.top();
        st.pop();
        while(!st.empty() && st.top()<0 && st.top()!=-4)
        {
            semn=st.top();
            st.pop();
            if(semn==-3)
                st.push(1-x2);
            else if(semn==-1)st.top()=(st.top()&x2);
            else st.top()=(st.top()|x2);
            x2=st.top();
            st.pop();
        }
        st.push(x2);
    }
    else
    {
        x2=st.top();
        st.pop();
        st.pop();
        st.push(x2);
        str[j]="#";
        rez();
        str[j]=")";
    }
}
int main()
{
    string s,x="";
    getline(fin,s);
    for(i=0;i<s.size();i++)
    {
        if(isalpha(s[i]))x.push_back(s[i]);
        else
        {
            if(x!="")str[++k]=x;
            x="";
            if(s[i]=='(' || s[i]==')')x.push_back(s[i]);
            if(x!="")str[++k]=x;
            x="";
        }
    }
    if(x!="")str[++k]=x;
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>car;
        lit[car-'A']=1-lit[car-'A'];
        for(j=1;j<=k;j++)
        {
            if(str[j]=="FALSE")
            {
                st.push(0);
                rez();
            }
            else if(str[j]=="TRUE")
            {
                st.push(1);
                rez();
            }
            else if(str[j].size()==1 && isalpha(str[j][0]))
            {
                st.push(lit[str[j][0]-'A']);
                rez();
            }
            else if(str[j]==")")rez();
            else if(str[j]=="AND")st.push(-1);
            else if(str[j]=="OR")st.push(-2);
            else if(str[j]=="NOT")st.push(-3);
            else if(str[j]=="(")st.push(-4);
        }
        fout<<st.top();
        st.pop();
    }
    return 0;
}