Cod sursa(job #1064653)

Utilizator acomAndrei Comaneci acom Data 22 decembrie 2013 10:22:03
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.98 kb
#include<fstream>
#include<cstring>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
int n,i,ap[256];
char c,*p,aux[1005],s[1005];
void prel()
{
    int j;
    p=strstr(s," AND");
    while (p)
    {
        strcpy(aux,p+5);
        p[0]='&', p[1]=0;
        strcat(s,aux);
        p=strstr(s," AND");
    }
    p=strstr(s," OR");
    while (p)
    {
        strcpy(aux,p+4);
        p[0]='|', p[1]=0;
        strcat(s,aux);
        p=strstr(s," OR");
    }
    p=strstr(s,"NOT");
    while (p)
    {
        strcpy(aux,p+4);
        p[0]='1', p[1]='^', p[2]=0;
        strcat(s,aux);
        p=strstr(s,"NOT");
    }
    p=strstr(s,"TRUE");
    while (p)
    {
        strcpy(aux,p+4);
        p[0]='1', p[1]=0;
        strcat(s,aux);
        p=strstr(s,"TRUE");
    }
    p=strstr(s,"FALSE");
    while (p)
    {
        strcpy(aux,p+5);
        p[0]='0', p[1]=0;
        strcat(s,aux);
        p=strstr(s,"FALSE");
    }
}
int expresie();
int opor();
int opand();
int opxor();
int main()
{
    int j;
    f.getline(s,1005);
    f>>n;
    prel(); f.get();
    for (j=0;j<n;++j)
    {
        f.get(c);
        i=0, ap[c]=1-ap[c];
        g<<expresie();
    }
    g<<'\n';
    return 0;
}
int expresie()
{
    int r=opor();
    while (s[i]=='|')
    {
        ++i;
        r|=opor();
    }
    return r;
}
int opor()
{
    int r=opand();
    while (s[i]=='&')
    {
        ++i;
        r&=opand();
    }
    return r;
}
int opand()
{
    int r=opxor();
    while (s[i]=='^')
    {
        ++i;
        r^=opxor();
    }
    return r;
}
int opxor()
{
    int r=0;
    if (s[i]=='(')
    {
        ++i;
        r=expresie();
        ++i;
    }
    else
    {
        if (s[i]=='0' || s[i]=='1')
        {
            r=s[i]-'0';
            ++i;
        }
        else if ('A'<=s[i] && s[i]<='Z')
        {
            r=ap[s[i]];
            ++i;
        }
    }
    return r;
}