Pagini recente » Cod sursa (job #913540) | Cod sursa (job #1947950) | Cod sursa (job #1968941) | Cod sursa (job #3264842) | Cod sursa (job #2790966)
// Mihai Priboi
#include <bits/stdc++.h>
#define MAXN 1001
char s[MAXN];
int ind;
bool val['Z' + 1];
bool or_operation();
bool factor() {
bool nr = false;
if( s[ind] == '(' ) {
ind++;
nr = or_operation();
ind++;
}
else if( s[ind] == '!' ) {
ind++;
nr = !factor();
}
else if( s[ind] >= 'A' && s[ind] <= 'Z' )
nr = val[s[ind]];
else if( s[ind] == 0 || s[ind] == 1 )
nr = s[ind];
return nr;
}
bool and_operation() {
bool rez;
rez = factor();
while( s[ind] == '&' ) {
ind++;
rez = factor() & rez;
}
return rez;
}
bool or_operation() {
bool rez;
rez = and_operation();
while( s[ind] == '|' ) {
ind++;
rez = and_operation() | rez;
}
return rez;
}
int main() {
FILE *fin, *fout;
int n, i, t;
char ch;
fin = fopen( "bool.in", "r" );
fgets( s, MAXN, fin );
fscanf( fin, "%d ", &n );
// prelucrare expresie
ind = i = 0;
while( s[ind] != '\n' ) {
if( s[ind] == 'T' && s[ind + 1] == 'R' ) { // TRUE devine 1
s[i++] = 1;
ind += 4;
}
else if( s[ind] == 'F' && s[ind + 1] == 'A' ) { // FALSE devine 0
s[i++] = 0;
ind += 5;
}
else if( s[ind] == 'N' && s[ind + 1] == 'O' ) { // NOR devine !
s[i++] = '!';
ind += 3;
}
else if( s[ind] == 'A' && s[ind + 1] == 'N' ) { // AND devine &
s[i++] = '&';
ind += 3;
}
else if( s[ind] == 'O' && s[ind + 1] == 'R' ) { // OR devine |
s[i++] = '|';
ind += 2;
}
else if( s[ind] == '(' || s[ind] == ')' )
s[i++] = s[ind];
else if( s[ind] >= 'A' && s[ind] <= 'Z' ) // pastram variabilele la fel
s[i++] = s[ind];
// trecem peste spatii
ind++;
}
s[i] = '\n';
fout = fopen( "bool.out", "w" );
for( i = 0; i < n; i++ ) {
ch = fgetc( fin );
val[ch] = !val[ch];
ind = 0;
fprintf( fout, "%d", or_operation() );
}
fclose( fin );
fclose( fout );
return 0;
}