Cod sursa(job #3210432)

Utilizator Ionut2212Nedelcu Alexandru Ionut Ionut2212 Data 6 martie 2024 11:34:59
Problema Bool Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.42 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;
ifstream fin ("bool.in");
ofstream fout ("bool.out");
char s[1001], t[1001], x;
int i, n, z = -1, vf;
int f[200];
int expresieXor();
int expresieAnd();
int expresieOr();
int factor();
int expresieOr()
{
    int r = expresieAnd();
    while (t[i] == '|')
    {
        i++;
        r = (r | expresieAnd());
    }
    return r;
}
int expresieAnd()
{
    int r = expresieXor();
    while (t[i] == '&')
    {
        i++;
        r = (r & expresieXor());
    }
    return r;
}
int expresieXor()
{
    int r = factor();
    while (t[i] == '^')
    {
        i++;
        r = (r^factor());
    }
    return r;
}
int factor()
{
    int r;
    /*
    Factorii pot fi de trei tupuri:
    (expresie)
    constanta 0 sau 1
    o variabila a carei valoare o extragem din vectorul f
    */
    if (t[i] == '(')
    {
        i++;
        r = expresieOr();
        i++;
    }
    else
    {
        if (t[i] =='0')
        {
            i++;
            return 0;
        }
        if (t[i] == '1')
        {
            i++;
            return 1;
        }
        return f[ t[i++] ];
    }
    return r;
}


int main()
{
    fin.get(s,1001);
    n = strlen(s);
    for(int i = 0; i < n; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'N')
        {
            t[++z] = '&';
            i+=2;
        }
        else if(s[i] == '(' || s[i] == ')')
        {
            t[++z] = s[i];
        }
        else if(s[i] == 'O' && s[i+1] == 'R')
        {
            t[++z] = '|';
            i+=1;
        }
        else if(s[i] == 'N' && s[i+1] == 'O')
        {
            t[++z] = '1';
            t[++z] = '^';
            i+=2;
        }
        else if((s[i] == 'T' && s[i+1] != ' ') || (s[i] == 'F' && s[i+1]!= ' '))
        {

            if(s[i] == 'T' && s[i+1] == 'R')
            {
                t[++z] = '1';
                i+=3;
            }
            else if(s[i] == 'F' && s[i+1] == 'A')

            {
                t[++z] = '0';
                i+=4;
            }
        }
        else if(s[i] >= 'A' && s[i] <='Z')
        {
            t[++z] = s[i];
        }

    }
    t[z+1] = NULL;

    fin >> n;
    for(int j = 1; j<=n; j++)
    {
        i = 0;
        fin >> x;
        f[x] = 1 - f[x];
        fout << expresieOr();
    }
    return 0;
}