Cod sursa(job #1524522)

Utilizator NicuBaciuNicu Baciu NicuBaciu Data 14 noiembrie 2015 10:54:25
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 7.3 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]=='+')
        {
            if(x[i-1]!=')')
            {
                if(negativ==true || nu==true || nup==true)
                {
                    polo[j]=-nrcrt;
                    j++;
                    nrcrt=0;
                    negativ=false;
                    nu=false;
                }
                else
                {
                    polo[j]=nrcrt;
                    j++;
                    nrcrt=0;
                }
            }

            if(!stiva.empty())
            {
                if(stiva.top()=='*')
                {
                    polo[j]=-1000000003;
                    j++;
                    stiva.pop();
                    stiva.push('+');
                }
                else if(stiva.top()=='/')
                {
                    polo[j]=-1000000004;
                    j++;
                    stiva.pop();
                    stiva.push('+');
                }
                else
                    stiva.push('+');
            }
            else
                stiva.push('+');
        }
        else if(x[i]=='-')
        {
            if(i==0)
                negativ=true;
            else
            {
                if(!(x[i-1]>='0' && x[i-1]<='9') && x[i-1]!=')')
                    negativ=true;
                else
                {
                    nu=true;
                    if(x[i+1]=='(')
                        nup=true;
                    if(x[i-1]!=')')
                    {
                        polo[j]=nrcrt;
                        j++;
                        nrcrt=0;
                    }
                    if(!stiva.empty())
                    {
                        if(stiva.top()=='*')
                        {
                            polo[j]=-1000000003;
                            j++;
                            stiva.pop();
                            stiva.push('+');
                        }
                        else if(stiva.top()=='/')
                        {
                            polo[j]=-1000000004;
                            j++;
                            stiva.pop();
                            stiva.push('+');
                        }
                        else
                            stiva.push('+');
                    }
                    else
                        stiva.push('+');
                }
            }
        }
        else if(x[i]=='*')
        {
            if(x[i-1]!=')')
            {
                if(negativ==true || nu==true || nup==true)
                {
                    polo[j]=-nrcrt;
                    j++;
                    nrcrt=0;
                    negativ=false;
                    nu=false;
                }
                else
                {
                    polo[j]=nrcrt;
                    j++;
                    nrcrt=0;
                }
            }
            stiva.push('*');
        }
        else if(x[i]=='/')
        {
            if(x[i-1]!=')')
            {
                if(negativ==true || nu==true || nup==true)
                {
                    polo[j]=-nrcrt;
                    j++;
                    nrcrt=0;
                    negativ=false;
                    nu=false;
                }
                else
                {
                    polo[j]=nrcrt;
                    j++;
                    nrcrt=0;
                }
            }
            stiva.push('/');
        }
        else if(x[i]=='(')
        {
            stiva.push('(');
        }
        else if(x[i]==')')
        {
            if(i==x.size()-1)
                paranteza=true;

            if(x[i-1]!=')')
            {
                if(negativ==true || nu==true || nup==true)
                {
                    polo[j]=-nrcrt;
                    j++;
                    nrcrt=0;
                    negativ=false;
                    nu=false;
                    nup=false;
                }
                else
                {
                    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(!paranteza)
    {
        if(negativ==true || nu==true || nup==true)
        {
            polo[j]=-nrcrt;
            j++;
            nrcrt=0;
        }
        else
        {
            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 modifica(int a[])
{
    bool sem1=false, sem2=false;

    for(int i=j-1; i>=1; i--)
    {
        if(a[i]==-1000000004)
        {
            if(sem2==true)
                a[i]=-1000000003;
            sem1=false;
            sem2=true;
        }
        else if(a[i]==-1000000002)
        {
            if(sem1==true)
                a[i]=-1000000001;
            sem1=true;
            sem2=false;
        }
        else
        {
            sem1=false;
            sem2=false;
        }
    }
}

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;

        for(int k=1; k<=j-1; k++)
            fout << a[k] << " ";
        fout << '\n';
    }
}

int main()
{
    fin >> x;

    poloneza(x);

    for(int k=1; k<=j-1; k++)
        fout << polo[k] << " ";

    fout << '\n';

    modifica(polo);
    rezolva(polo);

//    fprintf(f, "%.5d", polo+1);

    fout << polo[1];

    return 0;
}