Wklejam całość, chętnie usłyszę kilka słów krytyki. Aplikacja ma wyliczać średnią prędkość wiatru w danym miesiącu korzystając z danych pobieranych z pliku który znajduje się w załączniku. Dane wyświetlane mają być w formacie json. Dodam tylko, że to nie jest zadanie na jakies kolokwium itp.. Chciałbym się czegoś nauczyć, dlatego wymyślam sobie różne zadania.
FileFetcher
#ifndef FILEFETCHER_H
#define FILEFETCHER_H
#include <string>
#include <fstream>
#include <streambuf>
#include <memory>
class FileFetcher
{
public:
explicit FileFetcher(const std::string& filePath);
std::string& getRawString() const;
const std::string& getFilePath() const;
private:
const std::string filePath;
std::unique_ptr<std::string> rawString;
};
#endif // FILEFETCHER_H
#include "filefetcher.h"
#include <string>
#include <fstream>
#include <streambuf>
FileFetcher::FileFetcher(const std::string& path) : filePath(path)
{
std::ifstream t(filePath);
rawString = std::unique_ptr<std::string>{new std::string((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>())};
}
std::string& FileFetcher::getRawString() const
{
return *rawString;
}
const std::string& FileFetcher::getFilePath() const
{
return filePath;
}
AnalyzeData
#ifndef ANALYZEDATA_H
#define ANALYZEDATA_H
#include "filefetcher.h"
#include <vector>
#include <memory>
class AnalyzeData
{
public:
explicit AnalyzeData(const std::string& stringPtr);
void parseData(const std::string& date);
double calculateMean(const std::vector<double>& data);
double getMeanWindSpeed();
private:
const std::string& rawString;
std::vector<double> windSpeedVector;
};
#endif // ANALYZEDATA_H
#include "analyzedata.h"
#include <iostream>
#include <sstream>
#include <numeric>
#include <algorithm>
AnalyzeData::AnalyzeData(const std::string &stringPtr) : rawString(stringPtr) {}
void AnalyzeData::parseData(const std::string& inputDate)
{
std::istringstream* iss = new std::istringstream(rawString);
std::string line;
while (std::getline(*iss, line))
{
std::vector<std::string> tmp;
std::string date;
std::string restOfTheData;
std::stringstream ss(line);
std::getline(ss, date, '\t');
while (std::getline(ss, restOfTheData, '\t'))
{
tmp.push_back(restOfTheData);
}
if(date.find(inputDate) != std::string::npos)
{
double windSpeed(atof((tmp.back()).c_str()));
windSpeedVector.push_back(windSpeed);
}
}
}
double AnalyzeData::calculateMean(const std::vector<double>& data)
{
double sum = std::accumulate(data.begin(), data.end(), 0.0);
double mean = sum / data.size();
return mean;
}
double AnalyzeData::getMeanWindSpeed()
{
return calculateMean(windSpeedVector);
}
JSonParser
#ifndef JSONPARSER_H
#define JSONPARSER_H
#include <string>
#include <memory>
#include "analyzedata.h"
static const char * _json_format =
"{ \"%s\": {\n"
" \"windSpeed\": { \"mean\": %lf }\n"
"} }\n"
;
class JsonParser
{
public:
JsonParser(AnalyzeData& dataRef);
std::string getJsonData(const char* date,double windSpeed);
double getMeanWindSpeed() const;
template< typename... argv >
std::string stringf( const char* format, argv... args ) {
const size_t SIZE = std::snprintf( NULL, 0, format, args... );
std::string output;
output.resize(SIZE);
std::snprintf( &(output[0]), SIZE+1, format, args... );
return std::move(output);
}
private:
AnalyzeData& data;
};
#endif // JSONPARSER_H
#include "jsonparser.h"
JsonParser::JsonParser(AnalyzeData& dataRef) : data(dataRef)
{
}
std::string JsonParser::getJsonData(const char* date,double windSpeed)
{
std::string JsonData = stringf(_json_format,date,windSpeed, 0.00);
return std::move(JsonData);
}
double JsonParser::getMeanWindSpeed() const
{
return data.getMeanWindSpeed();
}
main.cpp
#include <iostream>
#include "filefetcher.h"
#include "analyzedata.h"
#include "jsonparser.h"
#include <memory>
using namespace std;
int main(int argc, char *argv[])
{
std::unique_ptr<FileFetcher> fd(new FileFetcher("Environmental_Data_Deep_Moor_2012.txt"));
std::unique_ptr<AnalyzeData> dataAnalizator(new AnalyzeData(fd->getRawString()));
dataAnalizator->parseData("2012_01_05");
JsonParser* jsonParser = new JsonParser(*dataAnalizator);
const std::string& output = jsonParser->getJsonData("2012_01_01",jsonParser->getMeanWindSpeed());
std::cout << output << std::endl;
return 0;
}