Pagini recente » Cod sursa (job #1663552) | Cod sursa (job #1441540) | Cod sursa (job #2728645) | Cod sursa (job #1633882) | Cod sursa (job #2703630)
#include <fstream>
using namespace std;
const int LE = 1001;
const int NOP = 101;
const int NL = 26;
char s[LE], op[NOP];
bool val[NL];
int p;
void transforma();
bool expresie();
bool termen();
bool factor();
void transforma()
{
int nr = 0, i = 0;
while (s[i] != '\0')
{
if (s[i] == 'T' && s[i + 1] == 'R')
{
s[nr++] = '1';
i += 4;
}
else if (s[i] == 'F' && s[i + 1] == 'A')
{
s[nr++] = '0';
i += 5;
}
else if (s[i] == 'N' && s[i + 1] == 'O')
{
s[nr++] = '!';
i += 3;
}
else if (s[i] == 'A' && s[i + 1] == 'N')
{
s[nr++] = '&';
i += 3;
}
else if (s[i] == 'O' && s[i + 1] == 'R')
{
s[nr++] = '|';
i += 2;
}
else if (s[i] != ' ')
{
s[nr++] = s[i++];
}
else
{
i++;
}
}
s[nr] = '\0';
}
bool expresie()
{
bool sau = termen();
while (s[p] == '|')
{
p++;
sau = (termen() || sau);
}
return sau;
}
bool termen()
{
bool shi = factor();
while (s[p] == '&')
{
p++;
shi = (factor() && shi);
}
return shi;
}
bool factor()
{
bool nu = false, v;
while (s[p] == '!')
{
p++;
nu = (!nu);
}
if (s[p] == '(')
{
p++;
v = expresie();
p++;
if (nu)
{
return !v;
}
return v;
}
if (s[p] == '0')
{
p++;
v = false;
}
else if (s[p] == '1')
{
p++;
v = true;
}
else
{
v = val[s[p++] - 'A'];
}
if (nu)
{
return !v;
}
return v;
}
int main()
{
ifstream in("bool.in");
ofstream out("bool.out");
in.getline(s, LE);
transforma();
//out << s << "\n";
int nop;
in >> nop >> op;
for (int i = 0; i < nop; i++)
{
val[op[i] - 'A'] = (!val[op[i] - 'A']);
p = 0;
out << expresie();
}
in.close();
out.close();
return 0;
}