Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
3.1 kB
0
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

int main() {
    unordered_map<string,string> operands = {{"zero","0"},{"one","1"},{"two","2"},{"three","3"},{"four","4"},{"five","5"},{"six","6"},{"seven","7"},{"eight","8"},{"nine","9"}};
    unordered_map<string,int> operators = {{"mul",1},{"div",1},{"add",1},{"sub",1},{"rem",1},{"pow",1}};
    string expression ;
    getline(cin,expression);
    stringstream ss(expression);
    string word;
    int nums=0,ops=0;
    bool error=false;
    vector<string> simplified;
    while(ss>>word){
        if(operators.find(word)!=operators.end()){
            simplified.push_back(word);
        }
        else{
            stringstream n(word);
            string w;
            string number="";
            while(getline(n, w, 'c'))
            {
               if(operands.find(w)==operands.end()){
                   error=true;
                   break;
               }
               else{
                   number+=operands[w];
               }
               
            }
            simplified.push_back(number);
        }
        if(error){
            cout<<"expression evaluation stopped invalid words present";
            return 0;
        }
    }
    
    while(true){
    int opt_idx=-1, opd_idx1=-1,opd_idx2=-1;
        int count=0;
        for(int i=0;i<simplified.size();i++){
            string w=simplified[i];
            if(operators.find(w)!=operators.end()) opt_idx=i;
            else if(w=="-1") {
                count++;
                continue;
            }
            else if(opd_idx1==-1) opd_idx1=i;
            else opd_idx2 = i;
            if(opt_idx!=-1&&opd_idx1!=-1&&opd_idx2!=-1){
                if(opt_idx>opd_idx1||opt_idx>opd_idx2){
                    cout<<"expression is not complete or invalid";
                    return 0;
                }
                string opt = simplified[opt_idx];
                string ans;
                if(opt=="mul"){
                     ans = to_string(stoi(simplified[opd_idx1])*stoi(simplified[opd_idx2]));
                }
                else if(opt=="div"){
                    ans = to_string(stoi(simplified[opd_idx1])/stoi(simplified[opd_idx2]));
                }else if(opt=="add"){
                    ans = to_string(stoi(simplified[opd_idx1])+stoi(simplified[opd_idx2]));
                }else if(opt=="sub"){
                    ans = to_string(stoi(simplified[opd_idx1])-stoi(simplified[opd_idx2]));
                }else if(opt=="rem"){
                    ans = to_string(stoi(simplified[opd_idx1])%stoi(simplified[opd_idx2]));
                }else if(opt=="pow"){
                    ans = to_string(pow(stoi(simplified[opd_idx1]),stoi(simplified[opd_idx2])));
                }
                simplified[opt_idx]=ans;
                simplified[opd_idx1]="-1";
                simplified[opd_idx2]="-1";
                break;
            }
        }
        if(count==simplified.size()-1) break;
    }
    string gg="";
    for(auto s:simplified) if(s!="-1") gg=s;
    cout<<gg<<endl;
	return 0;
}
Leave a Comment