标签:
1 package test.admin; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileFilter; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.util.ArrayList; 10 import java.util.List; 11 import java.util.Scanner; 12 13 public class Algorithm { 14 15 // 每列的和 16 public static double one_row = 0,two_row = 0,three_row= 0,four_row=0,five_row=0,six_row=0; 17 // 每列的除数 18 public static double one_row_i = 0,two_row_i = 0,three_row_i=0,four_row_i=0,five_row_i=0,six_row_i=0; 19 // 每列的结果 20 public static double one_column = 0,two_column = 0,three_column= 0,four_column=0,five_column=0,six_column=0; 21 // 读取txt计算(加法与除法)四种样式 最后的结果 22 public static String one_style = "",two_style="",three_style="",four_style=""; 23 24 // 每列的和 25 public static double one_row_divisions = 0,two_row_divisions = 0,three_row_divisions= 0,four_row_divisions=0; 26 // 每列的除数 27 public static double one_row_j = 0,two_row_j = 0,three_row_j=0,four_row_j=0; 28 // 读取txt计算(乘法与触发)四种样式 最后的结果 29 public static String one_division = "",two_division="",three_division="",four_division=""; 30 31 public static double one = 0,two=0,three=0,four=0,five=0,six=0; 32 33 public static double one_s = 0,two_s=0,three_s=0,four_s=0; 34 35 public static double one_i = 0,two_i=0,three_i=0,four_i=0,five_i=0,six_i=0; 36 37 static List<String> listArr=new ArrayList<String>(); 38 // 多个文件的集合 39 static List<String> listAddArr=new ArrayList<String>(); 40 41 static String[] retArr = new String[4]; 42 static String[] retOne = null; 43 static String[] retTwo = null; 44 static String[] retThree = null; 45 static String[] retFour = null; 46 static String[] divisionArr = null; 47 static String[] divisionName = null; 48 static String[] splitValue = null; 49 static String value=null; 50 static String lineValue=""; 51 52 private static BufferedReader brf; 53 private static FileWriter fw; 54 55 /** 56 * 获取目录下的所有TXT文件 57 */ 58 public static List<String> getFileList(File file) { 59 60 List<String> result = new ArrayList<String>(); 61 62 if (!file.isDirectory()) { 63 System.out.println(file.getAbsolutePath()); 64 result.add(file.getAbsolutePath()); 65 } else { 66 File[] directoryList = file.listFiles(new FileFilter() { 67 public boolean accept(File file) { 68 if (file.isFile() && file.getName().indexOf("txt") > -1) { 69 return true; 70 } else { 71 return false; 72 } 73 } 74 }); 75 for (int i = 0; i < directoryList.length; i++) { 76 result.add(directoryList[i].getPath()); 77 } 78 } 79 80 return result; 81 } 82 83 public static void main(String[] args) throws Exception { 84 String centerValue=""; 85 int Js = 0; 86 int Js_two = 0; 87 System.out.print("请输入文件读取路径:"); 88 Scanner sc = new Scanner(System.in); 89 String strv = sc.next(); 90 // 向量相加的 文件位置 91 String FILE_IN = strv; 92 File f = new File(FILE_IN); 93 List<String> list = new ArrayList<String>(); 94 // 调用方法获取txt文件集合与路径 95 list = getFileList(f); 96 // 如果向量相加 目录下 有两个TXT文件 需要做第二次向量相加 操作 97 retOne = new String[list.size()]; 98 retTwo = new String[list.size()]; 99 retThree = new String[list.size()]; 100 retFour = new String[list.size()]; 101 // 循环调用 目录下所有TXT文件 102 for (String l : list) { 103 brf = new BufferedReader(new FileReader(new File(l))); 104 String line = null; 105 // 读取每一行 内容 106 while ((line = brf.readLine()) != null) { 107 String str = line.substring(line.length() - 9, 108 line.length()); 109 // 根据 四种分类不同做不同的操作 110 if (str.equals("[1,0,0,0]")) { 111 // 获取 第一种类型的 结果 112 one_style = Algorithm.VectorAdd(line); 113 } else if (str.equals("[0,1,0,0]")){ 114 // 获取 第二种类型的 结果 115 two_style = Algorithm.VectorAdd(line); 116 }else if (str.equals("[0,0,1,0]")){ 117 // 获取 第三种类型的 结果 118 three_style = Algorithm.VectorAdd(line); 119 }else if (str.equals("[0,0,0,1]")){ 120 // 获取 第四种类型的 结果 121 four_style = Algorithm.VectorAdd(line); 122 } 123 } 124 // 第二次向量相加并做除法的 集合 125 retOne[Js] = one_style; 126 retTwo[Js] = two_style; 127 retThree[Js] = three_style; 128 retFour[Js] = four_style; 129 Js++; 130 // 重置 one_style two_style 等 参数 131 resetStyle(); 132 // 重置 one_row 等 参数 133 resetParameters(); 134 } 135 // 调用 第二个向量加法 136 one_style = Algorithm.VectorAdds(retOne); 137 // 重置 one_row 等 参数 138 resetParameters(); 139 two_style = Algorithm.VectorAdds(retTwo); 140 resetParameters(); 141 three_style = Algorithm.VectorAdds(retThree); 142 resetParameters(); 143 four_style = Algorithm.VectorAdds(retFour); 144 resetParameters(); 145 146 System.out.println(one_style+"===[1,0,0,0]"); 147 System.out.println(two_style+"===[0,1,0,0]"); 148 System.out.println(three_style+"===[0,0,1,0]"); 149 System.out.println(four_style+"===[0,0,0,1]"); 150 // 向量点乘 的集合 151 retArr[0] = one_style; 152 retArr[1] = two_style; 153 retArr[2] = three_style; 154 retArr[3] = four_style; 155 156 System.out.print("请输入文件读取路径:"); 157 Scanner sc1 = new Scanner(System.in); 158 String strv1 = sc1.next(); 159 // 向量点乘 的文件位置 160 String FILE_INFO = strv1; 161 File f1 = new File(FILE_INFO); 162 List<String> list1 = new ArrayList<String>(); 163 // 调用方法获取txt文件集合与路径 164 list1 = getFileList(f1); 165 divisionArr = new String[list1.size()]; 166 divisionName = new String[list1.size()]; 167 for (int i = 0; i < list1.size(); i++) { 168 String[] split = list1.get(i).split("\\\\"); 169 String txtName = split[split.length-1]; 170 divisionName[i] = txtName.substring(0, txtName.length()-4); 171 } 172 for (String l1 : list1) { 173 brf = new BufferedReader(new FileReader(new File(l1))); 174 String line = null; 175 // 读取每一行内容 176 while ((line = brf.readLine()) != null) { 177 //调用点乘方法 178 one_division = Algorithm.VectorDivision(line,retArr); 179 centerValue+=one_division; 180 } 181 182 centerValue = centerValue.substring(0,centerValue.length()-6); 183 divisionArr[Js_two]=centerValue; 184 centerValue=""; 185 Js_two++; 186 } 187 // 向量点乘结果 做向量加法 188 for (int i = 0; i < divisionArr.length; i++) { 189 value = divisionArr[i]; 190 splitValue = value.split("\\[diff\\]"); 191 value = value.substring(value.length()); 192 for (int j = 0; j < splitValue.length; j++) { 193 String oneArr = splitValue[j]; 194 String[] split = oneArr.split(","); 195 for (int n = 0; n < split.length; n++) { 196 // 取值 197 double values = Double.valueOf(split[n]).doubleValue(); 198 if(n==0){ 199 one_row_divisions +=values; 200 one_row_j++; 201 } else if (n==1) { 202 two_row_divisions +=values; 203 two_row_j++; 204 } else if (n==2){ 205 three_row_divisions +=values; 206 three_row_j++; 207 } else if (n==3){ 208 four_row_divisions+=values; 209 four_row_j++; 210 } 211 } 212 } 213 214 one_row_divisions = one_row_divisions / one_row_j; 215 two_row_divisions = two_row_divisions / two_row_j; 216 three_row_divisions = three_row_divisions / three_row_j; 217 four_row_divisions = four_row_divisions / four_row_j; 218 //打印最终结果 219 System.out.println(one_row_divisions+","+two_row_divisions+","+three_row_divisions+","+four_row_divisions); 220 one_s = (one_row_divisions+two_row_divisions+two_row_divisions+four_row_divisions)/one_row_divisions; 221 two_s = (one_row_divisions+two_row_divisions+two_row_divisions+four_row_divisions)/two_row_divisions; 222 three_s = (one_row_divisions+two_row_divisions+two_row_divisions+four_row_divisions)/three_row_divisions; 223 four_s = (one_row_divisions+two_row_divisions+two_row_divisions+four_row_divisions)/four_row_divisions; 224 lineValue+="["+divisionName[i]+"]["+one_s+","+two_s+","+three_s+","+four_s+"]\r\n";; 225 resetStyle(); 226 } 227 System.out.print("请输入文件输出路径:"); 228 Scanner sc2 = new Scanner(System.in); 229 String strv2 = sc2.next(); 230 fw = new FileWriter(strv2+"\\file.txt"); 231 BufferedWriter bf = new BufferedWriter(fw); 232 bf.write(lineValue); 233 bf.flush(); 234 } 235 236 237 /** 238 * 向量点乘方法 239 */ 240 private static String VectorDivision(String line, String[] addArr) { 241 double one_row_division=0; 242 double two_row_division=0; 243 double three_row_division=0; 244 double four_row_division=0; 245 String ret=""; 246 String[] strArr = line.split("\\]\\["); 247 String subs = strArr[1].substring(0, strArr[1].length()-1); 248 String[] split = subs.split(","); 249 for (int i = 0; i < addArr.length; i++) { 250 String[] split2 = addArr[i].split(","); 251 for (int j = 0; j < split2.length; j++) { 252 double file2value = Double.valueOf(split[j]).doubleValue(); 253 double one_style2value = Double.valueOf(split2[j]).doubleValue(); 254 if(i==0){ 255 one_row_division += file2value*one_style2value; 256 } else if(i==1){ 257 two_row_division += file2value*one_style2value; 258 } else if(i==2){ 259 three_row_division += file2value*one_style2value; 260 } else if(i==3){ 261 four_row_division += file2value*one_style2value; 262 } 263 } 264 } 265 ret=one_row_division+","+two_row_division+","+three_row_division+","+four_row_division+"[diff]"; 266 return ret; 267 } 268 269 /** 270 * 向量加法 271 */ 272 static String VectorAdd(String line) { 273 274 String[] strArr = line.split("\\]\\["); 275 276 String[] numArr = strArr[1].split(","); 277 for (int i = 0; i < numArr.length; i++) { 278 double doubleValue = Double.valueOf(numArr[i]).doubleValue(); 279 if(i==0){ 280 one_row +=doubleValue; 281 one_row_i++; 282 } else if (i==1) { 283 two_row +=doubleValue; 284 two_row_i++; 285 } else if (i==2){ 286 three_row +=doubleValue; 287 three_row_i++; 288 } else if (i==3){ 289 four_row+=doubleValue; 290 four_row_i++; 291 } else if (i==4){ 292 five_row+=doubleValue; 293 five_row_i++; 294 } else if (i==5){ 295 six_row+=doubleValue; 296 six_row_i++; 297 } 298 299 } 300 one_column = one_row / one_row_i; 301 two_column = two_row / two_row_i; 302 three_column = three_row / three_row_i; 303 four_column = four_row / four_row_i; 304 five_column = five_row / five_row_i; 305 six_column = six_row / six_row_i; 306 String result = one_column+","+two_column+","+three_column+","+four_column+","+five_column+","+six_column; 307 return result; 308 } 309 /** 310 * 两个TXT文件调用向量加法 311 */ 312 static String VectorAdds(String[] line) { 313 for (int i = 0; i < line.length; i++) { 314 if(line[i]!=""){ 315 String[] split = line[i].split(","); 316 for (int j = 0; j < split.length; j++) { 317 // 取值 318 double value = Double.valueOf(split[j]).doubleValue(); 319 if(j==0){ 320 one +=value; 321 one_i++; 322 } else if (j==1) { 323 two +=value; 324 two_i++; 325 } else if (j==2){ 326 three +=value; 327 three_i++; 328 } else if (j==3){ 329 four+=value; 330 four_i++; 331 } else if (j==4){ 332 five+=value; 333 five_i++; 334 } else if (j==5){ 335 six+=value; 336 six_i++; 337 } 338 } 339 } 340 } 341 342 one_column = one / one_i; 343 two_column = two / two_i; 344 three_column = three / three_i; 345 four_column = four / four_i; 346 five_column = five / five_i; 347 six_column = six / six_i; 348 String result = one_column+","+two_column+","+three_column+","+four_column+","+five_column+","+six_column; 349 return result; 350 } 351 352 /** 353 * 重置参数 354 * @return 355 */ 356 static void resetParameters() { 357 one_row_i = 0; 358 two_row_i = 0; 359 three_row_i=0; 360 four_row_i=0; 361 five_row_i=0; 362 six_row_i=0; 363 one_row = 0; 364 two_row = 0; 365 three_row=0; 366 four_row=0; 367 five_row=0; 368 six_row=0; 369 one_i = 0; 370 two_i = 0; 371 three_i=0; 372 four_i=0; 373 five_i=0; 374 six_i=0; 375 one = 0; 376 two = 0; 377 three=0; 378 four=0; 379 five=0; 380 six=0; 381 } 382 383 /** 384 * 重置参数 385 * @return 386 */ 387 static void resetStyle(){ 388 splitValue=null; 389 value=null; 390 one_style=""; 391 two_style=""; 392 three_style=""; 393 four_style=""; 394 one_row_divisions=0; 395 two_row_divisions=0; 396 three_row_divisions=0; 397 four_row_divisions=0; 398 one_row_j=0; 399 two_row_j=0; 400 three_row_j=0; 401 four_row_j=0; 402 } 403 404 }
第一个文件路径 是计算向量相加(最后一个[]中 数据想通的做相加)
第二个文件路径 是计算向量点乘
第三个文件路径 是输出结果!
标签:
原文地址:http://www.cnblogs.com/sean-lee/p/5626405.html