Cod sursa(job #2359653)

Utilizator trifangrobertRobert Trifan trifangrobert Data 28 februarie 2019 23:12:44
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.21 kb
#include <fstream>
#include <string>
#include <cstring>

#define BRACKET -1
#define AND     -2
#define OR      -3
#define NOT     -4

using namespace std;

const int NMAX = 1005;
const int SIGMA = 26;
int n, m;
char aux[NMAX], s[NMAX], lit[NMAX];
bool val[SIGMA + 5];
int st[NMAX], top;
int p;
bool expresie(), termen(), factor();

inline bool isLetter(char ch)
{
    return 'A' <= ch && ch <= 'Z';
}

inline bool CheckTrue(int i)
{
    return (s[i] == 'T' && s[i + 1] == 'R' && s[i + 2] == 'U' && s[i + 3] == 'E');
}

inline bool CheckFalse(int i)
{
    return (s[i] == 'F' && s[i + 1] == 'A' && s[i + 2] == 'L' && s[i + 3] == 'S' && s[i + 4] == 'E');
}

inline bool CheckNot(int i)
{
    return (s[i] == 'N' && s[i + 1] == 'O' && s[i + 2] == 'T');
}

inline bool CheckAnd(int i)
{
    return (s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D');
}

inline bool CheckOr(int i)
{
    return (s[i] == 'O' && s[i + 1] == 'R');
}

bool expresie()
{
    bool x = termen();
    while (p <= n && CheckOr(p))
    {
        p += 2;
        x |= termen();
    }
    return x;
}

bool termen()
{
    int x = factor();
    while (p <= n && CheckAnd(p))
    {
        p += 3;
        x &= factor();
    }
    return x;
}

bool factor()
{
    bool x = 0;
    if (s[p] == '(')
    {
        ++p;
        x = expresie();
        ++p;
    }
    else if (CheckNot(p))
    {
        p += 3;
        x ^= factor();
    }
    else if (CheckTrue(p))
    {
        p += 4;
        x = 1;
    }
    else if (CheckFalse(p))
    {
        p += 5;
        x = 0;
    }
    else
    {
        x = val[s[p] - 'A'];
        ++p;
    }
    return x;
}

int main()
{
    ifstream fin("bool.in");
    ofstream fout("bool.out");
    fin.getline(aux + 1, NMAX);
    n = 0;
    for (int i = 1;aux[i];++i)
        if (aux[i] != ' ')
            s[++n] = aux[i];
    fin >> m;
    fin.get();
    fin.getline(lit + 1, NMAX);
    string ans;
    for (int i = 1;i <= m;++i)
    {
        val[lit[i] - 'A'] = !val[lit[i] - 'A'];
        p = 1;
        ans.push_back((char)(expresie() + '0'));
    }
    fout << ans << "\n";
    fin.close();
    fout.close();
    return 0;
}