Pagini recente » Cod sursa (job #582202) | Cod sursa (job #923097) | Cod sursa (job #533611) | Cod sursa (job #598107) | Cod sursa (job #2795471)
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <cstring>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
string s, schimbari;
char s1[1001];
map<char, bool> variabile;
bool si(bool a, bool b)
{
return a*b;
}
bool sau(bool a, bool b)
{
return (a + b - a*b);
}
bool nu(bool a)
{
return (1 - a);
}
bool evaluare(string e)
{
int paranteze = 0,ok = 1;
if(e.size() > 1)
{
while(ok && e.size() > 1)
{
for(int i = 0; i < e.size() && ok; i++)
{
if(e[i] == '(') paranteze++;
if(e[i] == ')') paranteze--;
if(paranteze == 0 && i > 0 && i < e.size()-1) ok = 0;
}
paranteze = 0;
if(ok)
{
for(int i = 1; i < e.size() - 1; i++)
{
e[i-1] = e[i];
}
e.resize(e.size() - 2);
}
}
}
if(e.size() == 1)
{
return variabile[e[0]];
}
if(e == "TRUE") return 1;
if(e == "FALSE") return 0;
for(int i = 0; i < e.size();i++)
{
if(e[i] == '(') paranteze++;
if(e[i] == ')') paranteze--;
if(e[i] == 'O' && paranteze == 0)
{
if(i < e.size() - 1)
{
if(e[i + 1] == 'R')
{
string e1,e2;
for(int j = 0; j <= i - 2; j++)
{
e1 += e[j];
}
for(int j = i + 3; j < e.size(); j++)
{
e2 += e[j];
}
return sau(evaluare(e1), evaluare(e2));
}
}
}
}
for(int i = 0; i < e.size();i++)
{
if(e[i] == '(') paranteze++;
if(e[i] == ')') paranteze--;
if(e[i] == 'A' && paranteze == 0)
{
if(i < e.size() - 1)
{
if(e[i + 1] == 'N')
{
string e1,e2;
for(int j = 0; j <= i - 2; j++)
{
e1 += e[j];
}
for(int j = i + 4; j < e.size(); j++)
{
e2 += e[j];
}
return si(evaluare(e1), evaluare(e2));
}
}
}
}
for(int i = 0; i < e.size();i++)
{
if(e[i] == '(') paranteze++;
if(e[i] == ')') paranteze--;
if(e[i] == 'N' && paranteze == 0)
{
if(i < e.size() - 1)
{
if(e[i + 1] == 'O')
{
string e1;
for(int j = i + 4; j < e.size(); j++)
{
e1 += e[j];
}
return nu(evaluare(e1));
}
}
}
}
}
int main()
{
for(char i = 'A'; i <= 'Z'; i++)
{
variabile[i] = 0;
}
in.getline(s1, 1001);
s = s1;
int n;
in >> n;
in >> schimbari;
for(int i = 0; i < schimbari.size(); i++)
{
variabile[schimbari[i]] = 1 - variabile[schimbari[i]];
out << evaluare(s);
}
return 0;
}