-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmain.cpp
64 lines (60 loc) · 1.4 KB
/
main.cpp
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
#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>
using namespace std;
template <typename CONTAINER, typename FUNC>
string join(const CONTAINER& c, FUNC f, const string& delim) {
stringstream ss;
for (auto it = c.begin(); it != c.end(); ++it) {
ss << (it == c.begin() ? "" : delim) << f(*it);
}
return ss.str();
}
int main()
{
std::vector<int> vet;
while (true)
{
string line, cmd;
getline(cin, line);
cout << "$" << line << '\n';
stringstream ss(line);
ss >> cmd;
if (cmd == "end")
{
break;
}
else if (cmd == "push")
{
int value{};
while (ss >> value)
{
vet.push_back(value);
}
}
else if (cmd == "show")
{
cout << "[" + join(vet, [](auto x){return x;}, ", ") + "]" << '\n';
}
else if (cmd == "erase")
{
int index{};
ss >> index;
vet.erase(vet.begin() + index);
}
else if (cmd == "media")
{
double sum = 0;
for (auto item : vet)
{
sum += item;
}
cout << fixed << setprecision(2) << sum / vet.size() << '\n';
}
else
{
cout << "fail: invalid command" << '\n';
}
}
}