Cod sursa(job #1208943)

Utilizator andrei_diaconuAndrei Diaconu andrei_diaconu Data 16 iulie 2014 20:12:52
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>
#include <string.h>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
char s[1010], chr, tmp[1010];
int n, i, tof[257], j, k, ind;
bool notf();
bool andf();
bool parf();
bool orf()
{
    bool res=andf();
    while (tmp[ind]=='O' && tmp[ind+1]=='R') {
        ind+=2;
        res |= andf();
    }
    return res;
}
bool andf()
{
    bool res=notf();
    while (tmp[ind]=='A' && tmp[ind+1]=='N' && tmp[ind+2]=='D') {
        ind+=3;
        res &= notf();
    }
    return res;
}
bool notf()
{
    bool res=parf();
    while (tmp[ind]=='N' && tmp[ind+1]=='O' && tmp[ind+2]=='T') {
        ind+=3;
        if (tmp[ind]=='N' && tmp[ind+1]=='O' && tmp[ind+2]=='T')
            res=!notf();
        else
            res = !parf();
    }
    return res;
}
bool parf()
{
    bool res;
    if (tmp[ind]=='(') {
        ind++;
        res=orf();
        ind++;
    }
    else {
        if (tmp[ind]=='0' || tmp[ind]=='1') {
            res = tmp[ind]-'0';
            ind++;
        }
    }
    return res;
}
int main()
{
    f.get(s, 1001);
    f>>n;
    for (i=1; i<=n; i++) {
        f>>chr;
        if (tof[chr]==1)
            tof[chr]=0;
        else
            tof[chr]=1;
        k=-1;
        for (j=0; j<strlen(s); j++) {
            if (s[j]=='T' && s[j+1]=='R' && s[j+2]=='U' && s[j+3]=='E') {
                j+=3;
                tmp[++k]='1';
            }
            else if (s[j]=='F' && s[j+1]=='A' && s[j+2]=='L' && s[j+3]=='S' && s[j+4]=='E') {
                j+=4;
                tmp[++k]='0';
            }
            else if (s[j]!=' ') {
                if (s[j]>='A' && s[j]<='Z' && (!(s[j+1]>='A' && s[j+1]<='Z') && !(s[j-1]>='A' && s[j-1]<='Z')))
                    tmp[++k]='0' + tof[s[j]];
                else
                    tmp[++k]=s[j];
            }
        }
        ind=0;
        g<<orf();
    }
    return 0;
}