Cod sursa(job #1878094)

Utilizator GeanaVladGeana Vlad GeanaVlad Data 13 februarie 2017 21:15:10
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <iostream>
#include<fstream>
#include<cstring>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
int n,i,sz,m,p;
char st[110],s[110],c;
bool val[30];

bool termen();
bool factor();

bool eval()
{
    bool r=termen();
    if(s[p]=='&')
    {
        while(s[p]=='&')
        {
            ++p;
            r=r&termen();
        }
    }
    else if(s[p]=='|')
    {
        while(s[p]=='|')
        {
            ++p;
            r=r|termen();
        }
    }
    return r;
}
bool termen()
{
    bool r=factor();
    return r;
}
bool factor()
{
    bool r=0;
    if(s[p]>='A' && s[p]<='Z')
    {
        ++p;
        r=val[s[p]-'A'];
    }
    else if(s[p]=='(')
    {
        ++p;
        r=eval();
        ++p;
    }
    else if(s[p]=='t')
    {
        ++p;
        r=1;
    }
    else if(s[p]=='f')
    {
        ++p;
        r=0;
    }
    else if(s[p]=='!')
    {
        ++p;
        r=!factor();
    }
    return r;
}
int main()
{
    f.getline(st,101);
    n=strlen(st);
    for(i=0;i<n;)
    {
        if(st[i]=='A' && st[i+1]=='N')
            s[++sz]='&',i+=3;
        else if(st[i]=='O' && st[i+1]=='R')
            s[++sz]='|',i+=2;
        else if(st[i]=='N' && st[i+1]=='O')
            s[++sz]='!',i+=3;
        else if(st[i]=='T' && st[i+1]=='R')
            s[++sz]='t',i+=4;
        else if(st[i]=='F' && st[i+1]=='A')
            s[++sz]='f',i+=5;
        else
        {
            if(st[i]<='Z' && st[i]>='A' || st[i]=='(' || st[i]==')')
                s[++sz]=st[i];
            i++;
        }
    }
    f>>m;
    while(m--)
    {
        f>>c;
        val[c-'A']^=1;
        p=1;
        g<<eval();
    }

    return 0;
}