Cod sursa(job #2843295)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 2 februarie 2022 12:10:57
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.38 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <cctype>

using namespace std;

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

char s[1001];
char litere[101];
bool nr[26];
int ind;

bool expresie();
bool termen();
bool factor();

void easier()
{
    int i=0,j,kon=0;
    while(s[i]!='\0')
    {
        if(s[i]=='T' && s[i+1]=='R' && s[i+2]=='U' && s[i+3]=='E')  ///daca e adevarat
        {
            s[kon]='1';
            kon++;
            i=i+4;
        }
        else if(s[i]=='F' && s[i+1]=='A' && s[i+2]=='L' && s[i+3]=='S' && s[i+4]=='E')  ///daca e fals
        {
            s[kon]='0';
            kon++;
            i=i+5;
        }
        else if(s[i]=='N' && s[i+1]=='O' && s[i+2]=='T')    ///daca e nu
        {
            s[kon]='!';
            kon++;
            i=i+3;
        }
        else if(s[i]=='A' && s[i+1]=='N' && s[i+2]=='D')    ///daca e si
        {
            s[kon]='&';
            kon++;
            i=i+3;
        }
        else if(s[i]=='O' && s[i+1]=='R')       ///daca e sau
        {
            s[kon]='|';
            kon++;
            i=i+2;
        }
        else if (s[i]!=' ')
        {
           s[kon]=s[i];
           i++;
           kon++;
        }
        else
            i++;
    }
    s[kon]='\0';
}

bool factor()
{
    bool sign=1,x;
    while(s[ind]=='!')
    {
        ind++;
        sign=(!sign);
    }
    if(s[ind]=='(')
    {
        ind++;
        x=expresie();
        ind++;
        if(!sign)
            x=(!x);
        return x;
    }
    if(s[ind]=='0')
        x=0;
    else if (s[ind]=='1')
        x=1;
    else
        x=nr[s[ind]-'A'];
    ind++;
    if(!sign)
        x=(!x);
    return x;
}

bool termen()
{
    bool si,fact;
    si=factor();
    while(s[ind]=='&')
    {
        ind++;
        fact=factor();
        si=(fact && si);
    }
    return si;
}

bool expresie()
{
    bool ori,term;
    ori=termen();
    while(s[ind]=='|')
    {
        ind++;
        term=termen();
        ori=(term || ori);
    }
    return ori;
}

int main()
{
    int n,i,j;
    fin.getline(s,1001);
    fin>>n;
    fin>>litere;
    easier();
    for(i=0;i<n;i++)
    {
        nr[litere[i]-'A']=(!nr[litere[i]-'A']);
        ind=0;
        fout<<expresie();
    }
    return 0;
}