Cod sursa(job #2369176)

Utilizator ViAlexVisan Alexandru ViAlex Data 5 martie 2019 21:34:58
Problema Bool Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.34 kb
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
string expresion,to_change;
int len=0;
bool states[30];
int changes;
void read()
{
    getline(in,expresion);
    in>>changes;
    in>>to_change;
    len=expresion.length();

}

bool is_letter(char a)
{
    return a>='A'&&a<='Z';

}
string get_next_expresion(int&to_start)
{
    string formed="";
    for(; to_start<len; to_start++)
    {
        if(is_letter(expresion[to_start]))
            formed+=expresion[to_start];
        else
            break;
    }
    return formed;
}
bool check_not(int to_start)
{
    if(expresion[to_start]=='N'&&expresion[to_start+1]=='O'&&expresion[to_start+2]=='T')
        return true;
    return false;

}
void apply_operator(bool&last,bool newval,string&oper)
{
    if(oper=="AND")
        last=last&&newval;
    else
        last=last||newval;
}
bool is_operator(string&a)
{
    if(a=="OR" || a=="AND")
        return true;
    return false;
}
bool get_value(string exp)
{
    if(exp=="TRUE")
        return true;
    if(exp=="FALSE")
        return false;
    if(exp.length()==1)
    {
        char a=exp[0];
        return states[a-'A'];

    }
}
bool solve_paranthesis(int&to_start)
{
    bool last_value=false;
    string last_operator="OR";
    bool nott=false;
    for(; to_start<len&&expresion[to_start]!=')'; to_start++)
    {
        if(is_letter(expresion[to_start]))
        {
            string exp=get_next_expresion(to_start);
            if(is_operator(exp))
            {
                nott=false;
                last_operator=exp;
            }
            else if(exp=="NOT")
                nott=true;
            else
            {
                bool val=get_value(exp);
                if(nott)
                    val=!val;

                apply_operator(last_value,val,last_operator);

            }

        }
        else if(expresion[to_start]=='(')
        {
            to_start++;
            bool val=solve_paranthesis(to_start);
            if(nott)
                val=!val;

            apply_operator(last_value,val,last_operator);

        }


    }
    return last_value;




}
int main()
{
    read();
    int c=0;
    for(int i=0; i<changes; i++)
    {
        char ch=to_change[i];
        states[ch-'A']=!states[ch-'A'];
        int c=0;
        out<<solve_paranthesis(c);

    }
}