Pagini recente » Cod sursa (job #1415235) | Cod sursa (job #652137) | Cod sursa (job #1352948) | Cod sursa (job #2092177) | Cod sursa (job #1314169)
#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;
}