Cod sursa(job #1378412)

Utilizator Viorell008Iordache Viorel Viorell008 Data 6 martie 2015 12:03:00
Problema Evaluarea unei expresii Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.2 kb
#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;

int st[100005],n,top;
char s[100005];

void Citire()
{
    ifstream fin("evaluare.in");
    fin>>s;
    n=strlen(s);
    fin.close();

}

void Rezolvare()
{
    int i,nr;
    int semn,stiva;
    top=-1;
    i=0;
    while(i<=n-1)
    {
        if(s[i]=='(')
        {
           st[++top]=1000000001;
           i++;
        }

        if(s[i-1]=='(' && s[i]-'0'>=0 && s[i]-'0'<=9)
        {
            nr=0;
            while(s[i]-'0'>=0 && s[i]-'0'<=9)
            {
                nr=nr*10+(s[i]-'0');    ///(1+1)*13+10/2
                i++;
            }
            st[++top]=nr;
        }

        if(s[i]=='+')
        {
            nr=0; i++;
            while(s[i]-'0'>=0 && s[i]-'0'<=9)
            {
                nr=nr*10+(s[i]-'0');
                i++;
            }
            st[++top]=nr;

        }

        if(s[i]=='-')
        {
            nr=0; i++;
            while(s[i]-'0'>=0 && s[i]-'0'<=9)
            {
                nr=nr*10+(s[i]-'0');
                i++;
            }
            semn=-1;
            st[++top]=nr*semn;
        }

        if(s[i]=='*')
        {
            nr=0; i++;
            while(s[i]-'0'>=0 && s[i]-'0'<=9)
            {
                nr=nr*10+(s[i]-'0');
                i++;
            }
            st[top]=st[top]*nr;

        }

         if(s[i]=='/')
        {
            nr=0; i++;
            while(s[i]-'0'>=0 && s[i]-'0'<=9)
            {
                nr=nr*10+(s[i]-'0');
                i++;
            }
            st[top]=st[top]/nr;
        }

        if (s[i]==')')
        {

            i++;
            stiva=0;
            while (st[top]!=1000000001)
            {
                stiva=stiva+st[top];
                top--;
            }
             st[top]=stiva;
        }

    }

}

void Afisare()
{
    int suma,i;
    suma=0;
    ofstream fout("evaluare.out");
    for(i=0;i<=top;i++)
    {
        suma=suma+st[i];
    }

    fout<<suma<<"\n";
    fout.close();
}

int main()
{
    Citire();
    Rezolvare();
    Afisare();
    return 0;
}