Cod sursa(job #2529621)

Utilizator marius004scarlat marius marius004 Data 23 ianuarie 2020 18:23:46
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <iostream>
#include <fstream>
#include <string>

std::ifstream f("bool.in");
std::ofstream g("bool.out");

int i,map[256],t;
char var;
std::string s;

void parseString(std::string& s){
    
    std::string sol;
    
    for(int i = 0;i < s.size();++i){
        
        if(s[i] == ' ')
            continue;
        
        if(s[i] == '(' || s[i] == ')'){
            sol += s[i];
            continue;
        }
        
        if(s[i] == 'T' && s[i + 1] == 'R'){
            sol += '1';
            i += 3;
            continue;
        }
        
        if(s[i] == 'F' && s[i + 1] == 'A'){
            sol += '0';
            i += 4;
            continue;
        }
        
        if(s[i] == 'A' && s[i + 1] == 'N'){
            sol += '&';
            i += 2;
            continue;
        }
        
        if(s[i] == 'N' && s[i + 1] == 'O'){
            sol += "1^";
            i += 2;
            continue;
        }
        
        if(s[i] == 'O' && s[i + 1] == 'R'){
            sol += '|';
            i++;
            continue;
        }
        
        sol += s[i];
    }
    
    s = sol;
}

bool expresieOr();
bool expresieAnd();
bool expresieXor();
bool factor();

bool expresieOr(){
    
    bool r = expresieAnd();
    
    while(s[i] == '|'){
        i++;
        r = (r | expresieAnd());
    }
    
    return r;
}

bool expresieAnd(){
    
    bool r = expresieXor();
    
    while(s[i] == '&'){
        i++;
        r = (r & expresieXor());
    }
    
    return r;
}

bool expresieXor(){
    
    bool r = factor();
    
    while(s[i] == '^'){
        i++;
        r = (r ^ factor());
    }
    
    return r;
}

bool factor(){
    
    bool r;
    
    if(s[i] == '('){
        i++;
        r = expresieOr();
        i++;
    }else{
    
        if(s[i] == '1'){
            i++;
            return 1;
        }
        
        if(s[i] == '0'){
            i++;
            return 0;
        }
        
        return map[s[i++]];
    }
    
    return r;
}

int main(){
    
    std::getline(f,s);
    
    parseString(s);
    
    f >> t;
    
    while(t--){
        
        f >> var;
        
        map[var] = 1 - map[var];
        i = 0;
        
        g << expresieOr();
    }
    
    return 0;
}