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