Cod sursa(job #2285199)

Utilizator Anakin1001George Giorgiu Gica Anakin1001 Data 18 noiembrie 2018 12:01:41
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>
#define mod 1000000000
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
int k1,k2,i,nr,st1[100001],st2[100001],rez,pr[301];
char S[100001];
long long operatie(char a,int x,int y){
    if(a=='+')
        return x+y;
    else if(a=='-')
        return y-x;
    else if(a=='*')
        return y*x;
    else
        return y/x;
}
int main()
{   f>>S;
    pr['(']=0;
    pr[')']=0;
    pr['+']=1;
    pr['-']=1;
    pr['*']=2;
    pr['/']=2;
    k1=0;k2=0;
    for(i=0;S[i]!=0;i++){
        if(S[i]>='0'&&S[i]<='9'){
            nr=0;
            while(S[i]>='0'&&S[i]<='9'){
                nr=nr*10+S[i]-'0';
                i++;
            }
            i--;
            st2[++k2]=nr%mod;
        }
        else
            if(S[i]=='(')
               st1[++k1]='(';
        else if(S[i]==')'){
            while(st1[k1]!='('){
                rez=operatie(st1[k1],st2[k2],st2[k2-1]);
                k1--;
                k2--;
                st2[k2]=rez%mod;
            }
            k1--;
        }
        else{
            while(k1>=1 && pr[st1[k1]]>=pr[S[i]]){
                rez=operatie(st1[k1],st2[k2],st2[k2-1]);
                k1--;
                k2--;
                st2[k2]=rez%mod;
            }
            st1[++k1]=S[i];
        }
    }
    while(k1>0){
        rez=operatie(st1[k1],st2[k2],st2[k2-1]);
        k1--;
        k2--;
        st2[k2]=rez%mod;
    }
    g<<st2[k2];
    return 0;
}