⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gen_stck.cpp

📁 不错书对C++/C程序员很有用的大家不要错过.
💻 CPP
字号:
#include <iostream.h>

const int SIZE = 100;

template <class SType> class stack {
   SType stck[SIZE];
   int tos;
 public:
   stack(void);
   ~stack(void);
   void push(SType i);
   SType pop(void);
 };

template <class SType> stack<SType>::stack()
 {
   tos = 0;
   cout << "Stack Initialized." << endl;
 }

template <class SType> stack<SType>::~stack()
 {
   cout << "Stack Destroyed." << endl;
 }

template <class SType> void stack<SType>::push(SType i)
 {
   if(tos==SIZE)
    {
      cout << "Stack is full." << endl;
      return;
    }
   stck[tos++] = i;
 }
template <class SType> SType stack<SType>::pop(void)
 {
   if(tos==0)
    {
      cout << "Stack underflow." << endl;
      return 0;
    }
   return stck[--tos];
 }

void main(void)
 {
   stack<int> a;
   stack<double> b;
   stack<char> c;
   int i;

   a.push(1);
   a.push(2);
   b.push(99.3);
   b.push(-12.23);

   cout << a.pop() << " ";
   cout << a.pop() << " ";
   cout << b.pop() << " ";
   cout << b.pop() << endl;

   for(i=0; i<10; i++)
      c.push((char) 'A' + i);
   for(i=0; i<10; i++)
      cout << c.pop();
   cout << endl;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -