klasscho

Untitled

Nov 26th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. package com.company;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9. public static void ReadFromFile(int[][] Matrixx, String FName, int Row, int Col) throws
  10. Exception {
  11. String line;
  12. String[] Numb;
  13. BufferedReader buff = new BufferedReader(new FileReader(FName));
  14. for (int i = 0; i <= Row; i++) {
  15. line = buff.readLine();
  16. Numb = line.split(" ");
  17. for (int j = 0; j <= Col; j++) {
  18. Matrixx[i][j] = Integer.parseInt(Numb[j]);
  19. }
  20. }
  21. buff.close();
  22. }
  23.  
  24. public static int SizeOfMatrix(String fileName) throws Exception {
  25. Boolean isFirst = true, isCorrect = true;
  26. int columnCount = 0, rowCount = 1;
  27. String line;
  28. String[] stringRow;
  29.  
  30. BufferedReader reader = new BufferedReader(new FileReader(fileName));
  31. line = reader.readLine();
  32. while (line != null && isCorrect) {
  33. stringRow = line.split(" ");
  34. rowCount++;
  35.  
  36. if (isFirst) {
  37. columnCount = stringRow.length;
  38. isFirst = false;
  39. }
  40.  
  41. if (!isFirst && stringRow.length != columnCount) {
  42. isCorrect = false;
  43. }
  44. line = reader.readLine();
  45. }
  46.  
  47. if (isCorrect && columnCount == rowCount) {
  48. return columnCount;
  49. }
  50. return 0;
  51. }
  52.  
  53. public static int MatrixRowSize(String FName) throws Exception {
  54. int count = 0;
  55. FileReader MyFile = new FileReader(FName);
  56. BufferedReader buff = new BufferedReader(MyFile);
  57. while (buff.readLine() != null) {
  58. count++;
  59. }
  60. MyFile.close();
  61. return (count);
  62. }
  63.  
  64. public static int MatrixColSize(String FName) throws Exception {
  65. String line;
  66. String[] Numb;
  67. int count = 0;
  68. FileReader MyFile = new FileReader(FName);
  69. BufferedReader buff = new BufferedReader(MyFile);
  70. line = buff.readLine();
  71. Numb = line.split(" ");
  72. for (int i = 0; i < Numb.length; i++)
  73. count++;
  74. MyFile.close();
  75. return (count);
  76. }
  77.  
  78. public static void WriteToFile(float Arr[], String FName, int Row, int Col) throws Exception {
  79. FileWriter FileOut = new FileWriter(FName);
  80. for (int i = 0; i <= Row; i++) {
  81. FileOut.write(Arr[i] + " ");
  82. }
  83. FileOut.write("\n");
  84. FileOut.close();
  85. System.out.println("Saved to file: " + FName);
  86. }
  87.  
  88. public static float[] Average(int[][] Matrixx, int Col, int Row) {
  89. float Sum, Amount;
  90. float[] Arr = new float[Col];
  91. for (int i = 0; i < Row; i++) {
  92. Sum = 0;
  93. Amount = 0;
  94. for (int j = 0; j < Col; j++) {
  95. if (Matrixx[j][i] > 0) {
  96. Sum += Matrixx[j][i];
  97. Amount += 1;
  98. }
  99. Arr[i] = Sum / Amount;
  100. }
  101.  
  102. }
  103. return Arr;
  104. }
  105.  
  106. public static void AverageOutput(int[] Arr) {
  107. float avg;
  108. for (int i = 0; i < Arr.length; i++){
  109. System.out.printf("%.4f ", Arr[i]);
  110. }
  111. }
  112.  
  113. public static void MatrixOutput(int[][] Matrixx, int Row, int Col){
  114. for (int i = 0; i <= Row; i++) {
  115. for (int j = 0; j <= Col; j++) {
  116. System.out.printf("%d ", Matrixx[i][j]);
  117. }
  118. System.out.println();
  119. }
  120. System.out.println();
  121. }
  122.  
  123. public static void main (String[]args) throws Exception {
  124. int[][] Matrixx;
  125. float[] Arr;
  126. int Rows, Cols;
  127. String FileNameIn, FileNameOut;
  128. Rows = MatrixRowSize(FileNameIn);
  129. Cols = MatrixColSize(FileNameIn);
  130. Matrixx = new int[Rows][Cols];
  131. Rows--;
  132. Cols--;
  133. ReadFromFile(Matrixx, FileNameIn, Rows, Cols);
  134. System.out.println();
  135. System.out.println("Given matrix::");
  136. MatrixOutput(Matrixx, Rows, Cols);
  137. Arr = Average;
  138. System.out.println("The result after addition:");
  139. WriteToFile(Matrixx, FileNameOut, Rows, Cols);
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment