Cod sursa(job #3288433)

Utilizator 9onelostSendrescu Tudor-Gabriel 9onelost Data 22 martie 2025 11:41:44
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <bits/stdc++.h>
#define DIM 1050
using namespace std;

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

int k,n, i =0;

int F[130];

char s[DIM],t[DIM];

char c;

int or_op();

int and_op();

int not_op();

int factor();

void parse(){

    for(int i=0;t[i]!=0;i++){

        if(t[i] == ' '){

            continue;

        }

        if(t[i] == 'A' && t[i+1] == 'N'){

            s[k++] = '&';

            i+=2;

            continue;

        }

        if(t[i] == '(' || t[i] == ')'){

                s[k++] = t[i];

                continue;

        }

        if(t[i] == 'O' && t[i+1] == 'R'){

            s[k++] = '|';

            i++;

            continue;

        }

        if(t[i] == 'N' && t[i+1] == 'O'){

            s[k++] = '1';

            s[k++] = '^';

            i+=2;

            continue;

        }

        if(t[i] == 'T' && t[i+1] == 'R'){

            s[k++] = '1';

            i+=3;

            continue;

        }

        if(t[i] == 'F' && t[i+1] == 'A'){

            s[k++] = '0';

            i+=4;

            continue;

        }

        s[k++] = t[i];

    }

}

int or_op(){

    int ret = and_op();

    while(s[i] == '|'){

        i++;

        ret=(ret|and_op());

    }

    return ret;

}

int and_op(){

    int ret = not_op();

    while(s[i] == '&'){

        i++;

        ret=(ret & not_op());

    }

    return ret;

}

int not_op(){

    int ret = factor();

    while(s[i] == '^'){

        i++;

        ret = (ret^factor());

    }

    return ret;

}

int factor(){

    int ret;

    if(s[i] == '('){

        i++;

        ret = or_op();

        i++;

    }

    else if(s[i] >= 'A' && s[i] <= 'Z'){

        ret = F[s[i++]];

    }

    else if(s[i] >= '0' && s[i] <='9'){

        ret = s[i] - '0';

        i++;

    }

    return ret;

}

int main(){

    fin.getline(t,DIM);

    parse();

    fin >> n;

    fin.get();

    for(int j=1;j<=n;j++){

        fin >> c;

        F[c] = 1 - F[c];

        i = 0;

        fout << or_op();

    }

    return 0;

}