Cod sursa(job #2632764)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 4 iulie 2020 19:35:51
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.19 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <string.h>
#include <stdlib.h>
using namespace std;
ifstream in ("bool.in");
ofstream out("bool.out");
char buff[1010];
bool val[129];
int poz, n, q;
vector <char> a;
stack <char> stk;
inline void pars(string & s)
{
    while(buff[poz]==' ' && poz<=n-1)
        poz++;
    while(buff[poz]>='A' && buff[poz]<='Z' && poz<=n-1)
        s+=buff[poz++];
    if(s.empty() && poz<=n-1) s=buff[poz++];
}
inline char getNext()
{
    string s; pars(s);
    if(s.size()==1)
    {
        if(s[0]=='(')
            return '(';
        else if(s[0]==')')
            return ')';
        else
            return s[0];
    }
    if(s=="NOT") return '!';
    else if(s=="AND") return '&';
    else if(s=="OR") return '|';
    else if(s=="TRUE") return '1';
    else if(s=="FALSE") return '0';
    return 0;
}
void creareFormaPrefixata();
bool calculare()
{
    for(auto & x : a)
    {
        if(x>='A'&&x<='Z')
            stk.push(val[x]);
        if(x=='0'||x=='1')
            stk.push(x-'0');
        if(x=='!')
        {
            auto temp=stk.top();
            stk.pop();
            temp^=1;
            stk.push(temp);
        }
        if(x=='&')
        {
            auto temp1=stk.top(); stk.pop();
            auto temp2=stk.top(); stk.pop();
            stk.push(temp1&temp2);
        }
        if(x=='|')
        {
            auto temp1=stk.top(); stk.pop();
            auto temp2=stk.top(); stk.pop();
            stk.push(temp1|temp2);
        }
    }
    auto temp=stk.top();
    stk.pop();
    return temp;
}
int main()
{
    in.getline(buff, 1000); n=strlen(buff);
    creareFormaPrefixata();
    in.getline(buff, 1000); q=atoi(buff);

    in.getline(buff, 1000);
    for(int i=0; buff[i]!='\0'; i++)
    {
        val[buff[i]]^=1;
        out<<(int)calculare();
    }
    return 0;
}


void creareFormaPrefixata()
{
    char val=getNext();
    while(val)
    {
        if((val>='A' && val<='Z')||val=='1'||val=='0') ///e numar
        {
            a.push_back(val);
            val=getNext();
            continue;
        }
        if(stk.empty())
            stk.push(val);
        else
        {
            if(val=='(')
                stk.push('(');
            else if(val==')')
            {
                while(stk.top()!='(')
                    a.push_back(stk.top()), stk.pop();
                stk.pop();
            }
            else if(val=='!')
            {
                while(stk.top()=='!')
                    a.push_back(stk.top()), stk.pop();
                stk.push(val);
            }
            else if(val=='&')
            {
                while(stk.top()=='!'||stk.top()=='&')
                    a.push_back(stk.top()), stk.pop();
                stk.push(val);
            }
            else if(val=='|')
            {
                while(stk.top()=='!'||stk.top()=='&'||stk.top()=='|')
                    a.push_back(stk.top()), stk.pop();
                stk.push(val);
            }
        }
        val=getNext();
    }
    while(!stk.empty())
        a.push_back(stk.top()), stk.pop();
}