Cod sursa(job #446329)

Utilizator alexandru92alexandru alexandru92 Data 25 aprilie 2010 18:00:26
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
/* 
 * 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);
}