Cod sursa(job #3377328)

Utilizator biancagravaGRAVA Bianca biancagrava Data 22 noiembrie 2026 21:48:46
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 5.38 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;
    bool ok=true;
    for (char ch : infi) {
        if (ch>='0'&&ch<='9') {
            operand+=ch;
            ok=false;
        }
        else {
            if (ch=='(') {
                s.push('(');
                ok=true;
            }else {
                if (!operand.empty()) {
                    result.push_back(operand);
                    operand="";
                }
                if (ch==')') {
                    ok=false;
                    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()&&ch=='-'&&ok==true) {
                    result.push_back("0");
                        s.push('-');
                    }
                    else {
                        ok=true;
                        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')||(s[0]=='-'&&s.length()>1))
            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);
    out<<endl;
    //if (result!=-1)
      //  out<<result;
    //else
        //out<<"incorect"<<endl;
    return 0;
}
//
// #include <bits/stdc++.h>
// using namespace std;
// ifstream fin("evaluare.in");
// ofstream fout("evaluare.out");
// const int pd=1000000001, inm=1000000002, mi=1e9+3, divi=1e9+4;
// char s[100005];
// int st[100005], top;
// int main()
// {
//     fin>>s;
//     int nr=0;
//     for(int i=0; s[i]!='\0'; i++)
//     {
//         char c=s[i];
//         if(c=='(')
//             st[++top]=pd;
//         else if(c=='-')
//             st[++top]=mi;
//         else if(c=='*')
//             st[++top]=inm;
//         else if(c=='/')
//             st[++top]=divi;
//         else if(c>='0' && c<='9')
//         {
//             nr=0;
//             while(s[i]>='0' && s[i]<='9')
//             {
//                 nr=nr*10+(s[i]-'0');
//                 i++;
//             }
//             i--;
//             if(top==0 || st[top]==pd || st[top]<=1e9)
//                 st[++top]=nr;
//             else
//             {
//                 if(st[top]==mi)
//                     st[top]=-nr;
//                 else if(st[top]==inm || st[top]==divi)
//                 {
//                     int op=st[top--];
//                     if(op==inm)
//                         st[top]=st[top]*nr;
//                     else st[top]=st[top]/nr;
//                 }
//             }
//         }
//         else if(c==')')
//         {
//             nr=0;
//             while(st[top]!=pd)
//                 nr+=st[top--];
//             top--;
//             if(top==0 || st[top]==pd || st[top]<=1e9)
//                 st[++top]=nr;
//             else
//             {
//                 if(st[top]==mi)
//                     st[top]=-nr;
//                 else if(st[top]==inm || st[top]==divi)
//                 {
//                     int op=st[top--];
//                     if(op==inm)
//                         st[top]=st[top]*nr;
//                     else st[top]=st[top]/nr;
//                 }
//             }
//         }
//     }
//     int r=0;
//     while(top>0)
//         r+=st[top--];
//     fout<<r;
//     return 0;
// }