Cod sursa(job #3181901)

Utilizator db_123Balaban David db_123 Data 8 decembrie 2023 10:56:34
Problema Evaluarea unei expresii Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.96 kb
#include <fstream>
#include <vector>

using namespace std;

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

/// the code goes zbrrr

string s;

void read() {
    cin>>s;
}

void calculate_twoElem(vector<int>& stk1, vector<int>& stk2) {
    int curr=0;
    int elem1=stk2.back();
    stk2.pop_back();
    int elem2=stk2.back();
    stk2.pop_back();
    if(stk1.back() == 0) {
        curr += (elem2 + elem1);
    }
    else if(stk1.back() == 1) {
        curr += (elem2 - elem1);
    }
    else if(stk1.back() == 2) {
        curr += (elem2 * elem1);
    }
    else if(stk1.back() == 3) {
        curr += (elem2 / elem1);
    }
    stk2.push_back(curr);
    stk1.pop_back();
}

int eval() {
    vector<int> stk1; /// symbols
    vector<int> stk2; /// numbers
    /// 0 : +
    /// 1 : -
    /// 2 : *
    /// 3 : /
    
    for(int i=0;i<s.size();i++) {
        // cout<<"i: "<<i<<"\n";
        // cout<<"stk1: ";
        // for(auto it : stk1) {
        //     if(it==-1) {
        //         cout<<"(";
        //     }
        //     else if(it==0) {
        //         cout<<"+";
        //     }
        //     else if(it==1) {
        //         cout<<"-";
        //     }
        //     else if(it==2) {
        //         cout<<"*";
        //     }
        //     else {
        //         cout<<"/";
        //     }
        // }
        // cout<<"\n";
        // cout<<"stk2: ";
        // for(auto it : stk2) {
        //     cout<<it<<" ";
        // }
        // cout<<"\n\n";
        
        if(s[i]=='(') {
            stk1.push_back(-1);
        }
        else if(s[i]==')') {
            int curr=0;
            while(!stk1.empty() && !stk2.empty() && stk1.back()!=-1) {
                calculate_twoElem(stk1, stk2);
            }
            stk1.pop_back();
        }
        else if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/') {
            switch(s[i]) {
                case '+':
                    while(!stk1.empty() && stk1.back()>1) {
                        calculate_twoElem(stk1, stk2);
                    }
                    stk1.push_back(0);
                    break;
                case '-':
                    while(!stk1.empty() && stk1.back()>1) {
                        calculate_twoElem(stk1, stk2);
                    }
                    stk1.push_back(1);
                    break;
                case '*':
                    stk1.push_back(2);
                    break;
                case '/':
                    stk1.push_back(3);
                    break;
            }
        }
        else if(s[i]>='0' && s[i]<='9') {
            int nr=0;
            while(i<s.size() && s[i]>='0' && s[i]<='9') {
                nr *= 10;
                nr += s[i]-'0';
                i++;
            }
            i--;
            stk2.push_back(nr);
        }
    }
    while(!stk1.empty()) {
        calculate_twoElem(stk1, stk2);
    }
    return stk2.back();
}

void solve() {
    cout<<eval();
}

int main() {

    read();
    solve();
    return 0;
}