Cod sursa(job #942518)

Utilizator OpportunityVlad Negura Opportunity Data 22 aprilie 2013 20:23:20
Problema Bool Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

ifstream fi("bool.in"); 
ofstream fo("bool.out");

string s;
bool a[30];
int n,i,p;
char ch;

bool eval(),termen(),factor();

bool eval(){
	bool r=termen(); cout << "in_e" << p << endl;
	while ((s[p]=='O')&&(s[p+1]=='R')&&(p<(int)s.size())) p+=2,r|=termen();	
	return r;
}

bool termen(){
	bool r=factor(); cout << "in_t" << p << endl;
	while ((s[p]=='A')&&(s[p+1]=='N')) p+=3,r&=factor();
	return r;
}

bool factor(){
	bool r=0; cout << "in_f" << p << endl;
	if ((s[p]=='N')&&(s[p+1]=='O')) p+=3,r=!factor(); else
	if ((s[p]=='T')&&(s[p+1]=='R')) p+=4,r=1; else
	if ((s[p]=='F')&&(s[p+1]=='A')) p+=4,r=0; else
	if (s[p]=='(') ++p,r=eval(),++p; else r=a[s[p]-'A'],++p;
	return r;
}


int main(){
	// A-65 Z-90
	getline(fi,s);
	for (i=0; i<(int)s.length(); i++) if (s[i]==' ') s.erase(s.begin()+i);
	fi >> n;
	while (n--){
		fi >> ch;
		a[ch-'A']=!a[ch-'A'];
		p=0;
		fo << eval();
	}
	
	return 0;
}