Pagini recente » Cod sursa (job #1244938) | Cod sursa (job #604409) | Cod sursa (job #2933057) | Cod sursa (job #2859747) | Cod sursa (job #446329)
Cod sursa(job #446329)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on April 25, 2010, 4:12 PM
*/
#include <string>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#define ERROR_SIGNAL -1000000000
/*
*
*/
using namespace std;
string expresion;
string::const_iterator it, iend;
const unsigned int stop=4294967295;
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 ERROR_SIGNAL;
}
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 ); stop != op[level].find(*it); x=eval( x, eval( level+1 ), *it++ ) );
return x;
}
int main(int argc, char** argv)
{
ifstream in( "evaluare.in" );
ofstream out( "evaluare.out" );
getline( in, expresion );
it=expresion.begin();
iend=expresion.end();
out<<eval(0)<<'\n';
return (EXIT_SUCCESS);
}