#include <stdio.h> #include<stdbool.h> #include <stdlib.h> #include<string.h> #define MAXSIZE 100000 struct SqStack{ int data[MAXSIZE]; int top; }SqStack; //初始化栈 bool init(struct SqStack* S); //push操作(入栈) bool push(struct SqStack* S,int x); //pop操作(出栈) bool pop(struct SqStack* S); //top操作(输出栈顶数据) bool top(struct SqStack S); //处理输入的函数 void get(char* str); int main() { int n; scanf("%d",&n); int cot=0; char str[7]; struct SqStack S; init(&S); while(cot<=n){ bool status; get(str); if(!strcmp(str,"push")){ int x; scanf("%d",&x); getchar(); push(&S,x); } else if(!strcmp(str,"pop")){ status=pop(&S); if(!status) printf("error\n"); } else if(!strcmp(str,"top")){ status=top(S); if(!status) printf("error\n"); } cot++; } return 0; } bool init(struct SqStack* S){ S->top=-1; return true; } bool push(struct SqStack* S,int x){ bool flag; if(S->top>MAXSIZE-1) return false; S->top++; S->data[S->top]=x; return true; } bool pop(struct SqStack* S){ if(S->top==-1) return false; printf("%d\n",S->data[S->top]); S->top--; return true; } bool top(struct SqStack S){ if(S.top==-1) return false; printf("%d\n",S.data[S.top]); return true; } void get(char* str){ char ch; int i=0; while((ch=getchar())!='\n'){ if(ch==' ') break; str[i]=ch; i++; } str[i]='\0';//字符串的结尾必须有'\0' }