Pagini recente » Monitorul de evaluare | Cod sursa (job #388924) | Cod sursa (job #1186979) | Cod sursa (job #2111491) | Cod sursa (job #434595)
Cod sursa(job #434595)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 6, 2010, 9:09 AM
*/
#include <string>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#define ERROR_SIGNAL -1000000000
/*
*
*/
using namespace std;
string::const_iterator it, iend;
const string op[3]={ "+-", "*/", "" };
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 >= '0' && *it <= '9'; ++it )
x=x*10+*it-'0';
}
else for( x=eval( level+1 ); op[level].end() != find( op[level].begin(), op[level].end(), *it ); x=eval( x, eval( level+1 ), *it++ ) );
return x;
}
int main(int argc, char** argv)
{
string expresion;
ifstream in( "evaluare.in" );
getline( in, expresion );
it=expresion.begin(), iend=expresion.end();
ofstream out( "evaluare.out" );
out<<eval(0)<<'\n';
return EXIT_SUCCESS;
}