C++笔记121210
写了一个类,主要目的是实现csv文件的读取和处理以及操作。包括计算收益率,按月合并,等等。
csv文件需满足:第一列为日期,且格式为yyyy-mm-dd。
1 //FileReader.h 2 3 #ifndef FILEREADER_H 4 #define FiLEREADER_H 5 #include<string> 6 #include<vector> 7 8 9 10 class FileReader 11 { 12 public: 13 14 enum Term { day, month }; 15 16 FileReader(const std::string &path, const char dlm = ',', Term mode = day); 17 void show() const; 18 19 /* Remove the last column */ 20 void CutLastColumn() 21 { 22 for(std::vector< std::vector<std::string> >::iterator ritr = content.begin(); ritr != content.end(); ++ritr) 23 { 24 ritr->pop_back(); 25 } 26 } 27 28 /* Add a new column to record returns of column_N */ 29 void AddReturn(std::vector<std::string>::size_type column_N, std::string returnLabel); 30 31 /* output to file in &path */ 32 void output(const std::string &path, const char dlm = ',') const; 33 34 private: 35 std::vector< std::vector<std::string> > content; 36 }; 37 38 #endif
源文件:
1 #include <fstream> 2 #include <sstream> 3 #include <iostream> 4 #include "FileReader.h" 5 6 using namespace std; 7 8 9 FileReader::FileReader(const string &path, const char dlm ,Term mode) 10 { 11 ifstream infile(path); 12 string line, tmp; 13 vector<string> empty; 14 if(mode == day) 15 while(infile.good()) 16 { 17 getline(infile, line, '\n'); 18 stringstream ss(line); 19 content.push_back(empty); 20 21 while(ss.good()) 22 { 23 getline(ss, tmp, dlm); 24 content.back().push_back(tmp); 25 } 26 } 27 else if(mode == month) 28 { 29 string line_lag; 30 while(infile.good()) 31 { 32 getline(infile, line, '\n'); 33 34 if(!line_lag.empty()) 35 if(line.at(6) != line_lag.at(6)) 36 { 37 stringstream ss(line_lag); 38 content.push_back(empty); 39 while(ss.good()) 40 { 41 getline(ss,tmp,dlm); 42 content.back().push_back(tmp); 43 } 44 } 45 line_lag = line; 46 } 47 } 48 infile.close(); 49 infile.clear(); 50 } 51 52 void FileReader::output(const string &path, const char dlm) const 53 { 54 ofstream out(path); 55 for(vector< vector<string> >::const_iterator itr = content.begin(); itr != content.end(); ++itr) 56 { 57 for(vector<string>::const_iterator subitr = itr->begin(); subitr != itr->end(); ++subitr) 58 out<<*subitr<<dlm<<flush; 59 out<<endl; 60 } 61 out.close(); 62 out.clear(); 63 64 } 65 66 /* Add a new column to record return */ 67 68 void FileReader::AddReturn(vector<string>::size_type column, string returnLabel = "Return") 69 { 70 double currentVal = 0, lagVal = 0, rtrn = 0; 71 string rtrnStr; 72 vector< vector<string> >::iterator ritr = content.begin(); 73 ritr->push_back(returnLabel); 74 for(; ritr != content.end(); ++ritr) 75 { 76 stringstream tmpStream; 77 tmpStream << ritr->at(column); 78 tmpStream >> currentVal; 79 tmpStream.clear(); 80 if(lagVal) 81 { 82 rtrn = (currentVal*100-lagVal*100)/lagVal; 83 tmpStream << rtrn; 84 tmpStream >> rtrnStr; 85 tmpStream.clear(); 86 ritr->push_back(rtrnStr); 87 } 88 lagVal = currentVal; 89 } 90 } 91 92 void FileReader::show() const 93 { 94 for(vector< vector<string> >::const_iterator itr = content.begin(); itr != content.end(); ++itr) 95 { 96 for(vector<string>::const_iterator subitr = itr->begin(); subitr != itr->end(); ++subitr) 97 cout<<*subitr<<" "<<flush; 98 cout<<endl; 99 } 100 }
其中string和double型的转换,文件操作均采用流操作实现。
测试:
1 //main.cpp 2 3 #include <iostream> 4 #include <Windows.h> 5 #include "FileReader.h" 6 using namespace std; 7 8 int main() 9 { 10 unsigned cnt = ::GetTickCount(); 11 FileReader::Term dataTerm = FileReader::month; 12 FileReader rdr("e:\\tmp\\tmpdata.csv", ',' , dataTerm); 13 cout<< ::GetTickCount() - cnt << " ms"<<endl; 14 15 cnt = ::GetTickCount(); 16 rdr.CutLastColumn(); 17 cout<< ::GetTickCount() - cnt << " ms"<<endl; 18 19 string addLabel; 20 if(dataTerm = FileReader::month) 21 addLabel = "月"; 22 addLabel += "涨跌幅(%)"; 23 24 cnt = ::GetTickCount(); 25 rdr.AddReturn(1,addLabel); 26 cout<< ::GetTickCount() - cnt << " ms"<<endl; 27 28 cnt = ::GetTickCount(); 29 rdr.output("e:\\tmp\\out_test.csv"); 30 cout<< ::GetTickCount() - cnt<< " ms"<<endl; 31 32 return 0; 33 }