Cod sursa(job #80246)

Utilizator rss1987Marin Radu rss1987 Data 27 august 2007 00:42:45
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.99 kb
#include <stdio.h>
#include <string.h>
#define EXP_LEN 1001
#define MAXN 101

char compiled_exp[EXP_LEN],input_exp[EXP_LEN];
char stiva[EXP_LEN];
int inpLen,compLen,stackLevel;
char switchOps[MAXN];
int rezVals[MAXN];
int val[256];
int N;

void citesteDate()
{
	do
	{
		scanf("%c",&input_exp[inpLen++]);
	}
	while(input_exp[inpLen-1]>14);
	inpLen--;
	scanf("%d\n",&N);
	int i;
	scanf("%s",switchOps);
}

void compile()
{
	stackLevel = 0;
	compLen = 0;
	int i;
	for(i=0;i<inpLen;i++)
		switch(input_exp[i])
	{
		case ' ':
			break;
		case '(':
			stiva[stackLevel++] = '(';
			break;
		case ')':
			while(stiva[stackLevel-1] !='(')
			{
				compiled_exp[compLen++] = stiva[--stackLevel];
			}
			stackLevel--;
			break;
		case 'A':
			if (strncmp(&input_exp[i],"AND",3)==0)
			{
			if (stackLevel)
				while(stackLevel && stiva[stackLevel-1] == '!')
					compiled_exp[compLen++] = stiva[--stackLevel];
			stiva[stackLevel++] = '&';
			i+=2;
			}
			else
				compiled_exp[compLen++] = input_exp[i];
			break;
		case 'F':
			if (strncmp(&input_exp[i],"FALSE",5)==0)
			{
				compiled_exp[compLen++] = '0';
				i+=4;
			}
			else
				compiled_exp[compLen++] = input_exp[i];
			break;
		case 'N':
			if (strncmp(&input_exp[i],"NOT",3)==0)
			{
				stiva[stackLevel++] = '!';
				i+=2;
			}
			else
				compiled_exp[compLen++] = input_exp[i];
			break;
		case 'O':
			if (strncmp(&input_exp[i],"OR",2)==0)
			{
				if (stackLevel)
					while(stackLevel && (stiva[stackLevel-1] == '&' || stiva[stackLevel-1] =='!'))
						compiled_exp[compLen++] = stiva[--stackLevel];
				stiva[stackLevel++] = '|';
				i++;
			}
			else
				compiled_exp[compLen++] = input_exp[i];
			break;
		case 'T':
			if (strncmp(&input_exp[i],"TRUE",4)==0)
			{
				compiled_exp[compLen++] = '1';
				i+=3;
			}
			else
				compiled_exp[compLen++] = input_exp[i];
			break;
		default:
			compiled_exp[compLen++] = input_exp[i];
			break;
	}
	while(stackLevel)
		compiled_exp[compLen++] = stiva[--stackLevel];
}


int evaluate()
{
	stackLevel = 0;
	int i,a,b;
	for(i=0;i<compLen;i++)
		switch (compiled_exp[i])
	{
		case '&':
			a = val[stiva[--stackLevel]];
			b = val[stiva[--stackLevel]];
			stiva[stackLevel++] = (a&&b) + '0';
			break;
		case '!':
			a = val[stiva[--stackLevel]];
			stiva[stackLevel++] = (!a) + '0';
			break;
		case '|':
			a = val[stiva[--stackLevel]];
			b = val[stiva[--stackLevel]];
			stiva[stackLevel++] = (a||b) + '0';
			break;
		default:
			stiva[stackLevel++] = compiled_exp[i];
			break;
	}
	return val[stiva[0]];
}


void proceseaza()
{
	compile();
	val['0'] = 0;
	val['1'] = 1;
	int i;
	for(i=0;i<N;i++)
	{
		val[switchOps[i]] = 1 - val[switchOps[i]];
		rezVals[i] = evaluate();
	}
}

void afiseazaRez()
{
	int i;
	for(i=0;i<N;i++)
		printf("%d",rezVals[i]);
}

int main()
{
 freopen("bool.in","r",stdin);
 freopen("bool.out","w",stdout);
 citesteDate();
 proceseaza();
 afiseazaRez();
 return 0;
}