Cod sursa(job #3377163)

Utilizator biancagravaGRAVA Bianca biancagrava Data 21 noiembrie 2026 23:13:08
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.94 kb
//
// Created by Bianca Grava on 21/11/2026.
//
#include<iostream>
#include<fstream>
#include<stack>
#include<string>
#include<vector>

using namespace std;

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

int precedence(char ch) {
    if (ch=='^') return 3;
    if (ch=='*'||ch=='/') return 2;
    if (ch=='+'||ch=='-') return 1;
}

vector<string> postfixata(const string &infi) {
    string operand="";
    vector<string> result;
    stack<char> s;
    for (char ch : infi) {
        if (ch>='0'&&ch<='9')
            operand+=ch;
        else {
            if (ch=='(') {
                s.push('(');
            }else {
                if (!operand.empty()) {
                    result.push_back(operand);
                    operand="";
                }
                if (ch==')') {
                    while (!s.empty()&&s.top()!='(') {
                        result.push_back(string(1,s.top()));
                        s.pop();
                    }
                    if (!s.empty())
                        s.pop();
                }else {
                    //ch is an operator
                    if (!operand.empty()) {
                        result.push_back(operand);
                        operand="";
                    }
                    while (!s.empty()&&precedence(ch)<=precedence(s.top())&&s.top()!='(') {
                        result.push_back(string(1,s.top()));
                        s.pop();
                    }
                    s.push(ch);
                }
            }
        }
    }
    if (!operand.empty())
    result.push_back(operand);
    while (!s.empty()) {
        result.push_back(string(1,s.top()));
        s.pop();
    }
    return result;
}

int evaluare(const vector<string> &postfi) {
    stack<string> st;
    int op1,op2;
    for (string s: postfi) {
        if (s[0]>='0'&&s[0]<='9')
            st.push(s);
        else {
            if (st.empty())
                return -1;
            op1=stoi(st.top());
            st.pop();
            if (st.empty())
                return -1;
            op2=stoi(st.top());
            st.pop();
            if (s=="-")
                op1=op2-op1;
            else {
                if (s=="+")
                    op1=op2+op1;
                else {
                    if (s=="*")
                        op1=op2*op1;
                    else {
                        if (s=="/")
                            op1=op2/op1;
                        else {
                            if (s=="^")
                                op1=op2^op1;
                            else
                               return -1;
                        }
                    }
                }
            }
            st.push(to_string(op1));
        }
    }
    return stoi(st.top());
}

int main(){
    string s;
    in>>s;
    vector<string> post=postfixata(s);
    out<<evaluare(post);
    //if (result!=-1)
      //  out<<result;
    //else
        //out<<"incorect"<<endl;
    return 0;
}