Cod sursa(job #2340317)

Utilizator armigheGheorghe Liviu Armand armighe Data 10 februarie 2019 11:32:42
Problema Bool Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include<fstream>
#include<cstring>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
char s[1012];
int v[102];
int n,i=1;

int factor();
int termen();

int calc()
{
    while(s[i]==' ')
        i++;
    int x;
    x=termen();
    while(s[i]=='A'&&s[i+1]=='N'&&s[i+2]=='D')
    {
        i+=3;
        while(s[i]==' ')
        i++;
        x&=termen();
    }
    return x;
}

int termen()
{
    while(s[i]==' ')
        i++;
    int x=factor();
    while(s[i]=='O'&&s[i+1]=='R')
    {
        i+=2;
        while(s[i]==' ')
        i++;
        x|=factor();
    }
    return x;
}

int factor()
{
    int x=0;
    while(s[i]==' ')
        i++;
    if(s[i]=='(')
    {
        i++;
        x=calc();
        i++;
    }
    else
    if(s[i]=='N'&&s[i+1]=='O'&&s[i+2]=='T')
    {
        i+=3;
        while(s[i]==' ')
            i++;
        x=1-factor();
    }
    else
    if(s[i]=='T'&&s[i+1]=='R'&&s[i+2]=='U'&&s[i+3]=='E')
    {
        i+=4;
        while(s[i]==' ')
            i++;
        x=1;
    }
    else
    if(s[i]=='F'&&s[i+1]=='A'&&s[i+2]=='L'&&s[i+3]=='S'&&s[i+4]=='E')
    {
        i+=5;
        while(s[i]==' ')
            i++;
        x=0;
    }
    else
    {
        x=v[s[i]-'A'+1];
        i++;
        while(s[i]==' ')
            i++;
    }
    return x;
}

int main()
{
    int sol,m,j;
    char c;
    f.getline(s+1,1010);
    n=strlen(s+1);
    f>>m;
    for(j=1;j<=m;j++)
    {
        f>>c;
        v[c-'A'+1]=1-v[c-'A'+1];
        i=1;
        sol=calc();
        g<<sol;
    }
    return 0;
}