Cod sursa(job #2515282)

Utilizator BogdanFarcasBogdan Farcas BogdanFarcas Data 28 decembrie 2019 11:57:52
Problema Bool Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream fin("bool.in");
ofstream fout("bool.out");

char s[1002],x[1002],ch;
int p=0,n,d=0,t;
bool a[202];

bool expresie();
bool factor();
bool termen();

bool expresie()
{
    bool ans1=factor();
    while(x[p]=='|')
    {
        p++;
        ans1=ans1|factor();
    }
    return ans1;
}

bool factor()
{
    bool ans2=termen();
    while(x[p]=='&')
    {
        p++;
        ans2=ans2&termen();
    }
    return ans2;
}

bool termen()
{
    bool ans3,ok=true;
    while(x[p]=='!')
    {
        ok=!ok;
        p++;
    }
    if(x[p]=='1')
    {
        p++;
        if(ok==true)
        return 1;
        else return 0;
    }
    if(x[p]=='0')
    {
        p++;
        if(ok==true)
        return 0;
        else return 1;
    }
    if(x[p]=='(')
    {
        p++;
        if(ok==true)
        ans3=expresie();
        else ans3=!expresie();
        p++;
        return ans3;
    }
    if(x[p]>='A'&&x[p]<='Z')
    {
        p++;
        if(ok==true)
        return a[x[p-1]-'A'];
        else return !a[x[p-1]-'A'];
    }
}

int main()
{
    fin.getline(s,1001);
    fin>>t;
    n=strlen(s);
    int i=0;
    while(i<n)
    {
        if(s[i]=='A'&&s[i+1]=='N'&&s[i+2]=='D')
        {
            x[d++]='&';
            i+=3;
        }
        else if(s[i]=='O'&&s[i+1]=='R')
        {
            x[d++]='|';
            i+=2;
        }
        else if(s[i]==' ')
        {
            i++;
        }
        else if(s[i]=='N'&&s[i+1]=='O'&&s[i+2]=='T')
        {
            x[d++]='!';
            i+=3;
        }
        else if(s[i]=='T'&&s[i+1]=='R'&&s[i+2]=='U'&&s[i+3]=='E')
        {
            x[d++]='1';
            i+=4;
        }
        else if(s[i]=='F'&&s[i+1]=='A'&&s[i+2]=='L'&&s[i+3]=='S'&&s[i+4]=='E')
        {
            x[d++]='1';
            i+=5;
        }
        else
        {
            x[d++]=s[i++];
        }
    }
    x[d]=0;
    fin.get();
    for(int i=0;i<t;i++)
    {
        fin.get(ch);
        p=0;
        a[ch-'A']=!a[ch-'A'];
        fout<<expresie();
    }
    return 0;
}