Pagini recente » Cod sursa (job #1842575) | Cod sursa (job #978789) | Cod sursa (job #850451) | Cod sursa (job #1927102) | Cod sursa (job #2791074)
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1///ce sa i faci, programez in C:))
#define FALSE 0
int v[1002], var[26];///de pus pe char
int ind, maxi;
char paranteze();
char not();
char isconst();
char isconst(){
if((v[ind] == 0) || (v[ind] == 1)){
return TRUE;
}else{
return FALSE;
}
}
char not(){
char rez;
ind++;
if(v[ind] == '('){
ind++;
rez = !paranteze();
ind++;
}else if(v[ind] == '!'){
rez = !not();
}else if(isconst()){
rez = !v[ind];
ind++;
}else{
rez = !var[v[ind] - 'A'];
ind++;
}
return rez;
}
char and(char rez){
ind++;
if(v[ind] == '('){
ind++;
rez = rez & paranteze();
ind++;
}else if(v[ind] == '!'){
rez = rez & not();
}else if(isconst()){
rez = rez & v[ind];
ind++;
}else{
rez = rez & var[v[ind] - 'A'];
ind++;
}
if(v[ind] == '&'){
rez = and(rez);
}
return rez;
}
char paranteze(){
char rez;
if(v[ind] == '!'){
rez = not();
}else if(v[ind] == '('){
ind++;
rez = paranteze();
ind++;
}else if(isconst()){
rez = v[ind];
ind++;
}else{
rez = var[v[ind] - 'A'];
ind++;
}
while((ind < maxi) && (v[ind] != ')')){
if(v[ind] == '|'){
ind++;
if((v[ind] != '(') && (v[ind] != '!')){
if((v[ind + 1] == '|') || (v[ind + 1] == ')') || (v[ind + 1] == -1)){
if(isconst()){
rez = rez | v[ind];
}else{
rez = rez | var[v[ind] - 'A'];
}
ind++;
}else if(v[ind + 1] == '&'){
ind++;
rez = rez | and(v[ind]);
}
}else if(v[ind] == '('){
ind++;
rez = rez | paranteze();
ind++;
}else{
rez = rez | not();
}
}else if(v[ind] == '&'){
rez = and(rez);
}
}
return rez;
}
int main()
{
FILE *fin, *fout;
int n, i;
char ch;
ch = 0;
i = 0;
fin = fopen("bool.in", "r");
///citire sir, pff ...
while(ch != '\n'){
if(ch != ')'){
ch = fgetc(fin);
}
while((ch == '(') || (ch == ')')){
v[i] = ch;
i++;
ch = fgetc(fin);
}
if(('A' <= ch) && (ch <= 'Z')){
if(ch == 'N'){
ch = fgetc(fin);
if(ch == 'O'){
v[i] = '!';
ch = fgetc(fin);
ch = fgetc(fin);
}else{
v[i] = 'N';
}
}else if(ch == 'A'){
ch = fgetc(fin);
if(ch == 'N'){
v[i] = '&';
ch = fgetc(fin);
ch = fgetc(fin);
}else{
v[i] = 'A';
}
}else if(ch == 'O'){
ch = fgetc(fin);
if(ch == 'R'){
v[i] = '|';
ch = fgetc(fin);
}else{
v[i] = 'O';
}
}else if(ch == 'T'){
ch = fgetc(fin);
if(ch == 'R'){
v[i] = 1;
ch = fgetc(fin);
ch = fgetc(fin);
ch = fgetc(fin);
}else{
v[i] = 'T';
}
}else if(ch == 'F'){
ch = fgetc(fin);
if(ch == 'A'){
v[i] = 0;
ch = fgetc(fin);
ch = fgetc(fin);
ch = fgetc(fin);
ch = fgetc(fin);
}else{
v[i] = 'F';
}
}else{
v[i] = ch;
ch = fgetc(fin);
}
i++;
}
}
maxi = i;
v[maxi] = v[maxi + 1] = -1;
fscanf(fin, "%d\n", &n);
fout = fopen("bool.out", "w");
for(i = 0; i < n; i++){
ch = fgetc(fin);
var[ch - 'A'] = !var[ch - 'A'];
ind = 0;
fputc(paranteze() + '0', fout);
}
fclose(fin);
fclose(fout);
return 0;
}