Cod sursa(job #1515358)

Utilizator dyanagGrigore Diana dyanag Data 1 noiembrie 2015 15:36:28
Problema Bool Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.47 kb
#include <cstdio>
#include<cstring>
using namespace std;

FILE *f=fopen("bool.in", "r");
FILE *g=fopen("bool.out", "w");

char s[1001], sch[1001];
int v[31], poz;

void schimb(){
    int n=strlen(s);
    char *p;
    p=strchr(s, ' ');
    while(p!=NULL){
        strcpy(p, p+1);
        p=strchr(p+1, ' ');
    }
    p=strstr(s, "TRUE");
    while(p!=NULL){
        s[p-s+3]='1';
        strcpy(p, p+3);
        p=strstr(p, "TRUE");
    }
    p=strstr(s, "FALSE");
    while(p!=NULL){
        s[p-s+4]='0';
        strcpy(p, p+4);
        p=strstr(p, "FALSE");
    }
    p=strstr(s, "AND");
    while(p!=NULL){
        s[p-s+2]='+';
        strcpy(p, p+2);
        p=strstr(p, "AND");
    }
    p=strstr(s, "OR");
    while(p!=NULL){
        s[p-s+1]='/';
        strcpy(p, p+1);
        p=strstr(p, "OR");
    }
    p=strstr(s, "NOT");
    while(p!=NULL){
        s[p-s+2]='!';
        strcpy(p, p+2);
        p=strstr(p, "NOT");
    }
    p=strchr(s, '(');
    while(p!=NULL){
        if(s[p-s+2]==')'){
            strcpy(p, p+1);
            strcpy(p+1, p+2);
        }
        p=strchr(p+1, '(');
    }
    p=strstr(s, "!!");
    while(p!=NULL){
        strcpy(p, p+2);
        p=strstr(p, "!!");
    }
}
int f4();
int f3();
int f2();
int f1(){
    int r=f2(), p;
    while(s[poz]=='/'){ //OR
        ++poz;
        p=f2();
        if(r+p!=0) r=1;
        else r=0;
    }
    return r;
}
int f2(){
    int r=f3(), p;
    while(s[poz]=='+'){ //AND
        ++poz;
        p=f3();
        if(r+p==2) r=1;
        else r=0;
    }
    return r;
}
int f3(){
    int r=0;
    if(s[poz]=='!'){ //NOT
        ++poz;
        r=(f4()+1)%2;
        return r;
    }
    return f4();
}
int f4(){
    int val=0;
    if(s[poz]=='('){
        ++poz;
        val=f1();
        ++poz;
        return val;
    }
    if(s[poz]=='0'||s[poz]=='1'){
        ++poz;
        return (s[poz-1]-'0');
    }
    if('A'<=s[poz]&&s[poz]<='Z'){
        ++poz;
        return (v[s[poz-1]-'A'+1]);
    }
}


int main()
{
    char c;
    int n, x=0;
    fgets(s, 1001, f);
    fscanf(f, "%d%*c", &n);
    schimb();
    strcpy(sch, s);
    //fprintf(g, "%s\n", sch);
    x=f1();
    poz=0;
    for(int i=1; i<=n; ++i){
        fscanf(f, "%c", &c);
        v[c-'A'+1]=(v[c-'A'+1]+1)%2;
        if(strchr(s, c)!=NULL)
            x=f1();
        fprintf(g, "%d", x);
        strcpy(s, sch);
        poz=0;
    }
return 0;
}