Cod sursa(job #2984246)

Utilizator alex_dacDumitrascu Constantin Alexandru alex_dac Data 23 februarie 2023 19:20:01
Problema Bool Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include<bits/stdc++.h>

using namespace std;

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

char t[1005],s[1005],c;
int i,k,F[130];

int op_or();
int op_and();
int op_xor();
int factor();
void transforma();

int main(){
    int n;
    in.get(s,1005);
    transforma();
    in.get();
    in>>n;
    for(int i=0;i<n;i++){
        in>>c;
        F[c]=1-F[c];
        i=0;
        out<<op_or();
    }
}

int op_or(){
    int r=op_and();
    while(t[i]=='|'){
        i++;
        r=(r | op_and());
    }
    return r;
}

int op_and(){
    int r=op_xor();
    while(t[i]=='&'){
        i++;
        r=(r & op_xor());
    }
    return r;
}

int op_xor(){
    int r=factor();
    while(t[i]=='^'){
        i++;
        r=(r^factor());
    }
    return r;
}

int factor(){
    int r;
    if(t[i]=='('){
        i++;
        r=op_or();
        i++;
    }
    else{
        if(t[i]=='0' || t[i]=='1'){
            i++;
            return(t[i]=='1');
        }
    return F[ t[i++] ];
    }
    return r;
}

void transforma(){
    for(i=0;s[i]!=0;i++){
        if(s[i]==' ')
            continue;
        if(s[i]=='(' || s[i]==')'){
            t[k++]=s[i];
            continue;
        }
        if(s[i]== 'T' && s[i+1]=='R'){
            t[k++]='1';
            i=i+3;
            continue;
        }
        if(s[i]=='F' && s[i+1]=='A'){
            t[k++]='0';
            i=i+4;
            continue;
        }
        if(s[i]=='A'&& s[i+1]=='N'){
            t[k++]='&';
            i=i+2;
            continue;
        }
        if(s[i]=='O' && s[i+1]=='R'){
            t[k++]='|';
            i++;
            continue;
        }
        if(s[i]=='N' && s[i+1]=='O'){
            t[k++]='1';
            t[k++]='^';
            i=i+2;
            continue;
        }
        t[k++]=s[i];
    }
}