Description
使用C++的STL堆栈对象,编写程序实现行编辑功能。行编辑功能是:当输入#字符,则执行退格操作;如果无字符可退就不操作,不会报错
本程序默认不会显示#字符,所以连续输入多个#表示连续执行多次退格操作
每输入一行字符打回车则表示字符串结束
注意:必须使用堆栈实现,而且结果必须是正序输出
Input
第一行输入一个整数t,表示有t行字符串要输入
第二行起输入一行字符串,共输入t行
Output
每行输出最终处理后的结果,如果一行输入的字符串经过处理后没有字符输出,则直接输出NULL
Sample
#0
Input
Copy
4 chinaa# sb#zb#u ##shen###zhen### chi##a#####
Output
Copy
china szu sz NULL
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
int t; //输入次数
cin >> t;
while (t--)
{
stack <char> s; //操作栈
stack <char> a; //输出栈
string str;
cin >> str;
int len = str.length();
for (int i = 0; i < len; i++)
{
if (str[i] != '#')//不是#
s.push(str[i]);//存入
else if (str[i] == '#')
{
if (s.empty()) {}//如果栈为空 不操作
else
s.pop();//弹出
}
}
//如果栈为空
if (s.empty())
cout << "NULL";
while (!s.empty())
{
a.push(s.top());
s.pop();
//等价于如下代码
/*char c2 = s.top();
s.pop();
a.push(s.top());*/
}
while (!a.empty())
{
cout << a.top();
a.pop();
/*char c1 = a.top();
a.pop();
cout << c1;*/
}
cout << endl;
}
return 0;
}