So easy
2026-02-06 14:49:00
发布于:浙江
2阅读
0回复
0点赞
模拟栈+函数+分支语句
#include<iostream>
#include<stack>
#include<string>
using namespace std;
stack<int> judg(stack<int> s,int n,int x,string str){
if(str=="push"){
cin>>x;
s.push(x);
}
else if(str=="pop"){
if(!s.empty()){
cout<<"pop "<<s.top()<<endl;
s.pop();
}
else{
cout<<"pop fail"<<endl;
}
}
else if(str=="top"){
if(!s.empty()){
cout<<"top = "<<s.top()<<endl;
}
else{
cout<<"top fail"<<endl;
}
}
else if(str=="size"){
cout<<"size = "<<s.size()<<endl;
}
else if(str=="empty"){
if(s.empty()){
cout<<"yes"<<endl;
}
else{
cout<<"no"<<endl;
}
}
return s;
}
int main(){
stack<int> s;
int n,x;
string str;
cin>>n;
for(int i=0;i<n;i++){
cin>>str;
s=judg(s,n,x,str);
}
}
这里空空如也







有帮助,赞一个