Cod sursa(job #1657278)

Utilizator NicuBaciuNicu Baciu NicuBaciu Data 20 martie 2016 12:47:26
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 9.92 kb
#include <fstream>
#include <string>
#include <stack>
#include <stdio.h>

using namespace std;

ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
//FILE* f(fopen("expresie.out", "w"));


stack <char> stiva;

string x;
stack <int> polo;
int j=1;

void poloneza(string &x)
{
    int nrcrt=0;
    int o=0;
    bool paranteza=false;
    bool negativ=false;
    bool nu=false;
    bool nup=false;

    for(int i=0; i<x.size(); i++)
    {
        if(x[i]>='0' && x[i]<='9')
        {
            nrcrt=nrcrt*10+x[i]-'0';
        }
        else if(x[i]=='+')
        {
            if(x[i-1]!=')')
            {
                polo.push(nrcrt);
                nrcrt=0;
            }
            if(!stiva.empty())
            {
                bool sem=true;
                while(!stiva.empty() && sem==true)
                {
                    if(stiva.top()=='*')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(a*b);

                        stiva.pop();
                    }
                    else if(stiva.top()=='/')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(b/a);
                        stiva.pop();
                    }
                    else if(stiva.top()=='-')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(b-a);
                        stiva.pop();
                    }
                    else if(stiva.top()=='+')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(a+b);
                        stiva.pop();
                    }
                    else
                    {
                        stiva.push(x[i]);
                        sem=false;
                    }
                }
                if(sem==true)
                    stiva.push(x[i]);
            }
            else
                stiva.push(x[i]);
        }
        else if(x[i]=='-')
        {
            if(x[i-1]!=')')
            {
                polo.push(nrcrt);
                nrcrt=0;
            }
            if(!stiva.empty())
            {
                bool sem=true;
                while(!stiva.empty() && sem==true)
                {
                    if(stiva.top()=='*')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(a*b);

                        stiva.pop();
                    }
                    else if(stiva.top()=='/')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(b/a);
                        stiva.pop();
                    }
                    else if(stiva.top()=='-')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(b-a);
                        stiva.pop();
                    }
                    else if(stiva.top()=='+')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(a+b);
                        stiva.pop();
                    }
                    else
                    {
                        stiva.push(x[i]);
                        sem=false;
                    }
                }
                if(sem==true)
                    stiva.push(x[i]);
            }
            else
                stiva.push(x[i]);
        }
        else if(x[i]=='*')
        {
            if(x[i-1]!=')')
            {
                polo.push(nrcrt);
                nrcrt=0;
            }
            if(!stiva.empty())
            {
                bool sem=true;
                while(!stiva.empty() && sem==true)
                {
                    if(stiva.top()=='*')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(a*b);
                        stiva.pop();
                    }
                    else if(stiva.top()=='/')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(b/a);
                        stiva.pop();
                    }
                    else
                    {
                        stiva.push(x[i]);
                        sem=false;
                    }
                }
                if(sem==true)
                    stiva.push(x[i]);
            }
            else
                stiva.push(x[i]);
        }
        else if(x[i]=='/')
        {
            if(x[i-1]!=')')
            {
                polo.push(nrcrt);
                nrcrt=0;
            }
            if(!stiva.empty())
            {
                bool sem=true;
                while(!stiva.empty() && sem==true)
                {
                    if(stiva.top()=='/')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(b/a);
                        stiva.pop();
                    }
                    else if(stiva.top()=='*')
                    {
                        int a, b;

                        a=polo.top();
                        polo.pop();

                        b=polo.top();
                        polo.pop();

                        polo.push(a*b);
                        stiva.pop();
                    }
                    else
                    {
                        stiva.push(x[i]);
                        sem=false;
                    }
                }
                if(sem==true)
                    stiva.push(x[i]);
            }
            else
                stiva.push(x[i]);
        }

        else if(x[i]=='(')
        {
            stiva.push('(');
        }
        else if(x[i]==')')
        {
            if(x[i-1]!=')')
            {
                polo.push(nrcrt);
                nrcrt=0;
            }

            while(stiva.top()!='(')
            {
                if(stiva.top()=='+')
                {
                    int a, b;

                    a=polo.top();
                    polo.pop();

                    b=polo.top();
                    polo.pop();

                    polo.push(a+b);
                }
                if(stiva.top()=='-')
                {
                    int a, b;

                    a=polo.top();
                    polo.pop();

                    b=polo.top();
                    polo.pop();

                    polo.push(b-a);
                }
                if(stiva.top()=='*')
                {
                    int a, b;

                    a=polo.top();
                    polo.pop();

                    b=polo.top();
                    polo.pop();

                    polo.push(a*b);
                }
                if(stiva.top()=='/')
                {
                    int a, b;

                    a=polo.top();
                    polo.pop();

                    b=polo.top();
                    polo.pop();

                    polo.push(b/a);
                }
                stiva.pop();
            }
            stiva.pop();
        }
    }

    if(x[x.size()-1]!=')')
    {
        polo.push(nrcrt);
        nrcrt=0;
    }

    while(!stiva.empty())
    {
        if(stiva.top()=='+')
        {
            int a, b;

            a=polo.top();
            polo.pop();

            b=polo.top();
            polo.pop();

            polo.push(a+b);
        }
        if(stiva.top()=='-')
        {
            int a, b;

            a=polo.top();
            polo.pop();

            b=polo.top();
            polo.pop();

            polo.push(b-a);
        }
        if(stiva.top()=='*')
        {
            int a, b;

            a=polo.top();
            polo.pop();

            b=polo.top();
            polo.pop();

            polo.push(a*b);
        }
        if(stiva.top()=='/')
        {
            int a, b;

            a=polo.top();
            polo.pop();

            b=polo.top();
            polo.pop();

            polo.push(b/a);
        }
        stiva.pop();
    }
}

int main()
{
    fin >> x;

    poloneza(x);

    while(!polo.empty())
    {
        fout << polo.top() << " ";
        polo.pop();
    }

    return 0;
}