Cod sursa(job #2528718)

Utilizator marius004scarlat marius marius004 Data 22 ianuarie 2020 14:09:25
Problema Bool Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.4 kb
#include <iostream>
#include <fstream>
#include <string>

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

int i,t;
bool fr[256];
char variable;
std::string s;

bool evaluare();
bool termen();
bool factor();

bool evaluare(){
    
    while(s[i] == ' ')
        i++;
    
    bool r = termen();
    
    while(s[i] == ' ')
        i++;
    
    while((s[i] == 'N' && s[i + 1] == 'O' && s[i + 2] == 'T')
        || (s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D')
        || (s[i] == 'O' && s[i + 1] == 'R')){
        
        if(s[i] == 'N'){// NOT
            i += 3;
            r = !termen();
        }
        else if(s[i] == 'A'){// AND
            i += 3;
            r &= termen();
        }
        else{// OR
            i += 2;
            r = (r || termen());
        }
    }
    
    while(s[i] == ' ')
        i++;
    
    return r;
}

bool termen(){
    
    while(s[i] == ' ')
        i++;
    
    bool r = factor();
    
    while(s[i] == ' ')
        i++;
    
    bool inNOT{ false };
    while(s[i] == 'N' && s[i + 1] == 'O' && s[i + 2] == 'T'){
        i += 3;
        inNOT = true;
        r = !factor();
    }
    
    if(inNOT)
        return r;
    
    bool inAND{ false };
    while(s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D'){
        i += 3;
        inAND = true;
        r &= factor();
    }
    
    if(inAND)
        return r;
    
    while(s[i] == 'O' && s[i + 1] == 'R') {
        i += 2;
        r = (r || factor());// r^= factor
    }
    
    while(s[i] == ' ')
        i++;
    
    return r;
}

// functia e implementata.Nu modifica!!
bool factor(){
    
    bool r;
    
    while(s[i] == ' ')
        i++;
    
    if(s[i] == '('){
        i++;
        r = evaluare();
        i++;
        while(s[i] == ' ')
            i++;
    }else{
        
        std::string var;
        
        while(s[i] >= 'A' && s[i] <= 'Z'){
            var += s[i];
            i++;
        }
        
        if(var == "TRUE")
            r = 1;
        else if(var == "FALSE")
            r = 0;
        else
            r = fr[var[0]];
        
        while(s[i] == ' ')
            i++;
    }
    
    while(s[i] == ' ')
        i++;
    
    return r;
}

int main(){
    
    std::getline(f,s);
    s += "       ";
    
    f  >> t;
    
    while(t--){
        
        f >> variable;
        
        fr[variable] = !fr[variable];
        
        i = 0;
        g << evaluare();
    }
    
    return 0;
}