Cod sursa(job #2524449)

Utilizator marinaoprOprea Marina marinaopr Data 15 ianuarie 2020 18:32:50
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <stdio.h>
#include <string.h>
using namespace std;
FILE *fin = fopen("bool.in", "r");
FILE *fout = fopen("bool.out", "w");
int n,lg,i,poz;
char sir[1005],c,*p;
bool var[30];
bool expresie();
bool prioritate1();
bool prioritate2();
bool prioritate3();

bool prioritate3()

{
    bool ans;
    if(*p == '(')
    {
        ++p;
        poz++;
        ans = expresie();
        ++p;
        poz++;
    }
    else
        if((poz+1)<lg and *p == 'T' and *(p+1) == 'R')
        {
            p += 4;
            ans = 1;
            poz+=4;
        }
        else
            if((poz+1)<lg and *p == 'F' and *(p+1) == 'A')
            {
                p += 5;
                ans = 0;
                poz+=5;
            }
            else
            {
                ans = var[*p-'A'];
                ++p;
                poz++;
            }
    return ans;
}

bool prioritate2()
{
    bool ans;
    int nrn=0;

    while((poz+1)<lg and *p == 'N' and *(p+1) == 'O')
    {
        p += 3;
        poz+=3;
        nrn++;


    }
    if(nrn%2==1)  ans = !prioritate3();
    else
        ans = prioritate3();
    return ans;
}

bool prioritate1()
{
    bool ans = prioritate2();
    while((poz+1)<lg and *p == 'A' and *(p+1) == 'N')
    {
        p += 3;
        poz+=3;
        ans &= prioritate2();
    }
    return ans;
}

bool expresie()

{
    bool ans = prioritate1();
    while((poz+1)<lg and *p == 'O' and *(p+1) == 'R')
    {
        p += 2;
        poz+=2;
        ans |= prioritate1();
    }
    return ans;
}

int main()
{
    fgets(sir, 1002, fin);
    lg = strlen(sir)-1;
    for(i=0; i<strlen(sir)-1; ++i)
        if(sir[i] == ' ')
            strcpy(sir+i, sir+i+1);
    lg = strlen(sir);

    fscanf(fin, "%d\n", &n);
    while(n--)
    {
        c = fgetc(fin);
        var[c-'A'] = !var[c-'A'];
        p = sir;
        poz=0;
        fprintf(fout, "%d", expresie());
    }
    fclose(fin);
    fclose(fout);
    return 0;
}