#include<string>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
int main() {
int q;
string s;
stack <string> a;
int z=0;
cin>>q;
while(q)
{
int t;
cin>>t;
string m;
string n;
string x;
string l;
a.push(s);
int k;
int z;
switch(t)
{
case 1:
//append
cin>>x;
s.append(x);
//cout<<s<<endl;
x.clear();
a.push(s);
cout<<a.top()<<endl;
break;
case 2:
// cout<<s;
// delete
n=s;
cin>>k;
//cout<<s;
while(k>0)
{
s.pop_back();
k--;
}
a.push(s);
cout<<a.top()<<endl;
break;
case 3:
//print
l=a.top();
cin>>k;
cout<<l[k-1]<<endl;
l.clear();
break;
case 4:
//undo
a.pop();
cout<<a.top()<<endl;
break;
}
--q;}
return 0;
}
POP功能似乎无法正常使用以下输入
8
1 abc
3 3
2 3
1 xy
3 2
4
4
3 1
将xy
插入堆栈后,撤消操作会弹出堆栈但顶部始终为xy
后如何?为什么?
还有一件事,如果我声明字符串s
并在while循环中字符串,那么程序不运行并给出分段错误,为什么?