Cod sursa(job #3239675)

Utilizator vlad7654vladimir manescu vlad7654 Data 7 august 2024 11:56:19
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");

bool expresie(const string& s,int& p);
bool termen(const string& s,int& p);
bool factor(const string& s,int& p);
bool variabila(const string& s,int& p);

unordered_map<char,bool>valoare_adevar;
bool expresie(const string& s,int& p){
    bool result=termen(s,p);
    while(p+1<s.size() and s[p]=='O' and s[p+1]=='R'){
        p+=2;
        result=result|termen(s,p);
    }
    return result;
}
bool termen(const string& s,int& p){
    bool result=factor(s,p);
    while(p+2<s.size() and s[p]=='A' and s[p+1]=='N' and s[p+2]=='D'){
        p+=3;
        result=result&factor(s,p);
    }
    return result;
}
bool factor(const string& s,int& p){
    if(p+2<s.size() and s[p]=='N' and s[p+1]=='O' and s[p+2]=='T'){
        p+=3;
        return 1^factor(s,p);
    }else if(s[p]=='('){
        p++;
        bool result=expresie(s,p);
        p++;
        return result;
    }else{
        return variabila(s,p);
    }
}
bool variabila(const string& s,int& p){
    if(p+1<(int)s.size() && s[p]=='T' && s[p+1]=='R'){
        p+=4;
        return 1;
    }else if(p+1<(int)s.size() && s[p]=='F' && s[p+1]=='A'){
        p+=5;
        return 0;
    }else{
        p++;
        return valoare_adevar[s[p-1]];
    }
}
int main(){
    int n,p;
    string v,s,c;
    getline(fin,v);
    fin>>n>>c;
    for(int i=0; i<v.size(); i++){
        if(v[i]!=' '){
            s.push_back(v[i]);
        }
    }
    for(int i=0; i<n; i++){
        p=0;
        valoare_adevar[c[i]]=1^valoare_adevar[c[i]];
        fout<<expresie(s,p);
    }
}