Cod sursa(job #1515329)

Utilizator preda.andreiPreda Andrei preda.andrei Data 1 noiembrie 2015 15:00:32
Problema Bool Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

char s[2001];
bool b['Z'+1];
int ind;
int len;

bool eval3();

bool eval(){
    bool mod=false;

    if(strncmp(s+ind, "NOT", strlen("NOT"))==0){
        mod=true;
        ind+=strlen("NOT");
    }

    if(s[ind]=='('){
        ind++;
        bool r=eval3();
        ind++;
        if(mod)
            r=!r;
        return r;
    }

    bool r=b[s[ind]];
    ind++;
    if(mod)
        r=!r;
    return r;
}

bool eval2(){
    bool r=eval();

    while(strncmp(s+ind, "AND", strlen("AND"))==0){
        ind=ind+strlen("AND");
        bool r2=eval();

        r=r&&r2;
    }

    return r;
}

bool eval3(){
    bool r=eval2();

    while(strncmp(s+ind, "OR", strlen("OR"))==0){
        ind=ind+strlen("OR");
        bool r2=eval3();

        r=r||r2;
    }

    return r;
}

int main()
{
    FILE *fin=fopen("bool.in", "r");
    FILE *fout=fopen("bool.out", "w");

    char cs[2001];
    char *ptr;
    int t;
    char ch;

    fgets(s, 1500, fin);
    if(s[strlen(s)-1]=='\n')
        s[strlen(s)-1]='\0';
    len=strlen(s);

    ptr=strchr(s, ' ');
    while(ptr){
        strcpy(cs, ptr+1);
        strcpy(ptr, cs);
        ptr=strchr(ptr, ' ');
    }

    fscanf(fin, "%d\n", &t);
    for(int i=1; i<=t; ++i){
        fscanf(fin, "%c", &ch);
        //printf("Schimb %c\n", ch);
        b[ch] = !b[ch];
        //cout << "asd";
        ind=0;
        if(eval3())
            fprintf(fout, "1");
        else fprintf(fout, "0");
    }

    return 0;
}