Cod sursa(job #566550)

Utilizator nautilusCohal Alexandru nautilus Data 29 martie 2011 09:46:04
Problema Bool Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include<fstream>
#define dmax 1010
using namespace std;

char s[dmax];
int n,poz;
bool v[dmax];

int grad1();
int grad2();
int grad3();
int valoare();


int grad1() /*OR*/
{
 int rez;
 
 rez = grad2();
 while (s[poz] == 'O')
	 {
	  poz += 3;
	  rez = rez | grad2();
	 }
 
 return rez;
}


int grad2() /*AND*/
{
 int rez;
 
 rez = grad3();
 while (s[poz] == 'A')
	 {
	  poz += 4;
	  rez = rez & grad3();
	 }
 
 return rez;
}


int grad3() /*NOT*/
{
 int rez;
 
 if (s[poz] == 'N' && s[poz+1] == 'O')
	 {
	  poz += 4;
	  rez = !(valoare());
	 } else 
	 {
	  rez = valoare();
	 }
 
 return rez;
}


int valoare() /*variabila, TRUE, FALSE sau paranteze*/
{
 int rez;
 
 if (s[poz] >= 'A' && s[poz] <= 'Z' && (s[poz+1] <= 'A' || s[poz+1] >= 'Z'))
	 {
	  rez = v[s[poz] - 'A'];
	  poz += 2;
	 } else
	 if (s[poz] == 'T' && s[poz+1] == 'R')
		 {
		  rez = 1;
		  poz += 5;
		 } else
		 if (s[poz] == 'F' && s[poz+1] == 'A')
			 {
			  rez = 0;
			  poz += 5;
			 } else
			 {
			  if (s[poz] == '(')
				  poz++;
			  rez = grad1();
			  if (s[poz-1] == ')')
				  poz++;
			 }

 return rez;
}


void citire()
{
 int i;
 char c;
 
 ifstream fin("bool.in");
 ofstream fout("bool.out");
 
 fin.get(s,dmax);
 fin>>n;
 for (i=1; i<=n; i++)
	 {
	  fin>>c; poz = 0;
	  v[c - 'A'] = (v[c - 'A'] + 1) % 2;
	  fout<<grad1();
	 }
 
 fin.close();
 fout.close();
}


int main()
{
	
 citire();
	
 return 0;
}