stl stack sn

 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
// stack.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stack>
#include <vector>
using namespace std;
void printstack(stack<int> &);
float AverageINT(int, int);
void Process(stack<int> &, float);
int _tmain(int argc, _TCHAR* argv[])
{
int quentity, number, sum = 0;
float average;
stack<int> s;
cout << "Enter the number of stacks: ";
cin >> quentity;
for (int i = 0; i < quentity; i++)
{
cout << "[" << i + 1 << "]: ";
cin >> number;
sum += number;
s.push(number);
}
average = sum / quentity;
cout << "The stack before processing: ";
printstack(s);
cout << "Average: " << average << endl;
Process(s, average);
_getch();
return 0;
}
void Process(stack<int> &s, float average)
{
while (!s.empty())
{
if (((float)s.top()) < average)
cout << s.top() << " ";
s.pop();
}
}
void printstack(stack<int> &s)
{
vector<int> tmp;
while (!s.empty()) {
std::cout << s.top() << ' ';
tmp.push_back(s.top());
s.pop();
}
for (int i = tmp.size() - 1; i >= 0; --i)
{
s.push(tmp[i]);
}
cout << endl;
}