Pagini recente » Cod sursa (job #3344360) | Cod sursa (job #730476) | Cod sursa (job #3318352) | Cod sursa (job #3333688) | Cod sursa (job #3310985)
#include <iostream>
#include <fstream>
#include <cstring>
#include <stack>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
stack<int> S;
stack<char> op;
int f[26];
int priority(char op1)
{
if (op1 == 'N')
return 3;
if (op1 == 'A')
return 2;
if (op1 == 'O')
return 1;
return 0;
}
void ApplyOp()
{
if (op.top() == 'N')
{
if (S.top() == 1)
{
S.pop();
S.push(0);
}
if (S.top() == 0)
{
S.pop();
S.push(1);
}
}
if (op.top() == 'A')
{
int b = S.top();
S.pop();
int c = S.top();
S.pop();
S.push((b && c));
}
if (op.top() == 'O')
{
int b = S.top();
S.pop();
int c = S.top();
S.pop();
S.push((b || c));
}
op.pop();
}
int main()
{
bool ok = 0;
int n, m;
char s[1001], c;
fin.getline(s, 1001);
fin >> n;
m = strlen(s);
for (int j = 1; j <= n; j++)
{
fin >> c;
f[c - 'A'] = !f[c - 'A'];
cout << j << "\n";
for (int i = 0; i < m; i++)
{
cout << i << " ";
if (s[i] >= 'A' && s[i] <= 'Z' && (s[i + 1] < 'A' || s[i + 1] > 'Z'))
{
if (f[s[i] - 'A'] == 1)
S.push(f[s[i] - 'A']);
else
S.push(f[s[i] - 'A']);
}
if (s[i] == '(')
op.push('(');
if (s[i] == ' ')
continue;
if (s[i] >= 'A' && s[i] <= 'Z' && s[i + 1] >= 'A' && s[i + 1] <= 'Z')
{
cout << s[i] << "\n";
if (s[i] == 'N')
{
ok = 1;
op.push(s[i]);
i += 2;
}
else if (s[i] == 'A')
{
ok = 1;
op.push(s[i]);
i += 2;
}
else if (s[i] == 'O')
{
ok = 1;
op.push(s[i]);
i++;
}
else if (s[i] == 'T')
{
ok = 0;
S.push(1);
i += 3;
}
else if (s[i] == 'F')
{
ok = 0;
S.push(0);
i += 4;
}
if (ok == 1)
{
char new_op = op.top();
op.pop();
cout << new_op << " " << op.size() << "\n";
while (op.size() != 0 && priority(op.top()) >= priority(new_op))
{
ApplyOp();
}
op.push(new_op);
}
}
if (s[i] == ')')
{
while (op.top() != '(')
{
ApplyOp();
}
op.pop();
}
}
while (op.size() != 0)
{
ApplyOp();
}
fout << S.top();
S.pop();
}
return 0;
}