Pagini recente » Cod sursa (job #9475) | Cod sursa (job #3290747) | Cod sursa (job #14852) | Cod sursa (job #2172117) | Cod sursa (job #543731)
Cod sursa(job #543731)
#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;
}