#include<stdlib.h>
typedef double DataType;
const int MaxStackSize = 50;
class Stack
{
private:
DataType stacklist[MaxStackSize];
int top;
public:
Stack(void);
void push(const DataType& item);
void clearStack(void);
//访问栈顶元素
DataType peek(void)const;
//检测栈顶状态方法
int stackEmpty(void) const;
int stackFull(void) const;
DataType pop(void);
};
#include"astack.h"
//初始化栈的下标top
using namespace std;
Stack::Stack(void):top(-1)
{
}
void Stack::push(const DataType& item)
{
if(top == MaxStackSize-1)
{
std::cerr<<"Stack overflow!"<<endl;
exit(1);
}
top++;
stacklist[top] = item;
}
DataType Stack::pop()
{
DataType temp;
//若栈为空 程序退出
if(top == -1)
{
cerr<<"Attempt to pop an empty stack!"<<endl;
exit(-1);
}
//将顶元素赋值给temp
temp = stacklist[top];
//top-1 返回temp值
top--;
return temp;
}
//返回栈顶元素的值
DataType Stack::peek()const
{
if(top == -1)
{
cerr<<"Attempt to peek t an empty stack!"<<endl;
exit(1);
}
return stacklist[top];
}
//检查栈是否为空
int Stack::stackEmpty() const
{
return top==-1;
}
//检查栈是否满
int Stack::stackFull()const
{
return top==MaxStackSize -1;
}
//从栈中清除所有元素
void Stack::clearStack()
{
top = -1;
}