Cod sursa(job #2723531)

Utilizator ionanghelinaIonut Anghelina ionanghelina Data 14 martie 2021 15:26:27
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include<bits/stdc++.h>
using namespace std;
int i;

int expresie();
int termen();
int factor();
char s[100005];

inline int expresie()
{
    int r=termen();
    while(s[i]=='+' || s[i]=='-')
    {
        if(s[i]=='+')
        {
            i++;
            r+=termen();
        }
            else
        {
            i++;
            r-=termen();
        }
    }
    return r;
}

inline int termen()
{
    int r=factor();
    while(s[i]=='*' || s[i]=='/')
    {
        if(s[i]=='*')
        {
            i++;
            r*=factor();
        }
            else
        {
            i++;
            r/=factor();
        }
    }
    return r;
}

inline int factor()
{
    int r;
    if(s[i]=='(')
    {
        i++;
        r=expresie();
        i++;
    }
        else
    {
        r=0;
        while(isdigit(s[i]))
        {
            r=r*10+s[i]-'0';
            i++;
        }
    }
    return r;
}
int n;

int main()
{
    freopen("evaluare.in","r",stdin);
    freopen("evaluare.out","w",stdout);

    scanf("%s",s+1);
    n=strlen(s+1);
    i=1;

    printf("%d\n",expresie());

    return 0;
}