Repository navigation
Expand file tree
/
Copy pathstackWithMaxFunc.cpp
More file actions
99 lines (93 loc) · 2.11 KB
/
Copy pathstackWithMaxFunc.cpp
File metadata and controls
99 lines (93 loc) · 2.11 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
#include <iostream>
#include <stack>
#include <ctime>
#include <cstdlib>
#include <string>
void findMaxElem(std::stack <int> &steck) {
int tempLenght = steck.size();
std::cout << "Size of steck is " << tempLenght << std::endl;
std::stack <int> tempSteak;
int maxElem{ 0 };
for (int count = 0; count < tempLenght; count++) {
if (steck.top() >= maxElem) {
maxElem = steck.top();
steck.pop();
tempSteak.push(maxElem);
}
else {
tempSteak.push(steck.top());
steck.pop();
}
}
std::cout << "Max elem of steck is " << maxElem << std::endl;
for (int count = 0; count < tempLenght; count++) {
steck.push(tempSteak.top());
tempSteak.pop();
}
}
void print(std::stack <int> &steck) {
int lenght = steck.size();
std::stack <int> tempSteak;
for (int count = 0; count < lenght; count++) {
std::cout << steck.top() << "\t";
tempSteak.push(steck.top());
steck.pop();
}
std::cout << std::endl;
for (int count = 0; count < lenght; count++) {
steck.push(tempSteak.top());
tempSteak.pop();
}
}
int main() {
setlocale(LC_ALL, "rus");
srand(0);
std::stack <int> steck;
int lenght{ 0 };
std::cout << "Введите кол-ов элементов в стэке: ";
std::cin >> lenght;
std::cout << "Ввести вручную? (y = ДА, n = НЕТ)" << std::endl;
std::string temp;
std::cin >> temp;
int tempint;
if (temp == "y") {
for (int count = 0; count < lenght; count++) {
std::cin >> tempint;
steck.push(tempint);
}
}
else if (temp == "n") {
for (int count = 0; count < lenght; count++) {
steck.push(rand());
}
}
print(steck);
findMaxElem(steck);
print(steck);
bool quest = true;
if (steck.size() == 1) {
return 0;
}
while (quest) {
if (steck.empty()) {
break;
}
char quest2;
std::cout << "\nВы хотите удалить последний элемент из стэка? (y = ДА, n = НЕТ)" << std::endl;
std::cin >> quest2;
if (quest2 == 'y') {
if (!steck.empty()) {
steck.pop();
print(steck);
findMaxElem(steck);
}
else if (steck.empty()) {
quest = false;
}
}
else if (quest2 == 'n') {
quest = false;
}
}
return 0;
}