Cod sursa(job #2444294)

Utilizator ArkhamKnightyMarcus ArkhamKnighty Data 30 iulie 2019 23:04:26
Problema Bool Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>

using namespace std;

ifstream cin("evaluare.in");
ofstream cout("evaluare.out");

char s[100010];
int poz;
int eval();

int termen()
{
    int aux = 0;
    if(s[poz] == '(')
    {
        poz++;
        aux = eval();
        poz++;
        return aux;
    }
    while(s[poz] >= '0' && s[poz] <= '9')
        aux = aux * 10 + (s[poz++] - '0');
    return aux;
}

int produs()
{
    int aux = termen();

    while(s[poz] == '*' || s[poz] == '/')
    {
        if(s[poz++] == '*')
            aux = aux * termen();
        else aux /= termen();
    }

    return aux;
}

int eval()
{
    int aux = produs();

    while(s[poz] == '+' || s[poz] == '-')
    {
        if(s[poz++] == '+')
            aux = aux + produs();
        else aux -= produs();
    }

    return aux;
}

int main()
{
    cin >> s;
    cout << eval();
    return 0;
}