用C语言实现栈,尽可能使用面向对象思想,不得不说C语言写起来不如C++顺手

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef MAX
#define MAX 100000
#endif

typedef struct {
  int arr[MAX];
  int base;
}stack;

void pop(stack *s) {
  if (s->base == -1) {
    printf("error\n");
    return ;
  } else {
    printf("%d\n", s->arr[(s->base)--]);
  }
}

void push(stack *s, int num) {
  s->arr[++(s->base)] = num;
}

int top(stack *s) {
  if (s->base == -1) {
    printf("error\n");
    return -1;
  } else {
    return s->arr[(s->base)];
  }
}

int main(int argc, char *argv[]) {
  stack *s = (stack *)malloc(sizeof(stack));
  s->base = -1;
  
  int count = 0;
  scanf("%d", &count);
  
  char str[5];
  
  while (--count >= 0) {
    scanf("%s", str);
    
    if (!strcmp(str, "push")) {
      int tmp;
      scanf("%d", &tmp);
      push(s, tmp);
    } else if (!strcmp(str, "pop")) {
      pop(s);
    } else if (!strcmp(str, "top")) {
      int tmp = top(s);
      if (tmp != -1) {
        printf("%d\n", tmp);
      }
    }
  }
  
  return 0;
}