Cod sursa(job #3255381)

Utilizator answarIonascu Andrei answar Data 10 noiembrie 2024 15:30:47
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");
int t;
char v[100010];
int trm();
int f();
int exp() {
    int aux=trm();
    while(v[t]=='+'||v[t]=='-') {
        if(v[t]=='+'){
            t++;
            aux+=trm();
        }
        else {
            t++;
            aux-=trm();
        }
    }
    return aux;
}
int trm() {
    int aux=f();
    while(v[t]=='/'||v[t]=='*') {
        if(v[t]=='/'){
            t++;
            aux/=f();
        }
        else {
            t++;
            aux*=f();
        }
    }
    return aux;
}
int f() {
    int aux=0;
    if(v[t]=='(') {
        t++;
        aux=exp();
        t++;
    }
    else {
        aux=0;
        while(v[t]>='0'&&v[t]<='9') {
            aux*=10;
            aux+=v[t]-'0';
            t++;
        }
    }
    return aux;
}
int main() {
    cin>>v;
    t=0;
    cout<<exp();
}