// // Класс для работы с файлами: чтения временного ряда, запись результатов // #include "stdafx.h" #include #include #include #include #include #include "File.h" #include using namespace std; // прочитать временной ряд из файла // формат файла: вещественные числа, каждое находится на новой строке, разделитель дробной части - запятая vector File::readFile(string tsFileName) { vector 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 File::readKonfFile(string tsFileName) { vector 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 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(); }