Cod sursa(job #1529510)

Utilizator jimcarterJim Carter jimcarter Data 20 noiembrie 2015 23:08:11
Problema Evaluarea unei expresii Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 3.45 kb
#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 ( !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 , level = equation . top();

	while ( level . operation != '(' )
	{
		level . argR = execute ( level . argL , level . operation , y );
		y = level . argR;
		equation . pop();
		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()
{
    level = equation . top() , equation . pop () , op = '(';
    if ( !equation . empty() )
        op = equation . top() . operation;
    equation . push ( level );
    return op;
}

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;
}