-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzc.cpp
More file actions
62 lines (51 loc) · 1.77 KB
/
Copy pathzc.cpp
File metadata and controls
62 lines (51 loc) · 1.77 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
#include <iostream>
#include <fstream>
#include <string>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main(int argc, char* argv[]) {
// Проверяем, что передан путь к файлу
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <json_file_path>" << std::endl;
return 1;
}
std::string filePath = argv[1];
// Открываем и читаем файл
std::ifstream file(filePath);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filePath << std::endl;
return 1;
}
// Парсим JSON
json data;
try {
file >> data;
} catch (const json::parse_error& e) {
std::cerr << "Error parsing JSON: " << e.what() << std::endl;
return 1;
}
// Проверяем наличие поля "results"
if (!data.contains("results") || !data["results"].is_array()) {
std::cerr << "Error: JSON does not contain 'results' array" << std::endl;
return 1;
}
auto results = data["results"];
long long totalZeroCount = 0;
size_t count = 0;
// Суммируем все zeroCount
for (const auto& item : results) {
if (item.contains("zeroCount") && item["zeroCount"].is_number_integer()) {
totalZeroCount += item["zeroCount"].get<int>();
count++;
}
}
if (count == 0) {
std::cerr << "Error: No valid entries found in 'results'" << std::endl;
return 1;
}
// Вычисляем и выводим среднее значение
double average = static_cast<double>(totalZeroCount) / count;
std::cout << "Average zeroCount: " << average << std::endl;
std::cout << "Total entries: " << count << std::endl;
return 0;
}