Cod sursa(job #2334912)

Utilizator nicolaefilatNicolae Filat nicolaefilat Data 3 februarie 2019 12:37:36
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

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

string input;
int index;
char curent;

int factor();
int termen();
int expresie();
void moveon(){
    curent = input[++index];
}
int factor(){
    int r = 0;
    if(curent == '('){
        moveon();
        r = expresie();
        moveon();///trec peste )
    }while(isdigit(curent)){
        r = r * 10 + curent - '0';
        moveon();
    }
    return r;
}
int termen(){
    int r = factor();
    while(curent == '*' or curent == '/'){
        if(curent == '*'){
            moveon();
            r *= factor();
        }else{
            moveon();
            r /= factor();
        }
    }
    return r;
}
int expresie(){
    int r = termen();
    while(curent == '+' or curent == '-'){
        if(curent == '+'){
            moveon();
            r += termen();
        }else{
            moveon();
            r -= termen();
        }
    }
    return r;
}


int main()
{
    in>>input;
    curent = input[index];
    out<<expresie();
    return 0;
}