Cod sursa(job #459828)
#include <string>
#include <cstdlib>
#include <fstream>
/*
*
*/
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; break;
case '-' : return a-b; break;
case '*' : return a*b; break;
case '/' : return a/b; break;
default : exit(1);
}
}
inline int eval( int level )
{
int x;
if( 2 == level )
{
if( '(' == *it )
{
++it;
x=eval( 0 );
++it;
}
else for( x=0; it < iend && *it >= '0' && *it <= '9'; ++it )
x=x*10+*it-'0';
}
else for( x=eval( level+1 ); string::npos != op[level].find( *it ); x=eval( x, eval( level+1 ), *it++ ) );
return x;
}
int main( void )
{
ifstream in( "evaluare.in" );
getline( in, expresion, '\n' );
it=expresion.begin(); iend=expresion.end();
ofstream out( "evaluare.out" );
out<<eval(0)<<'\n';
return EXIT_SUCCESS;
}