Pagini recente » Cod sursa (job #1524700) | Cod sursa (job #674772) | Cod sursa (job #913440) | Cod sursa (job #2573691) | Cod sursa (job #2361512)
#include <iostream>
#include <fstream>
#include <cstring>
#include <stack>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
int n;
char s[1005],a[1005],nr[105];
bool valori[95];
int vf = -1;
int GetPriority(char c)
{
switch (c)
{
case '!':
return 3;
case '&':
return 2;
case '|':
return 1;
}
}
int GetValue(bool val1,bool val2,char c)
{
switch (c)
{
case '&':
return val1 && val2;
case '|':
return val1 || val2;
}
}
int Evalueaza()
{
int i = 0;
stack <bool> vals;
stack <char> op;
while (a[i] != NULL)
{
if (a[i] == '(')
{
op.push(a[i]);
}
else if (a[i] == '1')
{
vals.push(1);
}
else if (a[i] == '0')
{
vals.push(0);
}
else if (a[i] >= 'A' && a[i] <= 'Z')
{
vals.push(valori[a[i]]);
}
else if (a[i] == ')')
{
///cout << i << " ";
while (!op.empty() && op.top() != '(')
{
bool val2 = vals.top();
vals.pop();
char op1 = op.top();
op.pop();
if (op1 == '!')
{
///cout << val2 << " " << op1 << " ";
vals.push(!val2);
///cout << vals.top() << " ";
}
else
{
bool val1 = vals.top();
vals.pop();
///cout << val2 << " " << op1 << " " << val1 << " ";
vals.push(GetValue(val1,val2,op1));
/// cout << vals.top() << " ";
}
///cout << vals.top();
///cout << endl;
}
op.pop();
}
else
{
while (!op.empty() && op.top() != '(' && GetPriority(op.top()) >= GetPriority(a[i]))
{
bool val2 = vals.top();
vals.pop();
char op1 = op.top();
op.pop();
if (op1 == '!')
{
vals.push(!val2);
}
else
{
bool val1 = vals.top();
vals.pop();
vals.push(GetValue(val1,val2,op1));
}
}
op.push(a[i]);
}
i++;
}
while (!op.empty())
{
bool val2 = vals.top();
vals.pop();
char op1 = op.top();
op.pop();
if (op1 == '!')
{
vals.push(!val2);
}
else
{
bool val1 = vals.top();
vals.pop();
vals.push(GetValue(val1,val2,op1));
}
}
return vals.top();
}
int main()
{
int i = 0,k;
in.getline(s,1005);
in >> n;
in >> nr;
while (s[i] != NULL)
{
if (s[i] == '(')
{
a[++vf] = s[i];
}
if (s[i] == ')')
{
a[++vf] = s[i];
}
if (s[i] >= 'A' && s[i] <= 'Z')
{
int ok = 0;
char c[10];
int top = -1;
while (s[i] >= 'A' && s[i] <= 'Z')
{
ok = 1;
c[++top] = s[i];
i++;
}
c[++top] = NULL;
if (top == 1)
{
a[++vf] = s[i-1];
i--;
}
else
{
if (strcmp(c,"AND") == 0)
{
a[++vf] = '&';
}
if (strcmp(c,"OR") == 0)
{
a[++vf] = '|';
}
if (strcmp(c,"NOT") == 0)
{
a[++vf] = '!';
}
if (strcmp(c,"TRUE") == 0)
{
a[++vf] = '1';
}
if (strcmp(c,"FALSE") == 0)
{
a[++vf] = '0';
}
i--;
}
}
i++;
}
a[++vf] = NULL;
for (k=0; k<n; k++)
{
valori[nr[k]] = !valori[nr[k]];
out << Evalueaza();
}
return 0;
}