Cod sursa(job #2209797)

Utilizator DordeDorde Matei Dorde Data 4 iunie 2018 19:32:02
Problema Evaluarea unei expresii Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>
#include <string>
using namespace std;
char const in [] = "evaluare.in";
char const out [] = "evaluare.out";
int const NM = 1e5 + 7;
char st [NM];
int termen ();
int factor ();
int point;
inline bool check1 (char c)
{
    return c == '+' || c == '-';
}
inline bool check2 (char c)
{
    return c == '*' || c == '/';
}
inline int eval ()
{
    int a = termen ();
    while(check1 (st [point]))
    {
        if (st [point] == '+')
        {
            ++ point;
            a += termen ();
            break;
        }
        else
        {
            ++ point;
            a -= termen ();
            break;
        }
    }
    return a;
}
inline int termen ()
{
    int a = factor ();
    while( check2 (st [point]))
    {
        if (st [point] == '*')
        {
            ++ point;
            a = a * factor () ;
            break;
        }
        else
        {
            ++ point;
            a = a / factor () ;
            break;
        }
    }
    return a;
}
inline int factor ()
{
    int 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;
}