#include <iostream>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
using namespace std;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base = new ElemType[MAXSIZE];
if(!S.base) exit(OVERFLOW);
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
Status Push(SqStack &S, ElemType e)
{
if(S.top-S.base == MAXSIZE) return ERROR;
*S.top++ = e;
return OK;
}
Status Pop(SqStack &S, ElemType &e)
{
if(S.top == S.base) return ERROR;
e = *--S.top;
return OK;
}
ElemType GetTop(SqStack S)
{
if(S.top != S.base)
return *(S.top-1);
return ERROR;
}
int main()
{
SqStack S;
cout<<"1 InitStack"<<endl;
cout<<"2 Push"<<endl;
cout<<"3 Pop"<<endl;
cout<<"4 GetTop"<<endl;
int choice,e;
while(1)
{
cout<<"Input a choice: ";
cin>>choice;
switch(choice)
{
case 1:
InitStack(S);
continue;
case 2:
Push(S,3);
continue;
case 3:
Pop(S,e);
continue;
case 4:
cout<<"Get: "<<GetTop(S)<<endl;
continue;
default:
cout<<"End..."<<endl;
break;
}
break;
}
return 0;
}