Cod sursa(job #1571185)

Utilizator NicuBaciuNicu Baciu NicuBaciu Data 17 ianuarie 2016 14:13:31
Problema Evaluarea unei expresii Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 4.46 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;
int polo[1000000];
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]=='+' || x[i]=='-')
        {
            if(x[i-1]!=')')
            {
                polo[j]=nrcrt;
                j++;
                nrcrt=0;
            }
            if(!stiva.empty())
            {
                if(stiva.top()=='*')
                {
                    polo[j]=-1000000003;
                    j++;
                    stiva.pop();
                    stiva.push(x[i]);
                }
                else if(stiva.top()=='/')
                {
                    polo[j]=-1000000004;
                    j++;
                    stiva.pop();
                    stiva.push(x[i]);
                }
                else if(stiva.top()=='+')
                {
                    polo[j]=-1000000001;
                    j++;
                    stiva.pop();
                    stiva.push(x[i]);
                }
                else if(stiva.top()=='-')
                {
                    polo[j]=-1000000002;
                    j++;
                    stiva.pop();
                    stiva.push(x[i]);
                }
                else
                    stiva.push(x[i]);
            }
            else
                stiva.push(x[i]);
        }
        else if(x[i]=='*' || x[i]=='/')
        {
            if(x[i-1]!=')')
            {
                polo[j]=nrcrt;
                j++;
                nrcrt=0;
            }
            if(!stiva.empty())
            {
                if(stiva.top()=='*')
                {
                    polo[j]=-1000000003;
                    j++;
                    stiva.pop();
                    stiva.push(x[i]);
                }
                else if(stiva.top()=='/')
                {
                    polo[j]=-1000000004;
                    j++;
                    stiva.pop();
                    stiva.push(x[i]);
                }
                else
                    stiva.push(x[i]);
            }
            else
                stiva.push(x[i]);
        }
        else if(x[i]=='(')
        {
            stiva.push('(');
        }
        else if(x[i]==')')
        {
            if(x[i-1]!=')')
            {
                polo[j]=nrcrt;
                j++;
                nrcrt=0;
            }

            while(stiva.top()!='(')
            {
                if(stiva.top()=='+')
                    polo[j]=-1000000001;
                if(stiva.top()=='-')
                    polo[j]=-1000000002;
                if(stiva.top()=='*')
                    polo[j]=-1000000003;
                if(stiva.top()=='/')
                    polo[j]=-1000000004;
                j++;
                stiva.pop();
            }
            stiva.pop();
        }
    }

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

    while(!stiva.empty())
    {
        if(stiva.top()=='+')
            polo[j]=-1000000001;
        if(stiva.top()=='-')
            polo[j]=-1000000002;
        if(stiva.top()=='*')
            polo[j]=-1000000003;
        if(stiva.top()=='/')
            polo[j]=-1000000004;
        j++;
        stiva.pop();
    }
}

void rezolva(int a[])
{
    int p;
    int lung=j;
    for(int i=1; i<=(lung-1)/2; i++)
    {
        p=1;
        while(a[p]!=-1000000001 && a[p]!=-1000000002 && a[p]!=-1000000003 && a[p]!=-1000000004)
            p++;

        if(a[p]==-1000000001)
            a[p-2]=a[p-2]+a[p-1];
        if(a[p]==-1000000002)
            a[p-2]=a[p-2]-a[p-1];
        if(a[p]==-1000000003)
            a[p-2]=a[p-2]*a[p-1];
        if(a[p]==-1000000004)
            a[p-2]=a[p-2]/a[p-1];

        a[p-1]=0; a[p]=0;

        for(int k=p+1; k<=j-1; k++)
            a[k-2]=a[k];

        j-=2;
    }
}

int main()
{
    fin >> x;

    poloneza(x);

    rezolva(polo);

    fout << polo[1];

    return 0;
}