Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class StrumienB
  6. {
  7. int rozm1;
  8. int rozm2;
  9. double macierz [][];
  10.  
  11.  
  12. public StrumienB(int rozm1, int rozm2)
  13. {
  14.     this.rozm1=rozm1;
  15.     this.rozm2=rozm2;
  16.    
  17.     macierz=new double [rozm1][rozm2];
  18.  
  19.    
  20. }
  21. void wypelnijmacierz()
  22. { int zakresrzeczywistych=5;
  23.     { Random liczbaWierszy = new Random();
  24.            
  25.         for(int i=0;i<rozm1;i++)
  26.         {
  27.             for(int j=0; j<rozm2;j++)
  28.             {
  29.                 macierz[i][j]=zakresrzeczywistych*liczbaWierszy.nextDouble();
  30.            
  31.        
  32.             }  
  33.    
  34.         }
  35.     }
  36. }
  37.  
  38. public void wydruk()
  39. {
  40.    
  41.         for(int i=0;i<rozm1;i++)
  42.         {   PrintWriter wyj = new PrintWriter(System.out, true);
  43.             for(int j=0; j<rozm2;j++)
  44.             {
  45.                
  46.                 wyj.printf("%f"+" ", macierz[i][j]);                
  47.             }
  48.             wyj.printf("\n");
  49.         }
  50.  
  51.    
  52. }
  53.  
  54.  
  55. public void zapisdopliku()
  56. {
  57.     try{
  58.     File path = new File("uchoNiedzwiedzia.txt");
  59.    
  60.     PrintWriter wyj= new PrintWriter(path);
  61.     wyj.printf("%s%n","Macierz");
  62.     wyj.printf("%d%n", rozm1 );
  63.     wyj.printf("%d%n", rozm2 );
  64.    
  65.     for(int i=0;i<rozm1;i++)
  66.     {  
  67.         for(int j=0; j<rozm2;j++)
  68.         {
  69.            
  70.             wyj.printf("%f"+" ", macierz[i][j]);                
  71.         }
  72.         wyj.printf("%n");
  73.        
  74.     }
  75.     wyj.close();
  76.    
  77.     }
  78.    
  79.         catch(FileNotFoundException e)
  80.         {
  81.             System.out.println("Nie znaleziono");
  82.         }
  83. }
  84.  
  85. void zapisBinarny()
  86. {
  87.     try{
  88.         DataOutputStream os = new DataOutputStream (new FileOutputStream("kochamMalekotki.dat"));
  89.     os.writeUTF("Macierz");
  90.     os.writeInt (rozm1);
  91.     os.writeInt (rozm2);
  92.    
  93.      for(int i=0;i<rozm1;i++)
  94.     {  
  95.         for(int j=0; j<rozm2;j++)
  96.         {
  97.            
  98.             os.writeDouble (macierz[i][j]) ;              
  99.         }
  100.        
  101.     }
  102.     os.close();
  103. }
  104.  
  105.   catch(FileNotFoundException e)
  106.     {
  107.         System.out.println("Blad");
  108.     }
  109.    
  110.      catch (IOException e) {
  111.        
  112.         System.out.println("Blad wejscia/wyjscia");
  113.     }
  114. }
  115.  
  116.  
  117.  
  118. void wartsred()
  119. {
  120.     int ilosc=0;
  121.     double suma=0;
  122.     for(int i=0;i<rozm1;i++)
  123.     {
  124.         for(int j=0;j<rozm2;j++)
  125.         {
  126.             ilosc++;
  127.              suma+=macierz[i][j];
  128.         }
  129.        
  130.     }
  131.     System.out.println((double) suma/ilosc);
  132. }
  133.  
  134. void odczytajPlik()
  135. {
  136.     try{
  137.     BufferedReader wej = new BufferedReader( new FileReader("uchoNiedzwiedzia.txt"));
  138.     StreamTokenizer st = new StreamTokenizer(wej);
  139.    
  140.     String s=null;
  141.     double odczytanaMacierz[][] = null;
  142.     if((s=wej.readLine()).equals("Macierz"))
  143.     {
  144.         System.out.println(s);
  145.         int liczbaWierszy = Integer.parseInt(wej.readLine());
  146.         int liczbaKolumn = Integer.parseInt(wej.readLine());
  147.         System.out.println("Liczba wierszy " + liczbaWierszy);
  148.         System.out.println("Liczba kolumn "+ liczbaKolumn);
  149.         odczytanaMacierz = new double[liczbaWierszy][liczbaKolumn];
  150.        
  151.         int i=0;
  152.         int j=0;
  153.         int token = 0;
  154.         while((token = st.nextToken()) != StreamTokenizer.TT_EOF && i<liczbaWierszy)
  155.         {
  156.             if(token == StreamTokenizer.TT_NUMBER)
  157.             {
  158.                 odczytanaMacierz[i][j] = st.nval;
  159.                 j++;
  160.                 if(j>liczbaKolumn-1)
  161.                 {
  162.                     j=0;
  163.                     i++;
  164.                    
  165.                 }
  166.             }
  167.         }
  168.    
  169.         for(int a=0;a<liczbaWierszy-1;a++)
  170.         {   PrintWriter wyj = new PrintWriter(System.out, true);
  171.             for(int b=0; b<liczbaKolumn-1;b++)
  172.             {
  173.                     wyj.printf("%f"+" ", macierz[a][b]);                
  174.             }
  175.             wyj.printf("\n");
  176.         }
  177.     }
  178.     else
  179.     {
  180.         System.out.println("Blad- nazwa pliku do odczytu jest inna");
  181.     }
  182.    
  183.     }
  184.  
  185.      catch (FileNotFoundException e) {
  186.        
  187.         System.out.println("Nie znaleziono pliku");
  188.     }
  189.      catch (IOException e) {
  190.        
  191.         System.out.println("Blad wejscia/wyjscia");
  192.     }
  193.    
  194.  
  195.  
  196. }
  197.  
  198. void odczytBinarny()
  199. {
  200.     try{
  201.     InputStream is = new  FileInputStream("kochamMalekotki.dat");
  202.     DataInputStream dis = new DataInputStream(is);
  203.    
  204.     String s=null;
  205.     double odczytanaMacierz[][] = null;
  206.    
  207.     s=dis.readUTF();
  208.     int liczbaWierszy=dis.readInt();
  209.     int liczbaKolumn=dis.readInt();
  210.    
  211.     odczytanaMacierz = new double[liczbaWierszy][liczbaKolumn];
  212.    
  213.     int i=0;
  214.     int j=0;
  215.  
  216.        
  217.     while(s.equals("Macierz")&& i<liczbaWierszy)
  218.     {
  219.                
  220.                
  221.                 odczytanaMacierz[i][j] = dis.readDouble();
  222.                 j++;
  223.                 if(j>liczbaKolumn-1)
  224.                 {
  225.                     j=0;
  226.                     i++;
  227.                    
  228.                 }
  229.     }
  230.  
  231.    
  232.     for(int a=0;a<liczbaWierszy-1;a++)
  233.         {   PrintWriter wyj = new PrintWriter(System.out, true);
  234.             for(int b=0; b<liczbaKolumn-1;b++)
  235.             {
  236.                     wyj.printf("%f"+" ", macierz[a][b]);                
  237.             }
  238.             wyj.printf("\n");
  239.         }
  240.    
  241.     }
  242.    
  243.    
  244.      catch (FileNotFoundException e) {
  245.        
  246.         System.out.println("Nie znaleziono pliku");
  247.     }
  248.      catch (IOException e) {
  249.        
  250.         System.out.println("Blad wejscia/wyjscia");
  251.     }
  252.  
  253.  
  254.  
  255. }
  256.  
  257.  
  258. public static void main(String[]args)
  259. {
  260.     StrumienB macierz1 = new StrumienB(4,5);
  261.     macierz1.wypelnijmacierz();
  262.     macierz1.wydruk();
  263.     macierz1.zapisBinarny();
  264.     System.out.println("\n");
  265.     macierz1.odczytBinarny();
  266.     //macierz1.odczytajPlik();
  267.     System.out.println("\n");
  268.     System.out.println("Średnia wartość elementow tablicy wynosi: ");
  269.     macierz1.wartsred();
  270.  
  271.  
  272. }
  273.  
  274.  
  275.        
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement