Cod sursa(job #1458800)

Utilizator petru.cehanCehan Petru petru.cehan Data 8 iulie 2015 14:48:17
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 5.29 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 [100001] ; // aici se afla forma postfixata

int top_s , lg_c ;

char expresie [100001] ; // 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 ) ;
}

#define INT_DIGITS 19		/* enough for 64 bit integer */

char * itoa( int i )
{
static char buf [INT_DIGITS + 2] ;
char *p = buf + INT_DIGITS + 1;	// pointeaza la NULL din final
if ( i >= 0 )
{
    do
    {
      *--p = '0' + (i % 10);
      i /= 10;
    }
    while (i != 0);

    return p;
}

else // i < 0
{
    do
    {
      *--p = '0' - (i % 10);
      i /= 10;
    }
    while (i != 0);

    *--p = '-';
    return p ;

}
}

void ConvertToPostfix ( char sir [100001] ) // calculez forma postfixata
{
    int i = 0  , nr ;
    char nr_char [ 33 ] ; // folosit pentru itoa ( a trece numarul din int in char )
    int lg = strlen ( sir ) ;

    while ( lg > 0 )
      {
          if ( isplusminus ( sir[i] ) )
             {
                  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[i] , stiva [ top_s ].priority = 1 , strcpy ( sir , sir + 1 ) , -- lg ;
             }
          else
              if ( istimesdivide ( sir[i] ) )
                      ++ top_s , stiva [ top_s ].symb = sir[i] , stiva [ top_s ].priority = 10 , strcpy ( sir , sir + 1 ) , -- lg ;

            else
              if ( sir [i] == '(' ) // nu e considerata operator .. ( ii pun prioritate 0 )
                 ++ top_s , stiva [ top_s ].symb = sir[i] , stiva [ top_s ].priority = 0 , strcpy ( sir , sir + 1 ) , -- lg ;

              else
                if ( sir [i] == ')' )
                     {
                         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 ) , -- lg ;
                     }
                else // este numar
                  {
                      nr = atoi ( sir ) ;
                      itoa ( nr , nr_char , 10 ) ;
                      strcpy ( sir , sir + strlen ( nr_char ) ) , lg -= strlen ( nr_char )  ;
                      for ( unsigned int i = 0 ; i < strlen (nr_char) ; ++ i )
                                  coada [ lg_c ] = nr_char [i] , ++ lg_c ;
                      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

int EvaluareExpresie ( char sir [100001] )  // Functie ce se aplica pe forma postfixata
{
    int lg = strlen (sir) ;
    int nr , aux1 , aux2 ;

    char nr_char [33] ; // buffer pt itoa

    int i = 0 ;
    while ( lg > 0 )
    {
         if ( isplusminus ( sir[i] ) )
             {
                 aux1 = stv [ top ] , -- top , aux2 = stv [top] ;
                 if ( sir [i] == '+' )
                        stv [ top ] = ( aux1 += aux2 ) ;
                 else
                        stv [ top ] = ( aux2 -= aux1 ) ;

                 strcpy ( sir , sir + 2 ) , lg -= 2 ;
             }

         else
           if ( istimesdivide ( sir[i] ) )
             {
                 aux1 = stv [ top ] , -- top , aux2 = stv [top] ;
                 if ( sir [i] == '*' )
                        stv [ top ] = ( aux1 *= aux2 ) ;
                 else
                        stv [ top ] = ( aux2 /= aux1 ) ;

                 strcpy ( sir , sir + 2 ) , lg -= 2 ; // sterg si operatorul comma
             }

         else // e numar
         {
             nr = atoi ( sir ) ;
             itoa ( nr , nr_char , 10 ) ;
             strcpy ( sir , sir + strlen ( nr_char ) + 1 ) , lg -= strlen ( nr_char ) , -- lg  ;  // la fel sterg op comma ,
             ++ top , stv [ top ] = nr ;
         }
     }

return stv [ top ] ;

}

int main()
{
    Citire () ;
    ConvertToPostfix ( expresie ) ;
    fout << EvaluareExpresie (coada) ;
    return 0;
}