Cod sursa(job #2507043)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 9 decembrie 2019 15:25:11
Problema Evaluarea unei expresii Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("evaluare.in");
ofstream g ("evaluare.out");

char a[100005];
char *p = a;

int Eval();
int Termen();
int Factor();

int Eval()
{
    int r = Termen();

    while(*p == '+' || *p == '-')
    {
        if(*p == '+')
        {
            ++p;
            r += Termen();
        }
        else
        {
            ++p;
            r -= Termen();
        }
    }

    return r;
}

int Termen()
{
    int r = Factor();

    while(*p == '*' || *p == '/')
    {
        if(*p == '*')
        {
            ++p;
            r *= Factor();
        }
        else
        {
            ++p;
            r /= Factor();
        }
    }

    return r;
}

int Factor()
{
    int r = 0;

    if(*p == '(')
    {
        ++p;
        Eval();
        ++p;
    }
    else while(isdigit(*p))
        r = r * 10 + *p - '0' , p++;

    return r;
}

int main()
{
    f >> a;
    g << Eval();
    return 0;
}