Mai intai trebuie sa te autentifici.
Cod sursa(job #1458858)
Utilizator | Data | 8 iulie 2015 16:39:43 | |
---|---|---|---|
Problema | Evaluarea unei expresii | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 4.67 kb |
// Se aduce expresia la forma poloneza ( postfixata )
// Se evalueaza noua expresie
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
ifstream fin ("evaluare.in") ;
ofstream fout ("evaluare.out") ;
char coada [100000001] ; // aici se afla forma postfixata
int top_s , lg_c ;
char expresie [100000001] ; // aici se afla forma infixata ( normala )
bool isplusminus ( char op )
{
if ( strchr ( "+-" , op ) != 0 )
return true ;
return false ;
}
bool istimesdivide ( char op )
{
if ( strchr ( "*/" , op ) != 0 )
return true ;
return false ;
}
struct op
{
char symb ;
int priority ;
} ;
op stiva [100001] ; // stiva de operatori si paranteze ( parantezele nu sunt operatori )
void Citire ()
{
fin.getline ( expresie , 100001 ) ;
}
void ConvertToPostfix ( char *sir ) // calculez forma postfixata
{
while ( *sir != '\0' )
{
if ( isplusminus ( *sir ) )
{
if ( top_s > 0 )
while ( stiva [ top_s ]. priority >= 1 ) // verific ca nu cumva in varful stivei sa fie un operator cu prioritate mai mare
coada [ lg_c ] = stiva [ top_s ].symb , -- top_s , ++ lg_c , coada [ lg_c ] = ',' , ++ lg_c ;
++ top_s , stiva [ top_s ].symb = *sir , stiva [ top_s ].priority = 1 , strcpy ( sir , sir + 1 ) ;
}
else
if ( istimesdivide ( *sir ) )
{
if ( top_s > 0 )
while ( stiva [ top_s ]. priority >= 10 ) // verific ca nu cumva in varful stivei sa fie un operator cu prioritate mai mare
coada [ lg_c ] = stiva [ top_s ].symb , -- top_s , ++ lg_c , coada [ lg_c ] = ',' , ++ lg_c ;
++ top_s , stiva [ top_s ].symb = *sir , stiva [ top_s ].priority = 10 , strcpy ( sir , sir + 1 ) ;
}
else
if ( *sir == '(' ) // nu e considerata operator .. ( ii pun prioritate 0 )
++ top_s , stiva [ top_s ].symb = *sir , stiva [ top_s ].priority = 0 , strcpy ( sir , sir + 1 ) ;
else
if ( *sir == ')' )
{
while ( stiva [ top_s ].symb != '(' )
coada [ lg_c ] = stiva [ top_s ].symb , -- top_s , ++ lg_c , coada [ lg_c ] = ',' , ++ lg_c ;
-- top_s ; // scot paranteza '('
strcpy ( sir , sir + 1 ) ;
}
else // este numar
{
while ( *sir >= '0' && *sir <= '9' )
coada [ lg_c ] = *sir , ++ lg_c , strcpy ( sir , sir + 1 ) ;
coada [ lg_c ] = ',' , ++ lg_c ;
}
}
while ( top_s > 0 ) // daca au mai ramas operatori pe stiva ii scot si ii adaug la coada ( la forma postfixata )
coada [ lg_c ] = stiva [ top_s ].symb , -- top_s , ++ lg_c , coada [ lg_c ] = ',' , ++ lg_c ;
coada [ lg_c ] = 0 ;
}
int stv [100001] , top ; // aici pun operanzii
// Cand un operator este intalnit scot de pe stiva ultimii 2 operanzi , aplic operatorul intre ei si rezultatul este pus in varful stivei
long long EvaluareExpresie ( char sir [100000001] ) // Functie ce se aplica pe forma postfixata
{
int aux1 , aux2 ;
while ( *sir != '\0' )
{
if ( isplusminus ( *sir ) )
{
aux1 = stv [ top ] , -- top , aux2 = stv [top] ;
if ( *sir == '+' )
stv [ top ] = ( aux1 += aux2 ) ;
else
stv [ top ] = ( aux2 -= aux1 ) ;
strcpy ( sir , sir + 2 ) ; // sterg si operatorul comma
}
else
if ( istimesdivide ( *sir ) )
{
aux1 = stv [ top ] , -- top , aux2 = stv [top] ;
if ( *sir == '*' )
stv [ top ] = ( aux1 *= aux2 ) ;
else
stv [ top ] = ( aux2 /= aux1 ) ;
strcpy ( sir , sir + 2 ) ;
}
else // e numar
{
++ top , stv [ top ] = atoi ( sir ) ;
while ( *sir != ',' )
strcpy ( sir , sir + 1 ) ;
strcpy ( sir , sir + 1) ; // la fel , sterg si comma
}
}
return stv [ top ] ;
}
int main()
{
Citire () ;
ConvertToPostfix ( expresie ) ;
fout << EvaluareExpresie ( coada ) ;
return 0;
}