Pagini recente » Cod sursa (job #1768473) | Cod sursa (job #567083) | Cod sursa (job #1091433) | Cod sursa (job #122675) | Cod sursa (job #626775)
Cod sursa(job #626775)
#include <fstream>
using namespace std;
ifstream in ("bool.in");
ofstream out ("bool.out");
const int N = 1003;
char sc[N], *p, c, s[N], sc1[N], *p1;
int n;
void parsare () {
p1 = sc1;
p = sc;
while (*p1) {
if (*p1 == ' ') {
++p1;
continue;
}
if (*p1 == 'A' && *(p1+1) == 'N') {
*p = '&';
++p;
p1 += 3;
continue;
}
if (*p1 == 'O' && *(p1+1) == 'R') {
*p = '|';
++p;
p1+=2;
continue;
}
if (*p1 == 'N' && *(p1+1) == 'O') {
*p = '!';
++p;
p1+=3;
continue;
}
if (*p1 == '(' || *p1 == ')') {
*p = *p1;
++p;
++p1;
continue;
}
if (*p1 == 'T' && *(p1+1)=='R') {
*p = '1';
++p;
p1 += 4;
continue;
}
if (*p1 == 'F' && *(p1 + 1) == 'A') {
*p = '0';
++p;
p1 += 5;
continue;
}
*p = *p1;
++p1, ++p;
}
}
void init () {
p1 = sc;
p = s;
while (*p1) {
if (*p1 == '(' || *p1 == ')' || *p1 == '!' || *p1 == '&' || *p1 == '|' || *p1 =='0' || *p1 == '1') {
*p = *p1;
++p,++p1;
continue;
}
*p = '0';
++p;
++p1;
}
}
void trans (char c) {
p1=sc;
p=s;
while (*p1) {
if (*p1 == c) {
*p = (*p=='1') ? '0' : '1';
}
++p1;
++p;
}
}
bool andd(), orr(), termen();
bool andd() {
bool rez = orr();
while (*p == '&') {
++p;
rez = rez & orr();
}
return rez;
}
bool orr (){
bool rez = termen();
while (*p == '|') {
++p;
rez = rez | termen();
}
return rez;
}
bool termen () {
bool val;
bool semn = 1;
if (*p == '!') {
++p;
semn = 0;
}
if (*p == '(') {
++p;
val = andd();
++p;
if (semn) return val;
else return not val;
}
val = *p - '0';
++p;
if (semn) return val;
return not val;
}
void citire () {
in.getline (sc1,N);
parsare();
init ();
in >> n >> ws;
for (;n--;) {
in >> c;
trans (c);
p = s;
p1 = sc;
out << andd ();
}
}
int main (){
citire();
return 0;
}