Cod sursa(job #2541692)

Utilizator KPP17Popescu Paul KPP17 Data 8 februarie 2020 18:42:14
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
using namespace std;


#define fisier "evaluare"
#ifdef fisier
    #include <fstream>
    ifstream in(fisier ".in");
    ofstream out(fisier ".out");
#else
    #include <iostream>
    #define in cin
    #define out cout
#endif



int col;
char expr[100000];



inline bool numeric_chr(char chr) {return '0' <= chr && chr <= '9';}
inline bool paran_chr(char chr) {return chr == '(';}
inline bool operator_de_grad_1_chr(char chr) {return chr == '*' || chr == '/';}
inline bool operator_de_grad_0_chr(char chr) {return chr == '+' || chr == '-';}



int numar() {

    int rt = 0;

    while (numeric_chr(expr[col])) {

        rt *= 10;
        rt += expr[col] - '0';

        col++;

    }

    return rt;

}



int expresie();

int paran_sau_nr() {

    if (paran_chr(expr[col])) {

        col++;

        int rt = expresie();

        col++;

        return rt;

    } else {

        return numar();

    }

}



int grad_1() {

    int rt = paran_sau_nr();

    while (operator_de_grad_1_chr(expr[col])) {

        col++;

        if (expr[col - 1] == '*') {

            rt *= paran_sau_nr();

        } else {

            rt /= paran_sau_nr();

        }

    }

    return rt;

}



int expresie() { // int grad_0() {

    int rt = grad_1();

    while (operator_de_grad_0_chr(expr[col])) {

        col++;

        if (expr[col - 1] == '+') {

            rt += grad_1();

        } else {

            rt -= grad_1();

        }

    }

    return rt;

}



int main() {

    in >> expr;

    out << expresie();

}

















//