Cod sursa(job #2289536)

Utilizator ioanaa_ghGhiorghi Ioana-Cristina ioanaa_gh Data 24 noiembrie 2018 19:15:47
Problema Evaluarea unei expresii Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.21 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("evaluare.in");
ofstream fout("evaluare.out");

int st[100005];

char s[100005];

/*
'(' = 10000000001;
'*' = 10000000003;
'/' = 10000000004;
*/

int main()
{
    int i, top = 0, p, j, nr, k, semn = 1;
    fin >> s;
    for(i = 0; s[i]; )
    {
        if(s[i] == '(')
            st[++top] = 1000000001, i++;
        if(s[i] == '+')
            i++;
        if(s[i] == '-')
            semn = -1, i++;
        if(s[i] == '*')
            st[++top] = 1000000003, i++;
        if(s[i] == '/')
            st[++top] = 1000000004, i++;
        if('0' <= s[i] && s[i] <= '9')
        {
            nr = 0;
            while('0' <= s[i] && s[i] <= '9')
                nr = nr * 10 + (s[i] - '0'), i++;
            if(semn == -1)
                nr *= semn, semn = 1;
            while((st[top] == 1000000003 || st[top] == 1000000004) && top > 0)
            {
                if(st[top] == 1000000003)
                {
                    top--;
                    nr *= st[top];
                    top--;
                }
                if(st[top] == 1000000004)
                {
                    top--;
                    nr = st[top] / nr;
                    top--;
                }
            }
            st[++top] = nr;
        }
        if(s[i] == ')')
        {
            nr = 0;
            while(st[top] != 1000000001 && top > 0)
                nr += st[top], top--;
            top--;
            while((st[top] == 1000000003 || st[top] == 1000000004) && top > 0)
            {
                if(st[top] == 1000000003)
                {
                    top--;
                    nr *= st[top];
                    top--;
                }
                if(st[top] == 1000000004)
                {
                    top--;
                    nr = st[top] / nr;
                    top--;
                }
            }
            if(semn == -1)
                nr *= semn, semn = 1;
            st[++top] = nr;
            i++;
        }
    }
    for(j = 2; j <= top; j++)
        st[1] += st[j];
    fout << st[1] << " ";
    fin.close();
    fout.close();
    return 0;
}