Cod sursa(job #1780261)

Utilizator UrsuDanUrsu Dan UrsuDan Data 15 octombrie 2016 22:51:14
Problema Bool Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <iostream>
#include <fstream>

using namespace std;

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

char s[1050];
char notf[4] = {'N','O','T'};
char andf[4]= {'A','N','D'};
char orf[4]= {'O','R'};
char truf[7]= {'T','R','U','E'};
char falsf[7]= {'F','A','L','S','E'};

int poz;
bool val[35];

int crt();
bool expresie();
bool termen();

bool si(bool a,bool b)
{
    if(a==1 && b==1)
        return 1;
    else
        return 0;
}

bool sau(bool a,bool b)
{
    if(a==1 || b==1)
        return 1;
    else
        return 0;
}

bool fnot()
{
    int i;
    bool ok=1;
    for(i=0; i<=2; i++)
        if(s[i+poz]!=notf[i])
            ok=0;
    if(ok==1)
    {
        poz+=4;
        return 1;
    }
    else
        return 0;
}

int crtop()
{
    int i;
    bool ok=1;
    for(i=0; i<=2; i++)
        if(s[i+poz]!=andf[i])
            ok=0;
    if(ok==1)
        return 2;
    ok=1;
    for(i=0; i<=1; i++)
        if(s[i+poz]!=orf[i])
            ok=0;
    if(ok==1)
        return 3;
    return 5;
}

int crtval()
{
    int ok=1,i;
    if(s[poz]>='A' && s[poz]<='Z')
    {
        poz+=2;
        return val[s[poz-2]-'A'];
    }
    ok=1;
    for(i=0; i<=3; i++)
        if(s[i+poz]!=truf[i])
            ok=0;
    if(ok==1)
    {
        poz+=5;
        return 1;
    }
    ok=1;
    for(i=0; i<=4; i++)
        if(s[i+poz]!=falsf[i])
            ok=0;
    if(ok==1)
    {
        poz+=6;
        return 0;
    }
    return 5;
}

bool termen()
{
    bool ans;
    if(fnot()==0)
    {
        if(s[poz]=='(')
        {
            poz++;
            ans=expresie();
            poz++;
        }
        else
            ans=crtval();
        return ans;
    }
    else
    {
        if(s[poz]=='(')
        {
            poz++;
            ans=!expresie();
            poz++;
        }
        else
            ans=!crtval();
        return ans;
    }
}

bool fand()
{
    bool ans=termen();
    int t;
    while(crtop()==2)
    {
        poz+=4;
        ans=si(ans,termen());
    }
    return ans;
}

bool expresie()
{
    bool ans=fand();
    int t;
    while(crtop()==3)
    {
        poz+=3;
        ans=sau(ans,fand());
    }
    return ans;
}

int main()
{
    int n,i,a=0;
    char c;
    in.get(s,1050);
    in>>n;
    for(i=1; i<=n; i++)
    {
        in>>c;
        val[c-'A']=1-val[c-'A'];
        poz=0;
        out<<expresie();
    }
    return 0;
}