Cod sursa(job #1905541)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 6 martie 2017 09:07:29
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.25 kb
#include <bits/stdc++.h>
#define paran  1000000001       /// (
#define minu 1000000002         /// -
#define impartire 1000000003    /// /
#define inmultire 1000000004    /// *

using namespace std;

ifstream fin ("evaluare.in");
ofstream fout ("evaluare.out");

char a[100007];
int st[100007], t;

void Rezolvare()
{
    int i, x;
    for (i=0; a[i]!=0; )
    {
        if (a[i]=='+')
            i++;

        if (a[i]=='(')
        {
            st[++t]=paran;
            i++;
        }

        if (a[i]=='-')
        {
            st[++t]=minu;
            i++;
        }

        if (a[i]=='/')
        {
            st[++t]=impartire;
            i++;
        }

        if (a[i]=='*')
        {
            st[++t]=inmultire;
            i++;
        }

        if (a[i]>='0' && a[i]<='9')
        {
            x=0;
            while (a[i]>='0' && a[i]<='9')
            {
                x=x*10+a[i]-'0';
                i++;
            }

            if (st[t]==minu)
            {
                x*=(-1);
                t--;
            }

            while (st[t]==inmultire && t>0)
            {
                t--;
                x=x*st[t];
                t--;
            }

            while (st[t]==impartire && t>0)
            {
                t--;
                x=st[t]/x;
                t--;
            }

            st[++t]=x;
        }

        if (a[i]==')')
        {
            x=0;
            while (st[t]!=paran && t>0)
            {
                x+=st[t];
                t--;
            }
            t--;

            while (st[t]==inmultire && t>0)
            {
                t--;
                x=x*st[t];
                t--;
            }

            while (st[t]==impartire && t>0)
            {
                t--;
                x=st[t]/x;
                t--;
            }

            if (st[t]==minu)
            {
                x*=(-1);
                t--;
            }

            st[++t]=x;
            i++;
        }
    }
    x=0;
    for (i=1; i<=t; i++)
        x+=st[i];
    fout << x << "\n";
}

int main()
{
    fin >> a;
    Rezolvare();
    fin.close();
    fout.close();
    return 0;
}