Cod sursa(job #543731)

Utilizator BitOneSAlexandru BitOne Data 28 februarie 2011 15:39:45
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <string>
#include <fstream>
#include <cstdlib>
#define oo 1<<20

using namespace std;
string expresion;
string::const_iterator it, iend;
const string op[]={ "+-", "*/", "^" };
inline int eval( int a, int b, char op )
{
	switch(op)
	{
		case '+' : return a+b;
		case '-' : return a-b;
		case '*' : return a*b;
		case '/' : return a/b;
	}
	//return -oo;
}
inline int eval( int lev )
{
	int x;
	if( 2 == lev )
	{
		if( '(' == *it )
		{
			++it;
			x=eval( 0 );
			++it;
		}
		else for( x=0; it < iend && *it >= '0' && *it <= '9'; x=x*10+*it-'0', ++it );
	}
	else for( x=eval(lev+1); it < iend && string::npos != op[lev].find(*it); )
		 {
			 char op=*it;
			 ++it;
			 x=eval( x, eval(lev+1), op );
		 }
	return x;
}
int main( void )
{
	ifstream in( "evaluare.in" );
	in>>expresion;
	it=expresion.begin();
	iend=expresion.end();
	ofstream out( "evaluare.out" );
	out<<eval(0)<<'\n';
	return EXIT_SUCCESS;
}