Pagini recente » Cod sursa (job #505124) | Cod sursa (job #2796981) | Cod sursa (job #2605465) | Cod sursa (job #2549338) | Cod sursa (job #1076496)
#include <iostream>
#include <string.h>
#include <fstream>
#include <cstdio>
const static int NMAX = 1001;
using namespace std;
int pos;
char expression[NMAX];
bool litere[100];
bool termen();
bool factor();
bool eval()
{
bool rezultat = termen();
while (expression[pos] == 'O')
{
char op[4] = { 0 , 0 , 0 , 0};
for (int i = 0; expression[pos] != ' '; pos++,i++)
op[i] = expression[pos];
++pos;
rezultat |= termen();
}
return rezultat;
}
bool termen()
{
bool rezultat = factor();
while (expression[pos] == 'A')
{
char op[4] = { 0 , 0 , 0 , 0};
for (int i = 0; expression[pos] != ' '; pos++,i++)
op[i] = expression[pos];
++pos;
rezultat &= factor();
}
return rezultat;
}
bool factor()
{
bool rezultat = 0;
if (expression[pos] == '(')
{
pos++;
rezultat = eval();
pos++;
}
else
{
// verificam sa nu fie un NOT in fata
char op[4] = { 0 , 0 , 0 , 0};
for(int i = 0; expression[pos] >= 'A' && expression[pos] <= 'Z'; pos++ , i++)
{
op[i] = expression[pos];
}
if (strcmp(op,"NOT") == 0)
{
pos++;
rezultat = !factor();
}
else if (strcmp(op,"TRUE") == 0)
{
pos++;
rezultat = true;
}
else if (strcmp(op,"FALSE") == 0)
{
pos++;
rezultat = false;
}
else
{
char litera = op[0];
pos++;
rezultat = litere[litera - 'A'];
}
}
return rezultat;
}
int main()
{
FILE * input = fopen("bool.in","r");
ofstream output("bool.out");
fgets(expression,sizeof(char) * NMAX,input);
expression[strlen(expression) - 1] = 0;
int N;
fscanf(input,"%d\n",&N);
for (int i = 0; i < N ; i++)
{
char litera = fgetc(input);
litere[litera - 'A'] = !litere[litera - 'A'];
pos = 0;
output << eval();
}
return 0;
}