Cod sursa(job #626797)

Utilizator toniobFMI - Barbalau Antonio toniob Data 28 octombrie 2011 12:12:18
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <fstream>
using namespace std;

ifstream in ("bool.in");
ofstream out ("bool.out");

const int N = 1003;
char sc[N], *p, c, s[N], sc1[N], *p1;
int n;

void parsare () {
    p1 = sc1;
    p = sc;
    while (*p1) {
        if (*p1 == ' ') {
            ++p1;
            continue;
        }
        if (*p1 == 'A' && *(p1+1) == 'N') {
            *p = '&';
            ++p;
            p1 += 3;
            continue;
        }
        if (*p1 == 'O' && *(p1+1) == 'R') {
            *p = '|';
            ++p;
            p1+=2;
            continue;
        }
        if (*p1 == 'N' && *(p1+1) == 'O') {
            *p = '!';
            ++p;
            p1+=3;
            continue;
        }
        if (*p1 == '(' || *p1 == ')') {
            *p = *p1;
            ++p;
            ++p1;
            continue;
        }
        if (*p1 == 'T' && *(p1+1)=='R') {
            *p = '1';
            ++p;
            p1 += 4;
            continue;
        }
        if (*p1 == 'F' && *(p1 + 1) == 'A') {
            *p = '0';
            ++p;
            p1 += 5;
            continue;
        }
        *p = *p1;
        ++p1, ++p;
    }
}

void init () {
    p1 = sc;
    p = s;
    while (*p1) {
        if (*p1 == '(' || *p1 == ')' || *p1 == '!' || *p1 == '&' || *p1 == '|' || *p1 =='0' || *p1 == '1') {
            *p = *p1;
            ++p,++p1;
            continue;
        }
        *p = '0';
        ++p;
        ++p1;
    }
}

void trans (char c) {
    p1=sc;
    p=s;
    while (*p1) {
        if (*p1 == c) {
            *p = (*p=='1') ? '0' : '1';
        }
        ++p1;
        ++p;
    }
}

bool andd(), orr(), termen();

bool andd() {
    bool rez = orr();
    while (*p == '|') {
        ++p;
        rez = rez | orr();
    }
    return rez;
}
bool orr (){
    bool rez = termen();
    while (*p == '&') {
        ++p;
        rez = rez & termen();
    }
    return rez;
}
bool termen () {
    bool val;
    bool semn = 1;
    while (*p == '!') {
        ++p;
        semn = !semn;
    }
    if (*p == '(') {
        ++p;
        val = andd();
        ++p;
        if (semn) return val;
        else return !val;
    }
    if (*p == '0') val = false;
    else val = true;
    ++p;
    if (semn) return val;
    return  !val;
}

void citire () {
	in.getline (sc1,N);
	parsare();
	init ();
	in >> n >> ws;
	for (;n--;) {
		in >> c;
		trans (c);
        p = s;
		p1 = sc;
		out <<andd ();
	}
}

int main (){
	citire();

	return 0;
}