Pagini recente » Cod sursa (job #759426) | Cod sursa (job #1767281) | Cod sursa (job #1373693) | Cod sursa (job #1810872) | Cod sursa (job #2140346)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
bool F[1000];
int L, M, pos;
string s;
bool expresie();
bool termen();
bool factor();
bool expresie(){
bool R = termen();
while( pos < L && s[pos] == '|' )
R |= termen(), pos++;
return R;
}
bool termen(){
bool R = factor();
while( pos < L && s[pos] == '&' )
R &= factor(), pos++;
return R;
}
bool factor(){
bool R , ok = 0;
while( pos < L && s[pos] == '!' )
ok = !ok, pos++;
if( s[pos] == '(' ){
pos++;
R = expresie();
pos++;
}else{
R = F[ s[pos] - 'A' ];
pos++;
}
if( ok == 1 )
R = !R;
return R;
}
int main(){
getline( fin, s );
while( s.find( "TRUE" ) != string::npos )
s.replace( s.find( "TRUE" ), 4, "t" );
while( s.find( "FALSE" ) != string::npos )
s.replace( s.find( "FALSE" ), 5, "f" );
while( s.find( "AND" ) != string::npos )
s.replace( s.find( "AND" ), 3, "&" );
while( s.find( "OR" ) != string::npos )
s.replace( s.find( "OR" ), 2, "|" );
while( s.find( "NOT" ) != string::npos )
s.replace( s.find( "NOT" ), 3, "!" );
while( s.find( " " ) != string::npos )
s.replace( s.find( " " ), 1, "" );
F[ 't' - 'A' ] = true;
L = (int)s.length();
fin >> M;
for( int i = 0; i < M; i++ ){
char x; fin >> x;
F[ x - 'A' ] = !F[ x - 'A' ];
pos = 0;
fout << expresie();
}
return 0;
}