Pagini recente » Cod sursa (job #2787036) | Cod sursa (job #2926424) | Cod sursa (job #2296006) | Cod sursa (job #2256538) | Cod sursa (job #3269628)
#include <fstream>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
int n, ind;
string s, modif;
int v[30];
int constanta();
int paranteza();
int factor();
int termen();
int expresie();
int constanta()
{
int rez;
if(s[ind] == 'T' && s[ind + 1] == 'R' && s[ind + 2] == 'U' && s[ind + 3] == 'E')
{
ind += 4;
rez = 1;
}
else if(s[ind] == 'F' && s[ind + 1] == 'A' && s[ind + 2] == 'L' && s[ind + 3] == 'S' && s[ind + 4] == 'E')
{
ind += 5;
rez = 0;
}
else
{
rez = v[s[ind] - 'A'];
ind++;
}
if(s[ind] == ' ')
{
ind++;
}
return rez;
}
int paranteza()
{
int rez;
if(s[ind] == '(')
{
ind++;
rez = expresie();
ind++;
if(s[ind] == ' ')
{
ind++;
}
}
else
{
rez = constanta();
if(s[ind] == ' ')
{
ind++;
}
}
return rez;
}
int factor() // NOT
{
int rez;
if(s[ind] == 'N' && s[ind + 1] == 'O' && s[ind + 2] == 'T')
{
ind += 4;
rez = 1 - constanta();
ind++;
}
else
{
rez = constanta();
ind++;
}
return rez;
}
int termen() // AND
{
int rez = factor();
ind++;
while(s[ind] == 'A' && s[ind + 1] == 'N' && s[ind + 2] == 'D')
{
ind += 4; //OR si spatiu
rez &= factor();
ind++;
}
return rez;
}
int expresie() // OR
{
int rez = termen();
ind++;
while(s[ind] == 'O' && s[ind + 1] == 'R')
{
ind += 3; //OR si spatiu
rez |= termen();
ind++;
}
return rez;
}
int main()
{
getline(in, s);
s = s + "$$$$$$";
getline(in, modif);
getline(in, modif);
in>>n>>modif;
for(int i = 0; i<modif.size(); i++)
{
v[modif[i] - 'A'] = 1 - v[modif[i] - 'A'];
ind = 0;
out<<expresie();
}
return 0;
}