-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTStack.h
More file actions
191 lines (156 loc) · 3.51 KB
/
TStack.h
File metadata and controls
191 lines (156 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#include <iostream>
#include <string>
using namespace std;
const int MAX_STACK_SIZE = 10000;
template <class T>
class TStack {
T* pMem;
int MaxSize, Num;
public:
TStack(int _MaxSize=10);
~TStack();
TStack(const TStack& st);
TStack& operator =(const TStack<T>& st);
bool operator == (const TStack& st) const;
bool operator != (const TStack& st) const;
bool empty() const;
bool full() const;
int getNum();
T pop();
T top() const;
void push(T val);
void clear();
bool checkThrow(string str);
// Ввод элемента в стек
friend istream& operator>>(istream& in, TStack& s)
{
T val;
cout << "Введите элементы стека (введите 0 для завершения): ";
while (in >> val && val != 0) {
s.Push(val);
}
return in;
}
// Вывод элемента из стека
friend ostream& operator<<(ostream& out, const TStack& s)
{
for (int i = 0; i <= s.Num; i++) {
out << s.pMem[i] << " ";
}
return out;
}
};
template <class T>
TStack<T>::TStack(int _MaxSize) {
if (_MaxSize<0 || _MaxSize>MAX_STACK_SIZE) throw string("incorrect size");
MaxSize = _MaxSize;
Num = -1;
pMem = new T[MaxSize];
}
template <class T>
TStack<T>::~TStack() {
delete[] pMem;
}
template<class T>
TStack<T>::TStack(const TStack<T>& st) {
if (st.MaxSize < 0 || st.MaxSize > MAX_STACK_SIZE || st.Num < -1 || st.Num >= st.MaxSize)
throw - 1;
MaxSize = st.MaxSize;
Num = st.Num;
pMem = new T[MaxSize];
for (int i = 0; i < Num+1; i++) {
pMem[i] = st.pMem[i];
}
}
template<class T>
TStack<T>& TStack<T>::operator =(const TStack<T>& st)
{
if (this != &st) {
if (MaxSize != st.MaxSize) {
MaxSize = st.MaxSize;
delete[] pMem;
pMem = new T[MaxSize];
}
Num = st.Num;
for (int i = 0; i < Num + 1; i++) pMem[i] = st.pMem[i];
}
return *this;
}
template<class T>
bool TStack<T>::operator==( const TStack& st) const
{
if (this == &st) return true;
if (MaxSize != st.MaxSize || Num != st.Num) return false;
for (int i = 0; i < Num + 1; i++) {
if (pMem[i] != st.pMem[i]) return false;
}
return true;
}
template<class T>
bool TStack<T>::operator!=(const TStack& st) const
{
return !(*this==st);
}
template<class T>
bool TStack<T>::full() const
{
if (Num == ((MaxSize) - 1)) return true;
return false;
}
template<class T>
bool TStack<T>::empty() const
{
if (Num == -1) return true;
return false;
}
template<class T>
T TStack<T>::pop() {
if (empty()) {
throw string("Stack is empty");
}
T tmp = pMem[Num];
Num--;
return tmp;
}
template<class T>
T TStack<T>::top() const
{
if (empty()) {
throw string("Stack is empty");
}
return pMem[Num];
}
template<class T>
void TStack<T>::push(T val) {
if (full()) throw string("Stack is full");
Num++;
pMem[Num] = val;
}
template<class T>
void TStack<T>::clear()
{
Num = -1;
}
template <class T>
int TStack<T>::getNum()
{
return Num;
}
template <class T>
bool TStack<T>::checkThrow(string str)
{
TStack<char> s;
for (char ch : str)
{
if (ch == '(')
s.push('(');
else if (ch == ')')
{
if (s.empty())
return false;
s.pop();
}
}
return s.empty();
}