Cod sursa(job #2990990)

Utilizator xDemonstyMatei Haba Ionut xDemonsty Data 8 martie 2023 20:54:13
Problema Castel Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.75 kb
#include <fstream>
#include <stack>
#include <cstring>
using namespace std;
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");


stack<pair<long long,string>> s ;
char a [ 1000005 ] ;
int main()
{
    cin >> a ;
    long long l = strlen(a) ;

    long long val = 0;

    for ( int i = 0 ; i < l ; i ++ )
    {
        if ( isdigit( a [ i ] ) == true )
        {
            val = val * 10 + a [ i ] - '0';
        }
        else if ( a [ i ] == '+' || a[ i ] == '-')
        {

            if ( !s.empty() && s.top().second != "p")
            {

                if ( s.top().second == "-")
                    val = s.top().first - val ;
                else if ( s.top().second == "+")
                    val = s.top().first + val ;
                else if ( s.top().second == "*" )
                    val = s.top().first * val ;

                s.pop();

            }

            if ( !s.empty() && s.top().second == "p")
                s.pop();

            string x ;
            x += a [i ] ;
            s.push({val, x });

            val = 0 ;

        }
        else if (  a[ i ] == '*' || a[ i ] == '/')
        {
            if ( !s.empty() &&  s.top().second == "*")
            {
                val = val * s.top().first;
                s.pop();

            }
            else
                if ( !s.empty() && s.top().second == "/")
            {
                val = s.top().first / val ;
                s.pop();
            }

            if ( !s.empty() && s.top().second == "p")
                s.pop();

            string x ;
            x += a[ i ];
            s.push({val, x });

            val = 0 ;

        }
        else if ( a[ i ] == '(')
        {
            s.push( { -22, "p"});
        }
        else if ( a[ i ] == ')')
        {
            if ( s.top().second == "+")
                val += s.top().first;
            else if ( s.top().second == "-")
                val = s.top().first - val ;
            else
                if ( s.top().second == "*")
                val = val * s.top().first;
                else
                    if ( s.top().second =="/")
                    val = s.top().first / val ;

            if(!s.empty())
            s.pop();
        }
    }

    while ( !s.empty())
    {
        if ( s.top().second == "+")
            val += s.top().first;
        else if ( s.top().second == "-")
            val = s.top().first - val ;
        else if ( s.top().second == "*")
            val = s.top().first * val;
            else
                if ( s.top().second == "/")
            {
                val = s.top().first / val ;
            }

        s.pop();
    }

    cout << val ;
    return 0;
}