Cod sursa(job #160796)

Utilizator FlorinC1996Florin C FlorinC1996 Data 16 martie 2008 21:12:37
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.95 kb
 #include <cstdio>   
#include <cstring>   
  
const int MAX_L = 1024;   
const int ALPHA = 27;   
int N, K;   
char exp[MAX_L];   
bool map[ALPHA];   
  
bool E(void);   
  
bool F(void) {   
    bool res;   
    switch (exp[K]) {   
        case 'T':   
            if (exp[K + 1] == 'R') {   
                K += 4;   
                if (exp[K] == ' ') {   
                    ++ K;   
                }   
                return true;   
            }   
  
            if (exp[K + 1] == ' ') {   
                K += 2;   
                return map[exp[K - 2] - 'A'];   
            }   
            ++ K;   
            return map[exp[K - 1] - 'A'];   
        case 'F':   
            if (exp[K + 1] == 'A') {   
                K += 6;   
                return false;   
            }   
  
            if (exp[K + 1] == ' ') {   
                K += 2;   
                return map[exp[K - 2] - 'A'];   
            }   
            ++ K;   
            return map[exp[K - 1] - 'A'];      
        case 'N':   
            if (exp[K + 1] == 'O') {   
                K += 4;   
                res = F();   
                if (res == true)   
                    return false;   
                return true;   
            }   
  
            if (exp[K + 1] == ' ') {   
                K += 2;   
                return map[exp[K - 2] - 'A'];   
            }   
            ++ K;   
            return map[exp[K - 1] - 'A'];   
        case '(':   
            ++ K;   
            res = E();   
            if (exp[K + 1] == ' ') {   
                K += 2;   
                return res;   
            }   
            ++ K;   
            return res;   
        default:   
            if (exp[K + 1] == ' ') {   
                K += 2;   
                return map[exp[K - 2] - 'A'];   
            }   
            ++ K;   
            return map[exp[K - 1] - 'A'];   
    }   
}   
  
bool T(void) {   
    bool ret, res;   
    ret = res = F();   
    printf("%d\n", res);   
    while (exp[K] == 'A' && exp[K + 1] == 'N') {   
        K += 4;   
        res = F();   
        if (ret == true && res == false)   
            ret = false;   
    }   
    return ret;   
}   
  
bool E(void) {   
    // T OR T OR T ...   
    bool ret, res;   
    ret = res = T();   
    //printf("%d\n", res);   
    while (exp[K] == 'O' && exp[K + 1] == 'R') {   
        //printf("%d\n", res);   
        K += 3;   
        res = T();   
        //printf("%d\n", res);   
        if (ret == false && res == true)   
            ret = true;   
    }   
    return ret;   
}   
  
int main(void) {   
    FILE *in = fopen("bool.in", "r"),   
         *out = fopen("bool.out", "w");   
    fgets(exp, MAX_L, in);   
    N = strlen(exp) - 1;   
    if (exp[N] == '\n') {   
        exp[N] = 0;   
        -- N;   
    }   
    int S, i, j;   
    //puts(exp);   
    char ch;   
    /*map[0] = true;  
    fprintf(out, "%d", E());*/  
  
    fscanf(in, "%d\n", &S);   
    for (i = 0; i < S; ++ i) {   
        fscanf(in, "%c", &ch);   
        if (map[ch - 'A'] == true)   
            map[ch - 'A'] = false;   
        else  
            map[ch - 'A'] = true;   
        /*for (i = 0; i < 26; ++ i)  
            printf*/  
        K = 0;   
        fprintf(out, "%d", E());   
    }   
    fclose(in);   
    fprintf(out, "\n");   
    fclose(out);   
  
    /*K = 0;  
    strcpy(exp, "NOT (FALSE) AND NOT FALSE");  
    bool res = E();  
    printf("%d\n", res);*/  
  
    return 0;   
}  
#include <cstdio>
#include <cstring>

const int MAX_L = 1024;
const int ALPHA = 27;
int N, K;
char exp[MAX_L];
bool map[ALPHA];

bool E(void);

bool F(void) {
	bool res;
	switch (exp[K]) {
		case 'T':
			if (exp[K + 1] == 'R') {
				K += 4;
				if (exp[K] == ' ') {
					++ K;
				}
				return true;
			}

			if (exp[K + 1] == ' ') {
				K += 2;
				return map[exp[K - 2] - 'A'];
			}
			++ K;
			return map[exp[K - 1] - 'A'];
		case 'F':
			if (exp[K + 1] == 'A') {
				K += 6;
				return false;
			}

			if (exp[K + 1] == ' ') {
				K += 2;
				return map[exp[K - 2] - 'A'];
			}
			++ K;
			return map[exp[K - 1] - 'A'];	
		case 'N':
			if (exp[K + 1] == 'O') {
				K += 4;
				res = F();
				if (res == true)
					return false;
				return true;
			}

			if (exp[K + 1] == ' ') {
				K += 2;
				return map[exp[K - 2] - 'A'];
			}
			++ K;
			return map[exp[K - 1] - 'A'];
		case '(':
			++ K;
			res = E();
			if (exp[K + 1] == ' ') {
				K += 2;
				return res;
			}
			++ K;
			return res;
		default:
			if (exp[K + 1] == ' ') {
				K += 2;
				return map[exp[K - 2] - 'A'];
			}
			++ K;
			return map[exp[K - 1] - 'A'];
	}
}

bool T(void) {
	bool ret, res;
	ret = res = F();
	printf("%d\n", res);
	while (exp[K] == 'A' && exp[K + 1] == 'N') {
		K += 4;
		res = F();
		if (ret == true && res == false)
			ret = false;
	}
	return ret;
}

bool E(void) {
	// T OR T OR T ...
	bool ret, res;
	ret = res = T();
	//printf("%d\n", res);
	while (exp[K] == 'O' && exp[K + 1] == 'R') {
		//printf("%d\n", res);
		K += 3;
		res = T();
		//printf("%d\n", res);
		if (ret == false && res == true)
			ret = true;
	}
	return ret;
}

int main(void) {
	FILE *in = fopen("bool.in", "r"),
	     *out = fopen("bool.out", "w");
	fgets(exp, MAX_L, in);
	N = strlen(exp) - 1;
	if (exp[N] == '\n') {
		exp[N] = 0;
		-- N;
	}
	int S, i ;
	//puts(exp);
	char ch;
	/*map[0] = true;
	fprintf(out, "%d", E());*/

	fscanf(in, "%d\n", &S);
	for (i = 0; i < S; ++ i) {
		fscanf(in, "%c", &ch);
		if (map[ch - 'A'] == true)
			map[ch - 'A'] = false;
		else
			map[ch - 'A'] = true;
		/*for (i = 0; i < 26; ++ i)
			printf*/
		K = 0;
		fprintf(out, "%d", E());
	}
	fclose(in);
	fprintf(out, "\n");
	fclose(out);

	/*K = 0;
	strcpy(exp, "NOT (FALSE) AND NOT FALSE");
	bool res = E();
	printf("%d\n", res);*/

	return 0;
}