Cod sursa(job #2209798)

Utilizator DordeDorde Matei Dorde Data 4 iunie 2018 19:35:22
Problema Evaluarea unei expresii Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>
#include <string>
using namespace std;
char const in [] = "evaluare.in";
char const out [] = "evaluare.out";
int const NM = 1e5 + 7;
typedef long long i64;
char st [NM];
i64 termen ();
i64 factor ();
int point;
inline bool check1 (char c)
{
    return c == '+' || c == '-';
}
inline bool check2 (char c)
{
    return c == '*' || c == '/';
}
inline i64 eval ()
{
    i64 a = termen ();
    while(check1 (st [point]))
    {
        if (st [point] == '+')
        {
            ++ point;
            a += termen ();
            break;
        }
        else
        {
            ++ point;
            a -= termen ();
            break;
        }
    }
    return a;
}
inline i64 termen ()
{
    i64 a = factor ();
    while( check2 (st [point]))
    {
        if (st [point] == '*')
        {
            ++ point;
            a = a * factor () ;
            break;
        }
        else
        {
            ++ point;
            a = a / factor () ;
            break;
        }
    }
    return a;
}
inline i64 factor ()
{
    i64 a = 0;
    if(st [point] == '(')
    {
        ++ point;
        a = eval ();
        ++ point;
    }
    else
    {
        while(isdigit (st [point]))
        {
            a = a * 10 + (st [point] - '0');
            ++ point;
        }
    }
    return a;
}
int main()
{
    fstream f;
    fstream g;
    f . open (in , ios :: in);
    g . open (out , ios :: out);
    f >> st;
    g << eval () <<  ' ';
    return 0;
}