You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

data_read.h 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include"../matrix/matrix_pro.h"
  2. #include"../matrix/matrix_def.h"
  3. #include<iostream>
  4. #include<fstream>
  5. #include<string>
  6. #include<typeinfo>
  7. #include<cstring>
  8. using namespace std;
  9. string data;
  10. double str2double(char *src)
  11. {
  12. double ret = 0,sign = 1;
  13. char *p = src;
  14. if(*p == '+'){sign = 1;p ++;}
  15. else if(*p == '-'){sign = -1; p ++; }
  16. while(*p && (*p != '.'))
  17. {
  18. ret*=10;
  19. ret += (*p) - '0';
  20. p++;
  21. }
  22. if(*p == '.')
  23. {
  24. double step = 0.1;
  25. p++;
  26. while(*p)
  27. {
  28. ret+=step*((*p)-'0');
  29. step/=10;
  30. p++;
  31. }
  32. }
  33. return ret*sign;
  34. }
  35. str_Matrix read_file(string &file_path)
  36. {
  37. int count_rows = 0,count_col = 0;
  38. ifstream infile;
  39. infile.open(file_path);
  40. while(!infile.eof())
  41. {
  42. count_col = 0;
  43. count_rows+=1;
  44. infile >> data;
  45. int iSize = data.size();
  46. int flag_if = 1;
  47. for(int i = 0;i < iSize; i++)
  48. {
  49. if(data[i]==',')
  50. {
  51. count_col++;
  52. }
  53. }
  54. }
  55. cout<<count_rows<<endl;
  56. cout<<count_col+1<<endl;
  57. str_Matrix data_ma = CreateStr_Ma(1666,1666);
  58. ifstream infile2;
  59. infile2.open(file_path);
  60. int next_flag = 0;
  61. while(!infile2.eof())
  62. {
  63. infile2 >> data;
  64. string base = "";
  65. string added ="";
  66. string added2 = "";
  67. string base2 = "";
  68. int iSize = data.size();
  69. int flag_if = 1;
  70. int count_times = 0;
  71. int count_times2 = 0;
  72. for(int i = 0;i < iSize; i++)
  73. {
  74. //cout<<data[i]<<endl;
  75. if(data[i]!=',')
  76. {
  77. added+=data[i];
  78. }
  79. else if(data[i]==',')
  80. {
  81. count_times+=1;
  82. base = added;
  83. char *result = (char*)base.data();
  84. //cout<<result<<endl;
  85. data_ma.str_matrix[next_flag][count_times-1] = result;
  86. added ="";
  87. base = "";
  88. }
  89. if(count_times==count_col)
  90. {
  91. added2+=data[i+1];
  92. base2 = added2;
  93. char *result2 = (char*)base2.data();
  94. //cout<<result2<<endl;
  95. data_ma.str_matrix[next_flag][count_times] = result2;
  96. }
  97. }
  98. next_flag+=1;
  99. }
  100. return data_ma;
  101. }
  102. Matrix read_csv(string &file_path)
  103. {
  104. int count_rows = 0,count_col = 0;
  105. ifstream infile;
  106. infile.open(file_path);
  107. while(!infile.eof())
  108. {
  109. count_col = 0;
  110. count_rows+=1;
  111. infile >> data;
  112. int iSize = data.size();
  113. int flag_if = 1;
  114. for(int i = 0;i < iSize; i++)
  115. {
  116. if(data[i]==',')
  117. {
  118. count_col++;
  119. }
  120. }
  121. }
  122. Matrix data_ma = CreateMatrix(count_rows,count_col+1);
  123. ifstream infile2;
  124. infile2.open(file_path);
  125. int next_flag = 0;
  126. while(!infile2.eof())
  127. {
  128. infile2 >> data;
  129. string base = "";
  130. string added ="";
  131. string added2 = "";
  132. string base2 = "";
  133. int iSize = data.size();
  134. int flag_if = 1;
  135. int count_times = 0;
  136. int count_times2 = 0;
  137. for(int i = 0;i < iSize; i++)
  138. {
  139. if(data[i]!=',')
  140. {
  141. added+=data[i];
  142. }
  143. else if(data[i]==',')
  144. {
  145. count_times+=1;
  146. base = added;
  147. char *result = (char*)base.data();
  148. data_ma.matrix[next_flag][count_times-1] = str2double(result);
  149. added ="";
  150. base = "";
  151. }
  152. if(count_times==count_col)
  153. {
  154. added2+=data[i+1];
  155. base2 = added2;
  156. char *result2 = (char*)base2.data();
  157. data_ma.matrix[next_flag][count_times] = str2double(result2);
  158. }
  159. }
  160. next_flag+=1;
  161. }
  162. return data_ma;
  163. }
  164. int save_txt(Matrix mid1,string path = "./",string delimiter = ",",string header="./")
  165. {
  166. int index_x,index_y;
  167. ofstream fout(path,ios::app);
  168. fout<<header<<endl;
  169. for(index_x=0;index_x<mid1.row;index_x++)
  170. {
  171. for(index_y=0;index_y<mid1.col;index_y++)
  172. {
  173. fout<<mid1.matrix[index_x][index_y]<<delimiter;
  174. }
  175. fout<<endl;
  176. }
  177. fout.close();
  178. return 0;
  179. }
  180. /*
  181. int read_file(string path = "./",string delimiter = ",",string header = "./")
  182. {
  183. ystring list_word[]=[];
  184. string *p = list_word;
  185. for(int i = 0;i<5;i++)
  186. {
  187. *(list_word+i) = 1;
  188. cout<<"*p("<<i<<"): ";
  189. cout<<*(p+i)<<endl;
  190. }
  191. }
  192. */
  193. //-----------split------------
  194. //
  195. //

Edge : 一个开源的科学计算引擎