Pagini recente » Cod sursa (job #2359014) | Cod sursa (job #1098863) | Cod sursa (job #245154) | Cod sursa (job #60048) | Cod sursa (job #1214496)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int letter[27];
char constants[][6] = { "TRUE", "FALSE" };
struct Stack
{
char val[501];
int top;
Stack(int _top = 0) :top(_top){};
bool isEmpty() { return(top <= 0); }
char Top() { return val[top]; }
void pop() { --top; }
void push(char &c) { val[++top] = c; }
void clear() { top = 0; }
};
struct nod
{
int val;
int ind;
char op;
nod *left, *right;
};
typedef struct TreeStack
{
nod *val[1002];
int top;
TreeStack(int _top = 0) :top(_top){};
} Tree;
char convert(char s[])
{
if (strcmp(s, "AND")==0) return '*';
else if (strcmp(s, "OR") == 0) return '+';
else return '!';
}
int priority(char c)
{
int pri = 0;
if (c == '+') pri = 1;
else if (c == '*') pri = 2;
else if (c == '!') pri = 3;
return pri;
}
void push(TreeStack *&t, nod* n)
{
t->val[++t->top] = n;
}
void assignTree(nod*root)
{
if (root != NULL)
{
assignTree(root->left);
if (root->op == ' ')
if (root->ind != -1) root->val = letter[root->ind];
assignTree(root->right);
}
}
int evaluate(nod*root)
{
if (root->op == ' ') return root->val;
else
{
if (root->op == '!') return 1 - evaluate(root->right);
else if (root->op == '+') return evaluate(root->left) || evaluate(root->right);
else return evaluate(root->left) && evaluate(root->right);
}
}
int main()
{
ifstream f("bool.in");
ofstream g("bool.out");
int n,i,j,closed_brackets,length,p,ok,m=0;
char elem[1002],c,cuv[1002],postfix[2005];
Stack stack(0);
f.getline(elem, 1001);
i = p = 0;
length = strlen(elem);
while (i <= length)
{
ok = 0;
closed_brackets = 0;
if (elem[i] != ' ' && elem[i]!='\0') cuv[p++] = elem[i];
else
{
cuv[p] = '\0';
if (cuv[0] == '(')
{
while (cuv[0] == '(')
{
stack.push(cuv[0]);
strncpy_s(cuv + 0, p,cuv + 1, p);
--p;
}
}
if (cuv[p - 1] == ')')
{
while (cuv[p-1] == ')')
{
++closed_brackets;
cuv[p - 1] = '\0';
--p;
}
}
if (strcmp(cuv,"TRUE") == 0 || strcmp(cuv,"FALSE") == 0)
{
if (strcmp(cuv,"TRUE") == 0)
{
postfix[m++] = '1';
postfix[m++] = ' ';
}
else
{
postfix[m++] = '0';
postfix[m++] = ' ';
}
}
else if (p == 1 && cuv[0] >= 'A' && cuv[0] <= 'Z')
{
postfix[m++] = cuv[0];
postfix[m++] = ' ';
}
else
{
c = convert(cuv);
if (stack.isEmpty()) stack.push(c);
else
{
while (priority(stack.Top()) >= priority(c))
{
postfix[m++] = stack.Top();
postfix[m++] = ' ';
stack.pop();
}
stack.push(c);
}
}
for (j = 1; j <= closed_brackets; ++j)
{
while (stack.Top() != '(')
{
postfix[m++] = stack.Top();
postfix[m++] = ' ';
stack.pop();
}
stack.pop();
}
p = closed_brackets = 0;
}
++i;
}
while (!stack.isEmpty())
{
postfix[m++] = stack.Top();
postfix[m++] = ' ';
stack.pop();
}
postfix[m] = '\0';
i = 0;
Tree* t = new TreeStack;
while (i < m)
{
if (postfix[i] != ' ')
{
nod *node = new nod;
if (postfix[i] >= 'A' && postfix[i] <= 'Z')
{
node->val = letter[postfix[i] - 'A'];
node->ind = postfix[i] - 'A';
node->op = ' ';
node->left = node->right = NULL;
push(t, node);
}
else if (postfix[i] == '1' || postfix[i] == '0')
{
node->val = (postfix[i] == '1') ? 1 : 0;
node->ind = -1;
node->op = ' ';
node->left = node->right = NULL;
push(t, node);
}
else
{
if (postfix[i] == '!')
{
nod *op1 = t->val[t->top];
--t->top;
node->op = '!';
node->right = op1;
node->left = NULL;
push(t, node);
}
else
{
nod *op1 = t->val[t->top];
--t->top;
nod *op2 = t->val[t->top];
--t->top;
node->op = postfix[i];
node->right = op1;
node->left = op2;
push(t, node);
}
}
++i;
}
else ++i;
}
nod *root = t->val[t->top];
f >> n;
for (i = 1; i <= n; ++i)
{
f >> c;
letter[c - 'A'] = 1 - letter[c - 'A'];
assignTree(root);
g<<evaluate(root);
}
f.close();
g.close();
return 0;
}