Cod sursa(job #1314169)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 11 ianuarie 2015 16:44:07
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <cstdio>
#include <cctype>
#include <cstring>

using namespace std;

#define L_MAX 100005

char expr[ L_MAX ], *pos;
int solve( );
int number( ) {
    int x = 0;
    if( *pos == '(' ) {
        ++pos;
        x = solve( );
        ++pos;
    } else {
        while( isdigit( *pos ) ) {
            x = ( x << 1 ) + ( x << 3 ) + ( *pos - '0' );
            ++pos;
        }
    }
    return x;
}
int next( ) {
    int x;
    x = number( );
    while( *pos == '*' || *pos == '/' ) {
        ++pos;
        if( *( pos - 1 ) == '*' )
            x *= number( );
        else
            x /= number( );
    }
    return x;
}
int solve(  ) {
    int x;
    x = next( );
    while( *pos == '+' || *pos == '-' ) {
        ++pos;
        if( *( pos - 1 ) == '+' )
            x += next( );
        else
            x -= next( );
    }
    return x;
}
int main() {
    freopen( "evaluare.in", "r", stdin );
    freopen( "evaluare.out", "w", stdout );
    gets( expr );
    fclose( stdin );
    pos = expr;
    printf( "%d\n", solve( ) );
    fclose( stdout );
    return 0;
}