|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include"../matrix/matrix_pro.h"
- #include"../matrix/matrix_def.h"
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<typeinfo>
- #include<cstring>
- using namespace std;
- string data;
- double str2double(char *src)
- {
- double ret = 0,sign = 1;
- char *p = src;
- if(*p == '+'){sign = 1;p ++;}
- else if(*p == '-'){sign = -1; p ++; }
- while(*p && (*p != '.'))
- {
- ret*=10;
- ret += (*p) - '0';
- p++;
- }
- if(*p == '.')
- {
- double step = 0.1;
- p++;
- while(*p)
- {
- ret+=step*((*p)-'0');
- step/=10;
- p++;
- }
- }
- return ret*sign;
- }
-
- str_Matrix read_file(string &file_path)
- {
- int count_rows = 0,count_col = 0;
- ifstream infile;
- infile.open(file_path);
- while(!infile.eof())
- {
- count_col = 0;
- count_rows+=1;
- infile >> data;
- int iSize = data.size();
- int flag_if = 1;
- for(int i = 0;i < iSize; i++)
- {
- if(data[i]==',')
- {
- count_col++;
- }
- }
- }
- cout<<count_rows<<endl;
- cout<<count_col+1<<endl;
- str_Matrix data_ma = CreateStr_Ma(1666,1666);
- ifstream infile2;
- infile2.open(file_path);
- int next_flag = 0;
- while(!infile2.eof())
- {
- infile2 >> data;
- string base = "";
- string added ="";
- string added2 = "";
- string base2 = "";
- int iSize = data.size();
- int flag_if = 1;
- int count_times = 0;
- int count_times2 = 0;
- for(int i = 0;i < iSize; i++)
- {
- //cout<<data[i]<<endl;
- if(data[i]!=',')
- {
- added+=data[i];
- }
- else if(data[i]==',')
- {
- count_times+=1;
- base = added;
- char *result = (char*)base.data();
- //cout<<result<<endl;
- data_ma.str_matrix[next_flag][count_times-1] = result;
- added ="";
- base = "";
- }
- if(count_times==count_col)
- {
- added2+=data[i+1];
- base2 = added2;
- char *result2 = (char*)base2.data();
- //cout<<result2<<endl;
- data_ma.str_matrix[next_flag][count_times] = result2;
- }
- }
- next_flag+=1;
- }
- return data_ma;
- }
- Matrix read_csv(string &file_path)
- {
- int count_rows = 0,count_col = 0;
- ifstream infile;
- infile.open(file_path);
- while(!infile.eof())
- {
- count_col = 0;
- count_rows+=1;
- infile >> data;
- int iSize = data.size();
- int flag_if = 1;
- for(int i = 0;i < iSize; i++)
- {
- if(data[i]==',')
- {
- count_col++;
- }
- }
- }
- Matrix data_ma = CreateMatrix(count_rows,count_col+1);
- ifstream infile2;
- infile2.open(file_path);
- int next_flag = 0;
- while(!infile2.eof())
- {
- infile2 >> data;
- string base = "";
- string added ="";
- string added2 = "";
- string base2 = "";
- int iSize = data.size();
- int flag_if = 1;
- int count_times = 0;
- int count_times2 = 0;
- for(int i = 0;i < iSize; i++)
- {
- if(data[i]!=',')
- {
- added+=data[i];
- }
- else if(data[i]==',')
- {
- count_times+=1;
- base = added;
- char *result = (char*)base.data();
- data_ma.matrix[next_flag][count_times-1] = str2double(result);
- added ="";
- base = "";
- }
- if(count_times==count_col)
- {
- added2+=data[i+1];
- base2 = added2;
- char *result2 = (char*)base2.data();
- data_ma.matrix[next_flag][count_times] = str2double(result2);
- }
- }
- next_flag+=1;
- }
- return data_ma;
- }
- int save_txt(Matrix mid1,string path = "./",string delimiter = ",",string header="./")
- {
- int index_x,index_y;
- ofstream fout(path,ios::app);
- fout<<header<<endl;
- for(index_x=0;index_x<mid1.row;index_x++)
- {
- for(index_y=0;index_y<mid1.col;index_y++)
- {
- fout<<mid1.matrix[index_x][index_y]<<delimiter;
- }
- fout<<endl;
- }
- fout.close();
- return 0;
- }
- /*
- int read_file(string path = "./",string delimiter = ",",string header = "./")
- {
- ystring list_word[]=[];
- string *p = list_word;
- for(int i = 0;i<5;i++)
- {
- *(list_word+i) = 1;
- cout<<"*p("<<i<<"): ";
- cout<<*(p+i)<<endl;
- }
- }
- */
- //-----------split------------
- //
- //
|