C++练手

本文介绍了一个通用模板栈类的实现,并展示了如何使用该类进行数据的压入与弹出操作。此外,还通过文件读取与输出的方式实现了字符串向量的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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: }
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值