Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- template < class T, int maxsize = 20>
- class stake
- {
- public:
- stake(){ sp = 0; }
- void push(T data);
- T pop();
- private:
- int sp;
- T buffer[maxsize];
- static void error(){ cout << "\nstake error\n"; }
- };
- template<class T,int maxsize>
- void stake<T, maxsize>::push(T data)
- {
- if (sp == maxsize)
- error();
- else
- {
- buffer[sp++] = data;
- }
- }
- template<class T,int maxsize>
- T stake<T, maxsize>::pop()
- {
- if (sp == 0)
- {
- error();
- return 0;
- }
- else
- {
- cout << buffer[--sp];
- }
- }
- int main()
- {
- stake<int> st1;
- stake<char, 10> st2;
- st1.push(1);
- st1.push(2);
- st1.push(3);
- st2.push('a');
- st2.push('b');
- st2.push('c');
- st1.pop();
- st2.pop();
- st1.pop();
- st2.pop();
- st1.pop();
- st2.pop();
- st2.pop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement