C++ Stack 與String


// ConsoleApplication1.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//

#include "pch.h"
#include <iostream>
#include<stack>
#include <string>

using namespace std;


int main()
{
 //   //std::cout << "Hello World!\n"; 
    //stack<string>  words;//初始化
    //string str;
    //cout << "Enter some words (Ctrl +Z to end ):" << endl;
    //while (cin >> str)
    //{
    //    words.push(str);//將數據壓棧
    //}


    //while (words.empty() == false)
    //{
    //    cout << words.top() << endl;//將數據輸出
    //    words.pop();//輸出后進行刪除
    //}

    //return 0;

    stack <char> sta;
    string str;
    cin >> str;

    string::iterator iter = str.begin();
    while (iter !=str.end())
    {
        if (*iter != ')')
        {
            sta.push(*iter);//取值並將值放入stack
        }
        else
        {
            while (sta.top() != '(' && !sta.empty())
            {
                sta.pop();
            }
            if (sta.empty())
            {
                cout << "000000000000";
            }
            else
            {
                sta.pop();
                sta.push('@');
            }
        }
        ++iter;
    }
    while (!sta.empty())
    {
        cout << sta.top() << endl;
        sta.pop();

    }
    return 0;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM