#include <iostream>
#include <string>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if (n == 0) break;
char a;
stack<int> sta;
for(int i = 0;i < n; i++){
getchar();
scanf("%c",&a);//这边为什么会把换行当字符输入 才加个getchar() 或者用 cin >>a
if(a=='A'){
if(sta.empty()){
printf("E\n");
}else{
printf("%d\n",sta.top());
}
}else if(a == 'P'){
int b;
scanf("%d",&b);
sta.push(b);
}else if(a == 'O'){
if(!sta.empty()){
sta.pop();
}
}
}
printf("\n");
}
return 0;
}
堆栈的使用
最新推荐文章于 2023-08-31 19:13:37 发布
这是一个C++程序,主要使用栈数据结构处理字符输入。程序读取整数n,然后根据输入的字符'A'、'P'或'O'进行不同操作:'A'查询栈顶元素,'P'压栈,'O'弹栈。在读取字符前,通过getchar()排除换行符干扰。该程序展示了基本的输入处理和栈操作技巧。
摘要由CSDN通过智能技术生成