Cod sursa(job #301997)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 8 aprilie 2009 16:27:37
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <cstdio>
#define cmp(x1,x2,y) ((y == x1 || y == x2) ? 1:0)

char A[1<<17],*p=A;
const char semn[]={'+','-','*','/'};

int oper(int x,int y,char c)
{
	switch(c)
	{
		case '+' : return x+y;
		case '-' : return x-y;
		case '*' : return x*y;
		case '/' : return x/y;
	}
	return 0;
}

int eval(int h)
{
	int r = 0;
	
	if(h == 2)
	{
		for(;*p >= '0' && *p <= '9';++p)
			r = r * 10 + *p - '0';
		if(*(p-1) < '0' || *(p-1) > '9')
			++p,r = eval(0),++p;
		return r;
	}
	
	for(r = eval(h+1);cmp(semn[h<<1],semn[(h<<1)+1],*p);)
	{
		char s = *p++;
		r = oper(r,eval(h+1),s);
	}
	return r;
}

int main()
{
	freopen("evaluare.in","r",stdin);
	freopen("evaluare.out","w",stdout);
	
	fgets(A,1<<17,stdin);
	printf("%d\n",eval(0) );
	return 0;
}