#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
template <class T>
class Mystack{
public:
Mystack(int max){
if(max >= 0xff || max <= 0)
this->max = 0xff;
this->max = max;
i = -1;
}
bool push(T t);
bool pop(T& t);
int size(void);
bool printall(void);
private:
T t[0xff];
int max, i;
protected:
};
template <class T>
bool Mystack<T>::push(T t){
if(size() >= max){
cout << "The Stack is full!" << endl;
return false;
}
this->t[++i] = t;
return true;
}
template <class T>
bool Mystack<T>::pop(T& t){
if(size() <= 0){
cout << "The Stack is empty!" << endl;
return false;
}
t = this->t[i--];
return true;
}
template <class T>
int Mystack<T>::size(void){
return i + 1;
}
template <class T>
bool Mystack<T>::printall(void){
if(!size())
return false;
for(int i = 0; i < size(); ++i)
cout << " " << t[i];
cout << endl;
return true;
}
int main(void){
Mystack<int> s(20);
int i;
int& ii = i;
s.push(8);
s.pop(ii);
cout << s.size() << endl;
cout << ii << endl;
s.push(9);
s.push(10);
for(int i = 0; i < 23; i++)
s.push(0xf);
s.printall();
for(int i = 0; i < 25; i++)
s.pop(ii);
s.printall();
ifstream infile("test09.cpp");
ofstream outfile("mytext.txt");
string str;
vector<string> v;
while(getline(infile, str)){
v.push_back(str);
}
for(int i = 0; i < v.size(); i++)
outfile << i + 1 << ": " << v[i] << endl;
infile.close();
outfile.close();
}
/* running result
C:\Users\Administrator>g++ -o test09.exe test09.cpp
C:\Users\Administrator>test09
0
8
The Stack is full!
The Stack is full!
The Stack is full!
The Stack is full!
The Stack is full!
9 10 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
The Stack is empty!
The Stack is empty!
The Stack is empty!
The Stack is empty!
The Stack is empty!
*/
/* mytext.txt
1: #include <string>
2: #include <vector>
3: #include <iostream>
4: #include <fstream>
5:
6: using namespace std;
7:
8: template <class T>
9: class Mystack{
10: public:
11: Mystack(int max){
12: if(max >= 0xff || max <= 0)
13: this->max = 0xff;
14: this->max = max;
15: i = -1;
16: }
17: bool push(T t);
18: bool pop(T& t);
19: int size(void);
20: bool printall(void);
21: private:
22: T t[0xff];
23: int max, i;
24: protected:
25: };
26:
27: template <class T>
28: bool Mystack<T>::push(T t){
29: if(size() >= max){
30: cout << "The Stack is full!" << endl;
31: return false;
32: }
33: this->t[++i] = t;
34: return true;
35: }
36:
37: template <class T>
38: bool Mystack<T>::pop(T& t){
39: if(size() <= 0){
40: cout << "The Stack is empty!" << endl;
41: return false;
42: }
43: t = this->t[i--];
44: return true;
45: }
46:
47: template <class T>
48: int Mystack<T>::size(void){
49: return i + 1;
50: }
51:
52: template <class T>
53: bool Mystack<T>::printall(void){
54: if(!size())
55: return false;
56: for(int i = 0; i < size(); ++i)
57: cout << " " << t[i];
58: cout << endl;
59: return true;
60: }
61:
62: int main(void){
63: Mystack<int> s(20);
64: int i;
65: int& ii = i;
66: s.push(8);
67: s.pop(ii);
68: cout << s.size() << endl;
69: cout << ii << endl;
70: s.push(9);
71: s.push(10);
72: for(int i = 0; i < 23; i++)
73: s.push(0xf);
74: s.printall();
75: for(int i = 0; i < 25; i++)
76: s.pop(ii);
77: s.printall();
78: ifstream infile("test09.cpp");
79: ofstream outfile("mytext.txt");
80: string str;
81: vector<string> v;
82: while(getline(infile, str)){
83: v.push_back(str);
84: }
85: for(int i = 0; i < v.size(); i++)
86: outfile << i + 1 << ": " << v[i] << endl;
87: infile.close();
88: outfile.close();
89: }
*/