Cod sursa(job #434595)

Utilizator alexandru92alexandru alexandru92 Data 6 aprilie 2010 09:18:11
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
/* 
 * 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;
}