C++:スタックで逆ポーランド式の値を求める

10051 ワード

最初の方法は、簡単ではありませんが、実際には式全体を直接巡回することができます.
//              

/*********************************************************************
       ,        :
1.   ,         vector
2.  vector,        ,    b,a         
3.      
**********************************************************************/

#include 
#include 
#include 
#include 
#include 
using namespace std;

//    
using Stack = struct Stack {
    int top = 0;
    string str[100];
};

//   
bool Push(Stack&, const string);
//   
bool Pop(Stack&, string&);
void ElemToVector(const string&, std::vector<string>&);
void Calc(Stack&, const std::vector<string>&);
inline bool is_digit(const string&);
inline int ConvertToInt(const string&);
inline char ConvertTOChar(const string&);
inline string ConvertToString(const int&);
inline void iCalc(Stack&, string&);


int main(int argc, char** argv)
{
    Stack S;
    std::vector<string> sv;
    string str("234 34+2*$");
    ElemToVector(str, sv);
    Calc(S, sv);
    cout << S.str[0];
    return 0;
}

void iCalc(Stack &S, string &x)
{

    string b, a; //          
    int nb, na;   
    Pop(S, b); 
    Pop(S, a);
    nb = ConvertToInt(b);
    na = ConvertToInt(a);
    char oper = ConvertTOChar(x);
    switch(oper)
    {
        case '+':
            Push(S, ConvertToString(na+nb));
            break;
        case '-':
            Push(S, ConvertToString(na-nb));
            break;
        case '*':
            Push(S, ConvertToString(na*nb));
            break;
        case '/':
            Push(S, ConvertToString(na/nb));
            break;
    }
}


string ConvertToString(const int &e)
{
    string result;
    stringstream stream;
    stream << e;
    stream >> result;
    return result;

}

char ConvertTOChar(const string& str)
{
    char result;
    stringstream stream;
    stream << str;
    stream >> result;
    return result;
}

int ConvertToInt(const string &str)
{
    int result;
    stringstream stream;
    stream << str;
    stream >> result;
    return result;
}


bool is_digit(const string &str)
{
    for(const auto &x : str)
        if(!isdigit(x)) return false;
    return true;

    // stringstream                    
    //                  
    // int temp;
    // istringstream istream(str);
    // if(istream >> temp) {
    //  cout << "yes" << temp << endl;
    //  return true;    
    // }
    // return false;

}


void Calc(Stack &S, const std::vector<string> &sv)
{
    for(const auto &x : sv) {
        if(is_digit(x)) {
            Push(S, x);
        }
        else {
            //       ,   
            string sum;
            for(const auto &c : x) {
                if(c == '$') continue;
                //       ,     
                if(isdigit(c)){
                    string str(1,c);
                    sum += str;
                }
                else {
                //         ,          
                    if(sum != "") {
                        Push(S, sum);
                        sum = "";
                    }
                    string oper(1, c);
                    iCalc(S, oper);
                }   
            }
        }
    }
}

void ElemToVector(const string &str, std::vector<string> &sv)
{
    istringstream istream(str);
    string elem;
    while(istream >> elem)
        if (elem != "$") sv.push_back(elem);
}


bool Push(Stack &S, const string e)
{
    S.str[S.top++] = e;
    return true;
}

bool Pop(Stack &S, string &e)
{
    e = S.str[--S.top];
    return true;
}