Cod sursa(job #791131)

Utilizator toranagahVlad Badelita toranagah Data 23 septembrie 2012 01:31:14
Problema Bool Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.32 kb
#include <fstream>
#include <string>
using namespace std;

const int MAX_N = 1001;

ifstream fin("bool.in");
ofstream fout("bool.out");
string s, v;

int N;

bool val[27];
char st[1001];
bool stb[1001];
int st_top = 0;
char qu[1001];
int qu_front = 1, qu_back = 0;

int precedence(char ch);

int main(int argc, char const *argv[])
{
    getline(fin, s, '\n');
    fin >> N;
    fin >> v;

    int l = s.size();
    char ch;
    for (int i = 0; i < l; ++i) {
        ch = s[i];
        if (ch >= 'A' && ch <= 'Z' && s[i+1] >= 'A' && s[i+1] <= 'Z') {
            switch (ch) {
                case 'N':
                    ch = '!';
                    i += 2;
                    break;
                case 'A':
                    ch = '&';
                    i += 2;
                    break;
                case 'O':
                    ch = '|';
                    i += 1;
                    break;
                case 'T':
                    ch = '1';
                    i += 3;
                    break;
                case 'F':
                    ch = '0';
                    i += 4;
                    break;
                default:
                    fout << "ERROR" << endl;
            }
            if (ch == '0' || ch == '1') {
                qu[++qu_back] = ch;
            } else {
                while (!st_top && precedence(ch) <= precedence(st[st_top])) {
                    qu[++qu_back] = st[st_top];
                    --st_top;
                }
                st[++st_top] = ch;
            }
        } else if (ch == '(') {
            st[++st_top] = ch;
        } else if (ch == ')') {
            while (st[st_top] != '(') {
                qu[++qu_back] = st[st_top];
                --st_top;
            }
            --st_top;
        } else if (ch >= 'A' && ch <= 'Z') {
            val[ch - 'A'] = false;
            qu[++qu_back] = ch;
        } 
    }
    while (st_top >0) {
        qu[++qu_back] = st[st_top];
        --st_top;
    }

    bool result;
    int c1, t1;
    int l1 = v.size();
    for (int i = 0; i < l1; ++i) {
        val[v[i] - 'A'] = !val[v[i] - 'A'];
        c1 = 1, t1 = 0;
        while (c1 <= qu_back) {
            if (qu[c1] >= 'A' && qu[c1] <= 'Z') {
                stb[++t1] = val[qu[c1] - 'A'];
                ++c1;
            } else {
                switch (qu[c1]) {
                    case '1':
                        stb[++t1] = true;
                        break;
                    case '0':
                        stb[++t1] = false;
                        break;
                    case '!':
                        stb[t1] = !stb[t1];
                        break;
                    case '&':
                        stb[t1 - 1] = stb[t1 - 1] && stb[t1];
                        --t1;
                        break;
                    case '|':
                        stb[t1 - 1] = stb[t1 - 1] || stb[t1];
                        --t1;
                        break;
                    default:
                        fout << "ERROR2" << endl;
                }
                ++c1;
            }
        }
        result = stb[t1];
        --t1;
        fout << result;
    }

    return 0;
}

int precedence(char ch) {
    if (ch == '!') {
        return 3;
    } else if (ch == '&') {
        return 2;
    } else if (ch == '|') {
        return 1;
    } else {
        return 0;
    }
}