Cod sursa(job #2431037)

Utilizator rd211Dinucu David rd211 Data 17 iunie 2019 18:31:51
Problema Bool Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.33 kb
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
bool isDigit(char a)
{
    if(a>='0' && a<='9')
        return true;
    return false;
}
bool isLetter(char a)
{
    if(a>='A' && a<='Z')
        return true;
    return false;
}
string replaceOperators(string a)
{
    int found = a.find("AND");
    while(found!=string::npos)
    {
        a.replace(a.begin()+found,a.begin()+found+3,"&");
        found = a.find("AND");
    }
    found = a.find("OR");
    while(found!=string::npos)
    {
        a.replace(a.begin()+found,a.begin()+found+2,"|");
        found = a.find("OR");
    }
    found = a.find("NOT");
    while(found!=string::npos)
    {
        a.replace(a.begin()+found,a.begin()+found+3,"!");
        found = a.find("NOT");
    }
    found = a.find("TRUE");
    while(found!=string::npos)
    {
        a.replace(a.begin()+found,a.begin()+found+4,"1");
        found = a.find("TRUE");
    }
    found = a.find("FALSE");
    while(found!=string::npos)
    {
        a.replace(a.begin()+found,a.begin()+found+5,"0");
        found = a.find("FALSE");
    }
    return a;
}
bool higherImportance(char a,char b)
{
    if(a=='(' || b=='(' || a==')' || b==')')
        return false;
    if(a=='!')
        return true;
    if(b=='!')
        return false;
    if(a=='&')
        return true;
    if(b=='&')
        return false;
    return true;
}
stack<char> stac;
short table[50][900];
string infixToPostFix(string a)
{
    string res;
    a=replaceOperators(a);
    for(int i = 0;i<a.size();i++)
    {
        if(isLetter(a[i]) || isDigit(a[i]))
            res+=a[i];
        else
        {
            if(a[i]=='(')
                stac.push(a[i]);
            else if(a[i]==')')
            {
                while(stac.size() && stac.top()!='(')
                        res+=stac.top(),stac.pop();
                if(stac.size())
                    stac.pop();
            }
            else if(a[i]==' ')
            {

            }
            else
            {
                while(stac.size() && higherImportance(stac.top(),a[i]))
                    res+=stac.top(),stac.pop();
                stac.push(a[i]);
            }
        }
    }
    while(stac.size())
        res+=stac.top(),stac.pop();
    return res;
}
void buildIndexTable(string a)
{
    for(int i = 0;i<a.size();i++)
    {
        if(isLetter(a[i]))
        {
            table[a[i]-'A'][0]++;
            table[a[i]-'A'][ table[a[i]-'A'][0] ]=i;
        }
    }
}
string replaceVariables(string a)
{
    for(int i = 0;i<a.size();i++)
    {
        if(isLetter(a[i]))
            a[i]='0';
    }
    return a;
}
stack<bool> stacBool;
bool evaluate(string a)
{
    bool num1,num2,res;
    for(int i = 0;i<a.size();i++)
    {
        if(isDigit(a[i]))
            stacBool.push((a[i]=='0')?false:true);
        else if(a[i]=='!')
        {
            num1 = stacBool.top();
            num1= !num1;
            stacBool.pop();
            stacBool.push(num1);
        }
        else
        {
            num1 = stacBool.top();
            stacBool.pop();
            num2 = stacBool.top();
            stacBool.pop();
            if(a[i]=='&')
            {
                if(num1 && num2)
                    res = true;
                else
                    res = false;
            }
            else if(a[i]=='|')
            {
                if(num1||num2)
                    res = true;
                else
                    res = false;
            }
            stacBool.push(res);
        }
    }
    return stacBool.top();
}
void solve(string a,int changes)
{
    char c;
    a = infixToPostFix(a);
    buildIndexTable(a);
    a = replaceVariables(a);
    while(changes--)
    {
        fin>>c;
        int times = table[c-'A'][0];
        for(int i = 1;i<=times;i++)
        {
            if(a[table[c-'A'][i]]=='1')
                a[table[c-'A'][i]]='0';
            else
                a[table[c-'A'][i]]='1';
        }
        fout<<evaluate(a);
    }
}
int main()
{
    string a,b;
    fin>>b;
    int changes;
    while(!isDigit(b[0]))
        a+=b+" ",fin>>b;
    changes = stoi(b);
    solve(a,changes);
    return 0;
}