Pagini recente » Cod sursa (job #1607195) | Cod sursa (job #452681) | Cod sursa (job #3232496) | Cod sursa (job #2659242) | Cod sursa (job #1529562)
#include <cstdio>
#include <stack>
#include <cctype>
using namespace std;
FILE *f = fopen ( "evaluare.in" , "r" ) , *g = fopen ( "evaluare.out" , "w" );
struct levelType
{
int argL , argR;
char operation;
};
const levelType empty = { 0 , 0 , NULL };
stack <levelType> equation;
levelType level;
char c , op , overLap;
int y , num;
bool isDigit ( char c ) { return isdigit ( c ); }
bool isOperator ( char c ) { return (c == '-' || c == '+' || c == '*' || c == '/'); }
bool isBracket ( char c ) { return ( c == '(' || c == ')' ); }
int priority ( char c )
{
switch ( c )
{
case '(' : return 0;
case ')' : return 0;
case '-' : return 2;
case '+' : return 1;
case '*' : return 3;
case '/' : return 4;
}
}
int execute ( int x , char operation , int y )
{
switch ( operation )
{
case '+' : return x + y;
case '-' : return x - y;
case '*' : return x * y;
case '/' : return x / y;
}
}
void solveTopLayers( char c )
{
level = equation . top() , equation . pop() , y = level . argL;
if ( level . operation == '-' )
y = y * ( -1 );
if ( !equation . empty() )
level = equation . top();
while ( priority ( level . operation ) > priority ( c ) && !equation . empty() )
{
level . argR = execute ( level . argL , level . operation , y );
y = level . argR;
equation . pop();
if ( !equation . empty() )
level = equation . top();
}
level = empty , level .argL = y;
equation . push ( level );
}
void solveBrackets()
{
level = equation . top() , equation . pop() , y = level . argL ;
if ( level . operation == '-' )
y = y * ( -1 );
level = equation . top();
while ( level . operation != '(' )
{
level . argR = execute ( level . argL , level . operation , y );
y = level . argR;
equation . pop();
if ( !equation . empty() )
level = equation . top();
}
equation . top () . argL = y;
}
int getNumber ( char c )
{
num = c - '0';
while ( !feof(f) && !overLap )
{
//read a character
fscanf ( f , "%c" , &c );
if ( isDigit ( c ) )
num = num * 10 + ( c - '0' );
else
overLap = c;
}
return num;
}
void addLeftArg ( char c )
{
num = getNumber ( c );
level = empty;
level . argL = num;
equation . push ( level );
}
char getOperation()
{
if ( !equation . empty() )
{
level = equation . top() , equation . pop () , op = level . operation;
if ( !equation . empty() && op )
op = equation . top() . operation;
equation . push ( level );
if ( op )
equation . push ( empty );
return op;
}
else
{
equation . push ( empty );
return '(';
}
}
void addOperator ( char c )
{
overLap = NULL;
if ( priority ( c ) < priority ( getOperation() ) )
solveTopLayers ( c );
equation . top() . operation = c; // set the new operator
}
void addBracket ( char C )
{
overLap = NULL;
if ( c == '(' )
{
level = empty;
level . operation = c;
equation . push ( level );
}
else
solveBrackets();
}
void processCharacter ( char c )
{
if ( isDigit ( c ) ) //digit
addLeftArg ( c ); //we are on a new level in the stack
if ( isOperator ( c ) ) //operator
addOperator ( c ); //set the current level's operator to c and create a new level
if ( isBracket ( c ) ) //bracket
addBracket ( c ); //create a new level with operator = c
}
void read()
{
while ( !feof(f) && c != '\n' )
{
//read a character
if ( !overLap )
fscanf ( f , "%c" , &c );
else
c = overLap;
processCharacter ( c );
}
solveTopLayers ( '(' );
}
int main()
{
read();
//print the solution
fprintf ( g , "%d\n" , equation . top() . argL );
return 0;
}