61 lines
1.4 KiB
C
61 lines
1.4 KiB
C
#define _CRT_SECURE_NO_WARNINGS
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int opcodeToNum(char* opcode){
|
|
if(!strcmp(opcode, "push")) return 0;
|
|
if(!strcmp(opcode, "pop")) return 1;
|
|
if(!strcmp(opcode, "size")) return 2;
|
|
if(!strcmp(opcode, "empty")) return 3;
|
|
if(!strcmp(opcode, "top")) return 4;
|
|
return -1;
|
|
}
|
|
|
|
int main() {
|
|
int N;
|
|
scanf("%d",&N);
|
|
int* stack = (int*)malloc(sizeof(int)* N);
|
|
memset(stack,0,sizeof(int)*N);
|
|
|
|
int len = 0;
|
|
while(N--){
|
|
char str[13];
|
|
if (fgets(str, sizeof(str), stdin) == NULL) break;
|
|
if (str[0] == '\n') { N++; continue; }
|
|
str[strcspn(str, "\n")] = 0;
|
|
char *opcode = strtok(str, " ");
|
|
char *operend = strtok(NULL, " ");
|
|
int opNum = opcodeToNum(opcode);
|
|
int operendNum = operend ? atoi(operend) : 0;
|
|
|
|
switch (opNum)
|
|
{
|
|
case 0:
|
|
stack[len] = operendNum;
|
|
len++;
|
|
break;
|
|
case 1:
|
|
if(len==0) {
|
|
printf("-1\n");
|
|
}
|
|
else {
|
|
printf("%d\n",stack[--len]);
|
|
}
|
|
break;
|
|
case 2:
|
|
printf("%d\n",len);
|
|
break;
|
|
case 3:
|
|
printf("%d\n", len ? 0 : 1);
|
|
break;
|
|
case 4:
|
|
printf("%d\n", len ? stack[len-1] : -1);
|
|
break;
|
|
}
|
|
}
|
|
|
|
free(stack);
|
|
return 0;
|
|
}
|