Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package pkg33;
  2.  
  3. /**
  4.  *
  5.  * @author Acer
  6.  */
  7. import java.io.*;
  8. public class Main {
  9.     public static void main(String[] args) {
  10.         try
  11.     {
  12.             if (args.length != 2) throw new IncorrectLengthException();
  13.             InputStream fin = new FileInputStream(args[0]);
  14.             Matrix A = Matrix.input(fin);
  15.             fin.close();
  16.            A = Matrix.Func(A);
  17.             OutputStream fout = new FileOutputStream(args[1]);
  18.             A.output(fout);
  19.             fout.close();
  20.         }
  21.     catch (IncorrectLengthException Exc){System.out.println(Exc.getMessage());}
  22.         catch (FileNotFoundException Exc){System.out.println("Отсутствует входной файл!");}
  23.         catch (Exception Exc) {System.out.println(Exc.getMessage());}
  24.     }
  25. }
  26. class IncorrectLengthException extends Exception {
  27.     public IncorrectLengthException() {
  28.         super ("Неверное число аргументов!");
  29.     }
  30. }
  31.    
  32. /*
  33.  * To change this license header, choose License Headers in Project Properties.
  34.  * To change this template file, choose Tools | Templates
  35.  * and open the template in the editor.
  36.  */
  37. package pkg33;
  38.  
  39. /**
  40.  *
  41.  * @author Acer
  42.  */
  43. import java.io.DataInputStream;
  44. import java.io.DataOutputStream;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.io.OutputStream;
  48. import java.io.Serializable;
  49. import java.util.ArrayList;
  50. import java.util.Random;
  51.  
  52. public class Matrix implements Serializable{
  53.  
  54.     private static double[][] arr;
  55.     private static int m;
  56.     private static int n;
  57.  
  58.     Matrix(int m, int n) {
  59.  
  60.         Random rnd = new Random();
  61.         arr = new double[m][n];
  62.         for (int i = 0; i < m; i++)
  63.         {
  64.             for (int j = 0; j < n; j++)
  65.             {
  66.                 arr[i][j]=rnd.nextDouble();
  67.             }
  68.         }
  69.     }
  70.    
  71.     public int getCol(){
  72.         return n;
  73.     }
  74.     public int getRow(){
  75.         return m;
  76.     }
  77.  
  78.  
  79.     public void setArr(double value, int i, int j) {
  80.         arr[i][j] = value;
  81.     }
  82.  
  83.     public double getArr(int i, int j) {
  84.         return arr[i][j];
  85.     }
  86.  
  87. static Matrix Func(Matrix matr){
  88. ArrayList<Integer> row = new ArrayList<>();
  89. ArrayList<Integer> col = new ArrayList<>();
  90. for(int i=0; i<matr.m;i++){
  91. for(int j=0; j<matr.n;i++){
  92. if(matr.getArr(i,j) == 0){
  93. row.add(i);
  94. col.add(j);
  95. }
  96. }
  97. }
  98. for (int i = 0; i <row.size() ; i++) {
  99. for (int j = 0; j <matr.getCol() ; j++) {
  100. matr.setArr(row.get(i), j, 0);
  101. }
  102. }
  103. for (int i = 0; i <col.size() ; i++) {
  104. for (int j = 0; j <matr.m ; j++) {
  105. matr.setArr(j, col.get(i), 0);
  106. }
  107. } return matr;
  108. }
  109.         public void output(OutputStream out) throws IOException{
  110.     DataOutputStream Dout = new DataOutputStream(out);
  111.     Dout.writeInt(getRow());
  112.     Dout.writeInt(getCol());
  113.     for(int i = 0; i< getCol(); i++)
  114.             for(int j = 0; j< getRow(); j++)
  115.         Dout.writeDouble(arr[i][j]);
  116.     Dout.close();
  117.     }
  118.  
  119.     public static Matrix input(InputStream inp) throws IOException{
  120.     DataInputStream Dinp = new DataInputStream(inp);
  121.    
  122.     int n = Dinp.readInt();
  123.     int m = Dinp.readInt();
  124.         Matrix Mat = new Matrix(m,n);
  125.     for (int i = 0; i<m; i++)
  126.             for(int j = 0; j<n;j++)
  127.         Mat.setArr(Dinp.readDouble(),i,j);
  128.         Dinp.close();
  129.     return Mat;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement