include iostream include string include stack include vector using nam

 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
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
int main() {
stack<int> A, B;
cout << "Size of the first stack: ";
int sizeFirst;
cin >> sizeFirst;
cout << "Stack A = ";
for (int i = 0; i < sizeFirst; i++) {
int random = rand() % 100 - 50;
cout << random << " ";
A.push(random);
}
cout << endl;
cout << "Size of the second stack: ";
int sizeSecond;
cin >> sizeSecond;
cout << "Stack B = ";
for (int i = 0; i < sizeSecond; i++) {
int random = rand() % 100 - 50;
cout << random << " ";
B.push(random);
}
cout << endl;
vector<int> halfA, halfB;
cout << "The first half of stack A: ";
for (int i = 0; i < sizeFirst / 2; i++) {
halfA.push_back(A.top());
cout << halfA[i] << " ";
A.pop();
}
cout << endl;
cout << "The first half of stack B: ";
for (int i = 0; i < sizeSecond / 2; i++) {
halfB.push_back(B.top());
cout << halfB[i] << " ";
B.pop();
}
for (int i = 0; i < halfA.size(); i++) {
B.push(halfA[i]);
}
for (int i = 0; i < halfB.size(); i++) {
A.push(halfB[i]);
}
cout << "Stack A: ";
while(!A.empty()) {
cout << A.top() << " ";
A.pop();
}
cout << endl;
cout << "Stack B: ";
while(!B.empty()) {
cout << B.top() << " ";
B.pop();
}
cout << endl;
system("pause");
return 0;
}