Cod sursa(job #151961)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 8 martie 2008 20:25:29
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <stdio.h>

int poz;
char s[100000];

int fact();
int eval();
int term();

int fact()
{
	int aux = 0;

	if (s[poz] == '(')
	{
		poz++;
		aux = eval();
		poz++;
		return aux;
	}
	while (s[poz] >= '0' && s[poz] <= '9')
	{
		aux = aux * 10 + s[poz] - '0'; 
		poz++;
	}
	return aux;
}

int eval()
{
	int aux = term();
	while (s[poz] == '+' || s[poz] == '-')
		if (s[poz] == '+') { poz++; aux += term();}
		else { poz++; aux -= term();}
	return aux;
}

int term()
{
	int aux = fact();
	while (s[poz] == '*' || s[poz] == '/')
		if (s[poz] == '*') { poz++; aux *= fact();}
		else { poz++; aux /= fact();}
	return aux;
}

int main()
{
	freopen("evaluare.in","r",stdin);
	freopen("evaluare.out","w",stdout);
	scanf("%s",s);
	printf("%d\n",eval());
	return 0;
}