Cod sursa(job #2217031)

Utilizator Johnny07Savu Ioan-Daniel Johnny07 Data 28 iunie 2018 17:33:53
Problema Bool Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.4 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <stack>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
stack <int> s;
char ch[1100];
int i,z,nr,a[1100],n;
int b[1100], vec[110];

void checktrue()
{
    if (ch[i]=='T') if (ch[i+1]=='R') {
        nr++; a[nr]=1;
        i+=3;
        z=1;
    }
}

void checkfalse()
{
    if (ch[i]=='F') if (ch[i+1]=='A') {
        nr++; a[nr]=0;
        i+=4;
        z=1;
    }
}

void checknot()
{
    if (ch[i]=='N' && ch[i+1]=='O')
    {
        nr++; a[nr]=-3;
        i+=2;
        z=1;
    }
}

void checkor()
{
    if (ch[i]=='O' && ch[i+1]=='R')
    {
        nr++;
        a[nr]=-5;
        i++;
        z=1;
    }
}

void checkand()
{
    if (ch[i]=='A'&& ch[i+1]=='N')
    {
        nr++;
        i+=2;
        a[nr]=-4;
        z=1;
    }
}

void transforme ()
{
    n=strlen(ch)-1;
    i=0;
    while (i<=n)
    {
        z=0;
      if (z==0)  checktrue();
      if (z==0)  checkfalse();
     if (z==0)   checknot();
      if (z==0)  checkand();
       if (z==0) checkor();
        if (z==0)
        {
            if (ch[i]=='(') {nr++;a[nr]=-1;}
            if (ch[i]==')') {nr++;a[nr]=-2;}
            int zz=ch[i];
            if (zz>=65 && zz<=90) {nr++;a[nr]=zz;}
        }
       // cout<<"z="<<z<<" i="<<i<<" nr="<<nr<<" a[nr]="<<a[nr]<<"\n";
        i++;
    }
}

void postfix()
{
    n=nr;
    nr=0;
for (i=1;i<=n;i++)
{
    if (a[i]>=0)
    {
    nr++;
    b[nr]=a[i];
    }
    else
    {
    if (a[i]==-1) s.push(-1);
    if (a[i]==-2)
    {
        while (s.top()!=-1)
        {
            nr++;
            b[nr]=s.top();
            s.pop();
        }
    s.pop();
    }
    if (a[i]==-3)
    {
        while (!s.empty() && s.top()==-3)
        {
            nr++;
            b[nr]=s.top();
            s.pop();
        }
        s.push(a[i]);
    }
    if (a[i]==-4)
    {
        while (!s.empty() && s.top()>=-4 && s.top()<=-3)
        {
            nr++;
            b[nr]=s.top();
            s.pop();
        }
        s.push(a[i]);
    }
    if (a[i]==-5)
    {
        while (!s.empty() && s.top()>=-5 && s.top()<=-3)
        {
            nr++;
            b[nr]=s.top();
            s.pop();
        }
        s.push(a[i]);
    }
    }
}

while (!s.empty())
{
    nr++;
    b[nr]=s.top();
    s.pop();
}
}

void evaluate()
{
    int aux, auxx;
for (i=1;i<=nr;i++)
{
if (b[i]>=0) s.push(b[i]);
if (b[i]==-3)
{
    aux=s.top();
    s.pop();
    if (aux>1) aux=vec[aux];
    aux+=1;
    aux%=2;
    s.push(aux);
}
if (b[i]==-4)
{
    aux=s.top();
    s.pop();
    auxx=s.top();
    s.pop();
    if (aux>1) aux=vec[aux];
    if (auxx>1) auxx=vec[auxx];
    if (auxx==1 && aux==1) s.push(1);
    else s.push(0);
}
if (b[i]==-5)
{
        aux=s.top();
    s.pop();
    auxx=s.top();
    s.pop();
    if (aux>1) aux=vec[aux];
    if (auxx>1) auxx=vec[auxx];
    if (auxx==1 || aux==1) s.push(1);
    else s.push(0);
}
}
g<<s.top();
s.pop();
}



int main()
{
f.getline (ch,1001);
transforme();
//for (int i=1;i<=nr;i++) cout<<a[i]<<" ";
//cout<<"\n";
postfix();
f>>n;

f.getline (ch,2);
f.getline (ch,20000);
//for (i=1;i<=nr;i++) cout<<b[i]<<" ";
n--;
for (int j=0;j<=n;j++)
{
    int x=ch[j];
    vec[x]++;
    vec[x]%=2;
    evaluate();
}

    return 0;
}