Odpowiedź :
Odpowiedź:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
void makeClients
(std::vector<std::pair<std::string, int>> &clients, const std::vector<std::string> &clientDatas,
const std::vector<int> &averageUsage) {
std::pair<std::string, int> clientData;
for (int i = 0; i < clientDatas.size(); i++) {
clientData.first = clientDatas[i];
clientData.second = averageUsage[i];
clients.push_back(clientData);
}
}
int countAverage(std::string &s) {
std::istringstream ss(s);
std::string number;
int sum = 0;
int size = 0;
while (std::getline(ss, number, ';')) {
sum += std::stoi(number);
size++;
}
return sum / size;
}
bool findIf5Digits(const std::string &a) {
auto beginIt = std::find_if(a.begin(), a.end(), [](char b) { return b != '0'; });
auto endIt = std::find_if(a.begin(), a.end(), [](char b) { return std::isalpha(b); });
return std::distance(beginIt, endIt) == 5;
}
int main() {
std::fstream fs;
fs.open(R"(F:\CLionProjects\gctyc\wodociagi.txt)");
if (!fs.is_open()) {
std::cout << "Nie udalo sie otworzyc pliku" << std::endl;
} else {
std::cout << "Udalo sie otworzyc plik" << std::endl;
}
std::vector<std::string> lines;
std::vector<std::string> clientDatas;
std::vector<int> averageUsage;
std::vector<std::string> clientWith5digits;
std::string line;
std::string clientData;
int counter = 0;
while (fs >> line) {
auto it = line.find(';');
clientData = line.substr(0, it);
auto average = line.substr(it + 1);
if (counter != 0) {
clientDatas.push_back(clientData);
averageUsage.push_back(countAverage(average));
}
if (findIf5Digits(clientData) && counter != 0) {
clientWith5digits.push_back(clientData);
}
counter++;
}
// for (const auto &client: clientWith5digits) {
// std::cout << client << std::endl;
// }
//
// for (auto &client: clientDatas) {
// std::cout << client << std::endl;
// }
//
// for (int average: averageUsage) {
// std::cout << average << std::endl;
// }
std::vector<std::pair<std::string, int>> clients;
makeClients(clients, clientDatas, averageUsage);
for (const auto &data: clients) {
std::cout << data.first << " " << data.second << std::endl;
}
return 0;
}
Wyjaśnienie:
W sumie już większość brudnej roboty zrobione. Jak będzie mi się chciało, to to dokończę. Albo Ty możesz