#include<iostream>
#include<cstdlib>
using namespace std;
#define MAX 100
template <typename T>
class Stack {
public:
Stack(); //构造函数
bool empty() const ; //判断S是否为空栈
bool full() const; //判断栈S是否为满,
~Stack(); //将栈S清空
int size() const; //求栈S的长度,即栈S中的元素的个数
const T & top() const; //返回栈S的栈顶元素
void push( const T &e ); //入栈操作,将数据元素e插入栈S的当前栈顶
T & pop(); //出栈操作,删除栈S的栈顶元素,并返回其值
private:
// 补充
T data[MAX];
int cur;
};
template <typename T>
Stack<T>::Stack()
{
cur = 0;
}
template <typename T>
bool Stack<T>::empty() const
{
return cur == 0;
}
template <typename T>
bool Stack<T>::full() const
{
return cur >= MAX;
}
template <typename T>
Stack<T>::~Stack() {}
template <typename T>
int Stack<T>::size() const { return cur; }
template <typename T>
const T & Stack<T>::top() const
{
if (cur == 0) throw "stack is empty";
return data[cur - 1];
}
template <typename T>
void Stack<T>::push( const T &e )
{
if (cur >= MAX) throw "stack is full";
data[cur++] = e;
}
template <typename T>
T & Stack<T>::pop()
{
if (cur == 0) throw "stack is empty";
return data[--cur];
}
//测试的main函数
int main() {
Stack<char> sc; //stack of char
Stack<int> si; //Stack of int
char ch = ' ';
// 不断输入字符到栈中,=结束
while (ch != '=') {
cin >> ch;
if (ch == 'a')
sc.push('A');
sc.push(ch);
}
cout << "top=" << sc.top() << "size=" << sc.size() << endl;
cout << "pop " << sc.pop() << endl;
while (!sc.empty())
cout << sc.pop()<< " ";
cout << endl;
int x = 1;
// 不断输入int到栈中,0结束
while (x != 0) {
cin >> x;
if (x > 10)
si.push(10000);
si.push(x);
}
cout << "top=" << si.top() << "size=" << si.size() << endl;
cout << "pop " << si.pop() << endl;
while (!si.empty())
cout << si.pop()<< "/";
cout << endl;
return 0;
}