Cod sursa(job #2048553)

Utilizator robx12lnLinca Robert robx12ln Data 26 octombrie 2017 11:20:09
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.31 kb
#include<fstream>
#include<cstring>
#include<stack>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
int P[130], n;
char s[100005];
stack<char> stiva;
stack< pair<int,int> > v;
int main(){
    P[(int)('+')] = P[(int)('-')] = 1;
    P[(int)('*')] = P[(int)('/')] = 2;
    fin >> s;
    n = strlen(s);
    for( int i = 0; i < n; i++ ){
        if( s[i] == '(' ){
            stiva.push( s[i] );
            continue;
        }
        if( s[i] == ')' ){
            while( stiva.top() != '(' ){
                v.push( make_pair( 0, (int)( stiva.top() ) ) );
                stiva.pop();
            }
            stiva.pop();
            continue;
        }
        if( s[i] == '+' ){
            if( i == 0 || s[i - 1] == '(' )
                v.push( make_pair( 1, 0 ) );
            while( !stiva.empty() && P[(int)(stiva.top())] >= P[(int)(s[i])] ){
                v.push( make_pair( 0, (int)( stiva.top() ) ) );
                stiva.pop();
            }
            stiva.push( s[i] );
            continue;
        }
        if( s[i] == '-' ){
            if( i == 0 || s[i - 1] == '(' )
                v.push( make_pair( 1, 0 ) );
            while( !stiva.empty() && P[(int)(stiva.top())] >= P[(int)(s[i])] ){
                v.push( make_pair( 0, (int)( stiva.top() ) ) );
                stiva.pop();
            }
            stiva.push( s[i] );
            continue;
        }
        if( s[i] == '*' ){
            while( !stiva.empty() && P[(int)(stiva.top())] >= P[(int)(s[i])] ){
                v.push( make_pair( 0, (int)( stiva.top() ) ) );
                stiva.pop();
            }
            stiva.push( s[i] );
            continue;
        }
        if( s[i] == '/' ){
            while( !stiva.empty() && P[(int)(stiva.top())] >= P[(int)(s[i])] ){
                v.push( make_pair( 0, (int)( stiva.top() ) ) );
                stiva.pop();
            }
            stiva.push( s[i] );
            continue;
        }
        int nr = 0;
        while( s[i] >= '0' && s[i] <= '9' ){
            nr = nr * 10 + ( s[i] - '0' );
            i++;
        }
        v.push( make_pair( 1, nr ) );
        i--;
    }
    for( int i = 0; i < v.size(); i++ )
        if( v[i].first == 0 )
            fout << (int)()
    return 0;
}