写了一个栈模拟
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int head=0;
int s[N];
bool empty(){
if(head==0) return true;
else return false;
}
void pop(){
head--;
return;
}
void push(int x){
head++;
s[head]=x;
return;
}
void print(){
printf("\n\nStack printing:\n\n");
_sleep(500);
if(empty()) printf(" empty\n\n");
for(int i=head;i>=1;i--){
printf("| %d |\n",s[i]);
printf("------------\n");
_sleep(30);
}
return;
}
int main(){
printf("指令说明:empty 判断栈空,print 打印栈,pop出栈,push x 入栈x。\n\n");
printf("Instruction description: empty determines that the stack is empty, print prints the stack, pop exits the stack, push x enters the stack.\n\n");
while(true){
char input[10];
printf("input >>>");
cin>>input;
if(input[0]=='-' and input[1]=='1') break;
else if(input[0]=='p' and input[1]=='o' and input[2]=='p'){
if(!empty()) pop();
else if(empty()) printf("\nIllegal operation: the stack is empty!\n\n");
}
else if(input[0]=='p' and input[1]=='u' and input[2]=='s' and input[3]=='h'){
int x;
scanf("%d",&x);
push(x);
}
else if(input[0]=='e' and input[1]=='m' and input[2]=='p' and input[3]=='t' and input[4]=='y'){
if(!empty()) printf("\nStack is not empty.\n\n");
else if(empty()) printf("\nthe stack is empty.\n\n");
}
else if(input[0]=='p' and input[1]=='r' and input[2]=='i' and input[3]=='n' and input[4]=='t'){
print();
}
else{
printf("\nIllegal operation: Illegal input\n\n");
}
}
return 0;
}
-1退出
不错啊