ts-aggregator/project_template/File.cpp
2022-12-13 12:36:06 +04:00

118 lines
3.3 KiB
C++

//
// Êëàññ äëÿ ðàáîòû ñ ôàéëàìè: ÷òåíèÿ âðåìåííîãî ðÿäà, çàïèñü ðåçóëüòàòîâ
//
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include "File.h"
#include <math.h>
using namespace std;
// ïðî÷èòàòü âðåìåííîé ðÿä èç ôàéëà
// ôîðìàò ôàéëà: âåùåñòâåííûå ÷èñëà, êàæäîå íàõîäèòñÿ íà íîâîé ñòðîêå, ðàçäåëèòåëü äðîáíîé ÷àñòè - çàïÿòàÿ
vector<double> File::readFile(string tsFileName) {
vector<double> x; // ñþäà óïàäåò çàãðóæåííûé âðåìåííîé ðÿä
std::ifstream inFile; // ïîòîê äëÿ ÷òåíèÿ ôàéëà
inFile.open(tsFileName.c_str());
string tsString = "";
while(inFile >> tsString) {
string str = tsString;
int pos = tsString.find(".");
if (pos > 0) {
str = tsString.replace(tsString.find("."), 1, ",");
}
x.push_back(atof(str.c_str()));
}
inFile.close();
return x;
}
vector<double> File::readKonfFile(string tsFileName) {
vector<double> x; // ñþäà óïàäåò çàãðóæåííûé âðåìåííîé ðÿä
int forecastCountDots = 1;
string TSPeriod = "daily"; // day, mounth, year....? êàê ïðåäñêàçàíèå çàâèñèò îò âðåìåííîãî ïåðèîäà
string TSName = ""; // day, mounth, year....? êàê ïðåäñêàçàíèå çàâèñèò îò âðåìåííîãî ïåðèîäà
std::ifstream inFile; // ïîòîê äëÿ ÷òåíèÿ ôàéëà
inFile.open(tsFileName.c_str());
string tsString = "";
while (inFile >> tsString) {
int endpos = tsString.find(';');
int startpos = 0;
if (endpos > 0){
if (TSName == ""){
//first string with params
//endpos = tsString.find(';');
//set ts name
TSName = tsString.substr(0, endpos);
startpos = endpos;
//set ts name
endpos = tsString.find(';', startpos + 1);
forecastCountDots = atoi(tsString.substr(startpos + 1, endpos - startpos - 1).c_str());
startpos = endpos;
//set ts name
endpos = tsString.find(';', startpos + 1);
TSPeriod = tsString.substr(startpos + 1 , endpos - startpos - 1);
startpos = endpos;
endpos = tsString.find(';', startpos + 1);
}
//parse ts data
while (endpos > 0){
x.push_back(atof(tsString.substr(startpos + 1, endpos - startpos -1).c_str()));
startpos = endpos;
endpos = tsString.find(';', startpos + 1);
}
}
/*string str = tsString;
int pos = tsString.find(".");
if (pos > 0) {
str = tsString.replace(tsString.find("."), 1, ",");
}
x.push_back(atof(str.c_str()));*/
}
inFile.close();
return x;
}
double File::round (double value) {
return floor(value + 0.5);
}
//îêðóãëèòü äî äâóõ çíàêîâ ïîñëå çàïÿòîé
string File::myRound(double num) {
double n = ((double) round(num * 100)) / 100;
std::ostringstream sstream;
sstream << n;
string s = sstream.str();
// çàìåíèòü ðàçäåëèòåëü äðîáíîé ÷àñòè íà çàïÿòóþ
int commaIndex = s.find(".");
if (commaIndex > 0) {
s = s.replace(commaIndex, 1, ",");
}
return s;
}
// ñîõðàíèòü â ôàéë âåêòîð ñ ðåçóëüòàòîì ïðîãíîçà
void File::writeFile(string outFileName, vector<double> result) {
ofstream outFile(outFileName.c_str(), std::ofstream::out | std::ofstream::trunc);
for (unsigned int i = 0; i < result.size(); i++) {
outFile << myRound(result[i]) << "\n";
}
outFile.close();
}
// ñîõðàíèòü â ôàéë ïðî÷èå ðåçóëüòàòû ïðîãíîçèðîâàíèÿ, íàïðèìåð îöåíêè
void File::writeFile(string outFileName, string key, double value) {
ofstream outFile(outFileName.c_str(), ios::app);
outFile << key << ";" << myRound(value) << "\n";
outFile.close();
}