Cod sursa(job #3276750)

Utilizator suzanicaSuzanica Mihu suzanica Data 14 februarie 2025 13:46:51
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.78 kb
#include <bits/stdc++.h>
using namespace std;
vector< pair<string,int> > v;

void get_parenthesis_expressions(string str){
    int start = str.find_first_of('(');
    int End = -1;

    for(int i=start;i<str.length();i++){
        if(str[i]==')'){
            End=i;
            break;
        }
    }

    while(start!=string::npos && End!=-1){
        string m(str.substr(start,End-start+1));

        v.push_back(make_pair(m,0));
        str.erase(start,End-start+1);
        start = str.find_first_of('(');

        for(int i=start;i<str.length();i++){
            if(str[i]==')'){
                End=i;
                break;
            }
        }
    }
}

string strip(string str){
    string res;

    for(int i =0;i<str.length();i++){
        if(str[i]!=' '){
            res.push_back(str[i]);
        }
    }
    return res;
}

int ca(string s){
    char ce = '+';

    istringstream iss(s);

    string word;
    int r = 0;

    while(iss>>word){
        int val = atoi(word.c_str());

        if(val != 0){
            if(ce == '+'){
                r+=val;
            }
            if(ce == '-'){
                r-=val;
            }
        }
        else if(word[0] == '-'){
            ce = word[0];
        }
        else if(word[0] == '+'){
            ce = word[0];
        }

        int imul_pos = word.find('*');
        if(imul_pos!=string::npos){
                    int c = imul_pos+1;
                    string a,b;
                    while( isalnum(word[c]) ){
                        a.push_back(word[c]);
                        c++;
                    }
                    c=imul_pos-1;
                    while( isalnum(word[c]) ){
                        b.push_back(word[c]);
                        c--;
                    }
                    reverse(b.begin(),b.end());
                    //cout<<"i: "<<a<<" "<<b<<endl;

                    stringstream ss;
                    ss<<a<<" "<<b;

                    int a1,b1;
                    ss>>a1>>b1;

                    //cout<<"io: "<<a1<<" "<<b1<<endl;

                    if(ce == '-'){
                        //r-=atoi(a.c_str())*atoi(b.c_str());
                    }
                    if(ce == '+'){
                        //r+=atoi(a.c_str())*atoi(b.c_str());
                        r=r+a1*b1-a1;
                    }
        }
        int div_pos = word.find('/');
        if(div_pos!=string::npos){
                    int c=div_pos+1;
                    string a,b;

                    while( isalnum(word[c]) ){
                        a.push_back(word[c]);
                        c++;
                    }
                    c=div_pos-1;
                    while( isalnum(word[c]) ){
                        b.push_back(word[c]);
                        c--;
                    }
                    reverse(b.begin(),b.end());
                    //cout<<"d: "<<a<<" "<<b<<endl;

                    stringstream ss;
                    ss<<a<<" "<<b;

                    int a1,b1;
                    ss>>a1>>b1;

                    //cout<<"do: "<<a1<<" "<<b1<<endl;
                    if(ce == '-'){
                        r=r-a1/b1-a1;
                    }
                    if(ce == '+'){
                        r=r+a1/b1-a1;
                    }
                }
            }

    return r;
}

int main(){
    ifstream in("evaluare.in");
    ofstream out("evaluare.out");
     string str;
     getline(in,str);

     get_parenthesis_expressions(str);

     for(int i =0;i<v.size();i++){
        v[i].first.erase(v[i].first.begin());
        v[i].first.erase(v[i].first.end()-1);
        v[i].second = ca(v[i].first);
     }

     out<<ca(str)<<endl;

    return 0;
}