Cod sursa(job #1616024)

Utilizator hackerliAndrei Ion hackerli Data 27 februarie 2016 01:29:33
Problema Evaluarea unei expresii Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.93 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char s[100001];
int t, n;
long long eval()
{
    int valori[100001];
    char semne[100001];
    int p=1, v=1;
    while(s[t]!=')'&&t<n)
    {
        if(s[t]=='+'||s[t]=='-'||s[t]=='*'||s[t]=='/')
        {
            // cout<<"Caz 1 \n";
            semne[p]=s[t];
            p++;
            t++;
        }
        else if(s[t]>='0'&&s[t]<='9')
        {
            // cout<<"Caz 2 \n";
            int val=0;
            while(s[t]>='0'&&s[t]<='9')
            {
                val=val*10+(s[t]-'0');
                t++;
            }
            valori[v]=val;
            v++;
        }
        else if(s[t]=='(')
        {
            // cout<<"Caz 3 \n";
            t++;
            valori[v]=eval();
            t++; //
            v++;
        }
    }
    //t++;
    char saux;
    for(int i=1; i<=p; i++) // de la 0?
    {
        if(semne[i]=='*')
        {
            semne[i]=saux;
            valori[i+1]=valori[i]*valori[i+1];
            valori[i]=0;
        }
        else if(semne[i]=='/')
        {
            semne[i]=saux;
            valori[i+1]=valori[i]/valori[i+1];
            valori[i]=0;
        }
        else
        {
            saux=semne[i];
        }
    }
    long long vf=0;
    int poz=0;
    while(valori[poz]==0&&poz<=v)
        poz++;
    if(poz<v)
    {
        vf=vf+valori[poz];
        for(int i=1; i<=p; i++) // de la 0?
        {
            if(semne[i]=='+')
            {
                vf=vf+valori[i+1];
            }
            else if(semne[i]=='-')
            {
                vf=vf-valori[i+1];
            }
        }
    }
    return vf;
}
int main()
{
    fin.getline(s, 1000000);
    n=strlen(s);
    t=0;
    fout<<eval();
    fin.close();
    fout.close();
    return 0;
}