-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordCounter.cpp
42 lines (36 loc) · 1.09 KB
/
WordCounter.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
#include <iostream>
#include <fstream>
#include <string_view>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <filename>\n";
return 1;
}
const auto filename = argv[1];
std::ifstream file(filename);
if (!file) {
std::cerr << "Error: failed to open file " << filename << "\n";
return 1;
}
std::vector<int> numbers;
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
int number;
while (iss >> number) {
numbers.push_back(number);
}
}
auto minmax = std::minmax_element(numbers.begin(), numbers.end());
auto sum = std::accumulate(numbers.begin(), numbers.end(), 0);
auto average = static_cast<double>(sum) / numbers.size();
std::cout << "Min: " << *minmax.first << "\n";
std::cout << "Max: " << *minmax.second << "\n";
std::cout << "Sum: " << sum << "\n";
std::cout << "Average: " << average << "\n";
return 0;
}