Created
March 28, 2017 18:27
-
-
Save Voultapher/b9d01e469877aa7734422f45f9adac5d to your computer and use it in GitHub Desktop.
never do this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <algorithm> | |
#include <string> | |
#include <vector> | |
#include <sstream> | |
void foo(const char* filepath) | |
{ | |
std::ifstream file(filepath); | |
if (!file.is_open()) | |
{ | |
std::cerr << "Failed to open: " << filepath << '\n'; | |
} | |
std::string line; | |
std::getline(file, line); | |
size_t colcount = std::count(std::begin(line), std::end(line), ',') + 1; | |
std::vector<int> sums(colcount); | |
size_t rowcount{}; | |
for (; std::getline(file, line, '\n'); ++rowcount) | |
{ | |
std::stringstream stream; | |
stream << line; | |
std::string num; | |
for(size_t i{}; i < colcount; ++i) | |
{ | |
std::getline(stream, num, ','); | |
sums[i] += std::stoi(num); | |
} | |
} | |
size_t mean = 2; | |
size_t sum = 3; | |
std::cout << "1[MEAN]: " << (static_cast<double>(sums[mean - 1]) / rowcount) << '\n'; | |
std::cout << "2[SUM]: " << sums[sum - 1] << '\n'; | |
} | |
int main() | |
{ | |
constexpr auto filepath = | |
"/home/dev/projects/_cpp/_challenge/clean-code-competition17/task1/test.small"; | |
foo(filepath); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment