棧的應用習題

//中綴表達式轉後綴表達式

/*特別注意 中綴表達式轉爲後綴表達式:當運算符優先級比棧頂運算符高時直接入運算棧,如果等於或低於棧頂運算符時將運算符棧的出棧 (轉爲前綴表達式時沒有等於)
*/
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    string str;
    stack<char> opr;
    stack<char> open;
    cout << "輸入一箇中綴表達式:"<< endl;
    cin >> str;
    cout << "原表達式爲:" << str << endl;

    auto i = str.begin();
    while (i != str.end())
    {
        switch (*i)
        {
            case '(': 
                {
                    opr.push(*i);   // 入棧
                    ++i;
                    break;
                }
            case '+':
            case '-':
                {
                    if (opr.empty())
                    {
                        opr.push(*i);
                        ++i;
                        break;
                    }
                    while (!opr.empty() && opr.top() != '(')
                    {
                        open.push( opr.top() ); // 當前運算符優先級低於操作符時                                       //把操作符棧內的入到數棧
                        opr.pop(); // 出棧

                    }   
                    opr.push(*i);
                    ++i;
                    break;
                }

            case '*':
            case '/':
                {
                    if (opr.empty())
                    {
                        opr.push(*i);
                        ++i;
                        break;
                    }
                    if ( (opr.top() == '+' || opr.top() == '-') || 
                                            opr.top() =='(' )
                    {   opr.push(*i);
                            ++i;
                            break;
                    }
                    while (!opr.empty() && 
                    (opr.top() == '/' || opr.top() == '*') )
                    {
                        open.push(opr.top());
                        opr.pop();
                    }
                    opr.push(*i);
                    ++i;
                    break;


                }
/*          case '/':
                {
//                  while (!opr.empty() && opr.top() != '(')
                    opr.push(*i);
                    ++i;
                    break;
                }
                */
            case ')':
                {
                    while (opr.top() != '(')
                    {
                        open.push(opr.top());
                        opr.pop();
                    }
                    if (opr.top() == '(')
                        opr.pop();
                    ++i;
                    break;
                }
            default :
                {
                    open.push(*i);
                    ++i;
                    break;

                }
        }
    }
    //opr彈出到open
    while (!opr.empty())
    {
        open.push(opr.top());
        opr.pop();

    }
    cout << "操作完畢:" << endl;
    //顯示棧:
    while (!open.empty())
    {
        cout << open.top();
        open.pop();
    }
    return 0;
}

//括號配對問題

#include <iostream>
#include <stack>
using namespace std;

int main()
{
    string str;
    cout << "請輸入一串只有()[]的字符" << endl;
    cin >> str;
    cout << "str 中的元素是:" << str << endl;

    stack<char>sta;  // 注意是char類型的
    auto i = str.begin();

    while (i != str.end())
    {
        if (*i == '(' || *i == '[')
        {
            sta.push(*i);
            i++;
        }

        else if ( *i == ')')
        {
            if (sta.empty() || sta.top() != '(')
            {
                cout << "no" << endl;
                return 0;
            }
            else if(sta.top() == '(')
            {
                sta.pop();
                ++i;
            }

        }

        else if (*i == ']')
        {
            if (sta.empty() || sta.top() != '[')
            {
                cout << "no" << endl;
                return 0;
            }
            else if (sta.top() == '[')
            {
                sta.pop();
                ++i;
            }
        }
    }

    if (sta.empty())
        cout << "yes" << endl;
    else
        cout << "no" << endl;
    cout << "結束";
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章