Cod sursa(job #2320279)

Utilizator DordeDorde Matei Dorde Data 14 ianuarie 2019 16:42:00
Problema Bool Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <fstream>
#include <cstring>

using namespace std;
int const NM = 1003;
char init [NM] , s [NM];
bool v [10 + 'Z'];
ifstream f ("bool.in");
ofstream g ("bool.out");
bool exp ();
bool term ();
bool fact ();
int p;
bool exp (){
    bool sum = term ();
    while (s [p] == 'o'){
        ++ p;
        sum = sum | term ();
    }
    return sum;
}
bool term (){
    bool prod = fact ();
    while (s [p] == 'a'){
        if (s [p] == 'a'){
            ++ p;
            prod = prod & fact ();
        }
    }
    return prod;
}
bool fact (){
    bool x = true , val = false;
    while (s [p] == 'n'){
        x = false;
        ++ p;
    }
    int pr = 0;
    if (s [p] == '('){
        while (s [p] == '(')
            ++ p , ++ pr;
        val = exp ();
        p += pr;
        return x ? val : ! val;
    }
    if (s [p] <= 'Z' + 2 && s [p] >= 'A')
        val = v [s [p]] , ++ p;
    return x ? val : ! val;
}
int main()
{
    v ['Z' + 1] = true;
    f . getline (init , NM);
    int l = strlen (init) , l2 = 0;
    while (p < l){
        if (init [p] == 'A' && init [p + 1] == 'N')
            s [l2] = 'a' , p += 3 , ++ l2;
        if (init [p] == 'N' && init [p + 1] == 'O')
            s [l2] = 'n' , p += 3 , ++ l2;
        if (init [p] == 'O' && init [p + 1] == 'R')
            s [l2] = 'o' , p += 2 , ++ l2;
        if (init [p] == 'T' && init [p + 1] == 'R')
            s [l2] = 'Z' + 1 , p += 4 , ++ l2;
        if(init [p] == 'F' && init [p + 1] == 'A')
            s [l2] = 'Z' + 2 , p += 5 , ++ l2;
        if (init [p] != ' ')
            s [l2] = init [p] , ++ l2;
        ++ p;
    }
    p = 0;
    int t;
    f >> t;
    f . get ();
    while (t --){
        char c = f . get ();
        v [c] = ! v [c];
        p = 0;
        g << exp ();
    }
    g << '\n' << s;
    return 0;
}