Pagini recente » Cod sursa (job #780224) | Cod sursa (job #2338491) | Cod sursa (job #2650088) | Cod sursa (job #1906716) | Cod sursa (job #2652593)
#include <fstream>
#include <cstring>
#include <queue>
using namespace std;
ifstream f ( "bool.in" );
ofstream g ( "bool.out" );
const int N = 1005;
deque < int > num;
deque < char > op;
char S[N];
int val[N], pr[N];
void calculate ( ){
char ch = op.front ( );
if ( ch == '!' )
num.front ( ) = ! num.front ( );
else if ( ch == '|' ){
int a = num.front ( );
num.pop_front ( );
num.front ( ) = num.front ( ) | a;
}
else if ( ch == '&' ){
int a = num.front ( );
num.pop_front ( );
num.front ( ) = num.front ( ) & a;
}
op.pop_front ( );
}
void operate ( int start ){
for ( int i = start; S[i] != 0; i++ ){
if ( S[i] >= 'A' && S[i] <= 'Z' )
num.push_front ( val[S[i] - 'A'] );
else if ( S[i] == '|' || S[i] == '&' || S[i] == '!' ){
while ( pr[op.front ( )] > pr[S[i]] )
calculate ( );
op.push_front ( S[i] );
}
else if ( S[i] == '(' )
op.push_front ( S[i] );
else if ( S[i] == ')' ){
while ( op.front ( ) != '(' )
calculate ( );
op.pop_front ( );
}
else if ( S[i] == '1' || S[i] == '0' )
num.push_front ( S[i] - '0' );
}
while ( ! op.empty ( ) )
calculate ( );
}
int main()
{ int n, i;
char ch;
f.getline ( S + 1, N );
f >> n;
for ( i = 1; S[i] != 0; i++ ){
if ( S[i] == 'T' && S[i + 1] == 'R' ){
strcpy ( S + i + 1, S + i + 4 );
S[i] = '1';
}
else if ( S[i] == 'F' && S[i + 1] == 'A' ){
strcpy ( S + i + 1, S + i + 5 );
S[i] = '0';
}
else if ( S[i] == 'A' && S[i + 1] == 'N' ){
strcpy ( S + i + 1, S + i + 3 );
S[i] = '&';
}
else if ( S[i] == 'N' && S[i + 1] == 'O' ){
strcpy ( S + i + 1, S + i + 3 );
S[i] = '!';
}
else if ( S[i] == 'O' && S[i + 1] == 'R' ){
strcpy ( S + i + 1, S + i + 2 );
S[i] = '|';
}
else if ( S[i] == ' ' ){
strcpy ( S + i, S + i + 1 );
i--;
}
}
pr['!'] = 2;
pr['&'] = 1;
for ( i = 1; i <= n; i++ ){
f >> ch;
val[ch - 'A'] = ! val[ch - 'A'];
operate ( 1 );
g << num.front ( );
num.pop_front ( );
}
return 0;
}