Pagini recente » Cod sursa (job #3222088) | Cod sursa (job #2346519) | Cod sursa (job #3276154) | Cod sursa (job #2838027) | Cod sursa (job #2870369)
#include <bits/stdc++.h>
#define N 1008
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
int n;
char s[N];
map< string, int > prec;
map< char, int > val;
void Citire()
{
fin.getline( s + 1, N );
n = strlen( s + 1 );
prec["("] = 0;
prec[")"] = 0;
prec["OR"] = 1;
prec["AND"] = 2;
prec["NOT"] = 3;
}
stack<string> ops;
stack<int> values;
inline int rez( int val1, int val2, string op )
{
if( op == "OR" )
return val1 | val2;
if( op == "AND" )
return val1 & val2;
if( op == "NOT" )
return !val1;
return 0;
}
void Rezolvare()
{
int i;
for( i=1; i<=n; i++ )
{
if( s[i] == ' ' )
continue;
if( s[i] == '(' )
{
ops.push( "(" );
continue;
}
if( s[i] == ')' )
{
while( !ops.empty() && ops.top() != "(" )
{
int val1, val2;
if( ops.top() != "NOT" )
{
val2 = values.top();
values.pop();
}
val1 = values.top();
values.pop();
string op;
op = ops.top();
ops.pop();
values.push( rez( val1, val2, op ) );
}
ops.pop();
continue;
}
if( s[i] == 'T' && s[i + 1] == 'R' )
{
i += 3;
values.push( 1 );
continue;
}
if( s[i] == 'F' && s[i + 1] == 'A' )
{
i += 4;
values.push( 0 );
continue;
}
if( isalpha( s[i] ) && (i == n || !isalpha( s[i + 1] )) )
{
values.push( val[ s[i] ] );
continue;
}
{
string op = "";
while( isalpha( s[i] ) )
op += s[i], i++;
i--;
while( !ops.empty() && op != "NOT" && prec[ ops.top() ] >= prec[ op ] )
{
int val1, val2;
if( ops.top() != "NOT" )
{
val2 = values.top();
values.pop();
}
val1 = values.top();
values.pop();
string op2;
op2 = ops.top();
ops.pop();
values.push( rez( val1, val2, op2 ) );
}
ops.push( op );
}
}
while( !ops.empty() )
{
int val1, val2;
if( ops.top() != "NOT" )
{
val2 = values.top();
values.pop();
}
val1 = values.top();
values.pop();
string op2;
op2 = ops.top();
ops.pop();
values.push( rez( val1, val2, op2 ) );
}
fout << values.top();
values.pop();
}
int main()
{
Citire();
int q;
fin >> q;
fin.get();
for( int i=1; i<=q; i++ )
{
char c;
fin >> c;
val[c] = !val[c];
Rezolvare();
}
//cout << sizeof( Lee ) / 1024.0 / 1024.0;
return 0;
}