Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class StrumienB
- {
- int rozm1;
- int rozm2;
- double macierz [][];
- public StrumienB(int rozm1, int rozm2)
- {
- this.rozm1=rozm1;
- this.rozm2=rozm2;
- macierz=new double [rozm1][rozm2];
- }
- void wypelnijmacierz()
- { int zakresrzeczywistych=5;
- { Random liczbaWierszy = new Random();
- for(int i=0;i<rozm1;i++)
- {
- for(int j=0; j<rozm2;j++)
- {
- macierz[i][j]=zakresrzeczywistych*liczbaWierszy.nextDouble();
- }
- }
- }
- }
- public void wydruk()
- {
- for(int i=0;i<rozm1;i++)
- { PrintWriter wyj = new PrintWriter(System.out, true);
- for(int j=0; j<rozm2;j++)
- {
- wyj.printf("%f"+" ", macierz[i][j]);
- }
- wyj.printf("\n");
- }
- }
- public void zapisdopliku()
- {
- try{
- File path = new File("uchoNiedzwiedzia.txt");
- PrintWriter wyj= new PrintWriter(path);
- wyj.printf("%s%n","Macierz");
- wyj.printf("%d%n", rozm1 );
- wyj.printf("%d%n", rozm2 );
- for(int i=0;i<rozm1;i++)
- {
- for(int j=0; j<rozm2;j++)
- {
- wyj.printf("%f"+" ", macierz[i][j]);
- }
- wyj.printf("%n");
- }
- wyj.close();
- }
- catch(FileNotFoundException e)
- {
- System.out.println("Nie znaleziono");
- }
- }
- void zapisBinarny()
- {
- try{
- DataOutputStream os = new DataOutputStream (new FileOutputStream("kochamMalekotki.dat"));
- os.writeUTF("Macierz");
- os.writeInt (rozm1);
- os.writeInt (rozm2);
- for(int i=0;i<rozm1;i++)
- {
- for(int j=0; j<rozm2;j++)
- {
- os.writeDouble (macierz[i][j]) ;
- }
- }
- os.close();
- }
- catch(FileNotFoundException e)
- {
- System.out.println("Blad");
- }
- catch (IOException e) {
- System.out.println("Blad wejscia/wyjscia");
- }
- }
- void wartsred()
- {
- int ilosc=0;
- double suma=0;
- for(int i=0;i<rozm1;i++)
- {
- for(int j=0;j<rozm2;j++)
- {
- ilosc++;
- suma+=macierz[i][j];
- }
- }
- System.out.println((double) suma/ilosc);
- }
- void odczytajPlik()
- {
- try{
- BufferedReader wej = new BufferedReader( new FileReader("uchoNiedzwiedzia.txt"));
- StreamTokenizer st = new StreamTokenizer(wej);
- String s=null;
- double odczytanaMacierz[][] = null;
- if((s=wej.readLine()).equals("Macierz"))
- {
- System.out.println(s);
- int liczbaWierszy = Integer.parseInt(wej.readLine());
- int liczbaKolumn = Integer.parseInt(wej.readLine());
- System.out.println("Liczba wierszy " + liczbaWierszy);
- System.out.println("Liczba kolumn "+ liczbaKolumn);
- odczytanaMacierz = new double[liczbaWierszy][liczbaKolumn];
- int i=0;
- int j=0;
- int token = 0;
- while((token = st.nextToken()) != StreamTokenizer.TT_EOF && i<liczbaWierszy)
- {
- if(token == StreamTokenizer.TT_NUMBER)
- {
- odczytanaMacierz[i][j] = st.nval;
- j++;
- if(j>liczbaKolumn-1)
- {
- j=0;
- i++;
- }
- }
- }
- for(int a=0;a<liczbaWierszy-1;a++)
- { PrintWriter wyj = new PrintWriter(System.out, true);
- for(int b=0; b<liczbaKolumn-1;b++)
- {
- wyj.printf("%f"+" ", macierz[a][b]);
- }
- wyj.printf("\n");
- }
- }
- else
- {
- System.out.println("Blad- nazwa pliku do odczytu jest inna");
- }
- }
- catch (FileNotFoundException e) {
- System.out.println("Nie znaleziono pliku");
- }
- catch (IOException e) {
- System.out.println("Blad wejscia/wyjscia");
- }
- }
- void odczytBinarny()
- {
- try{
- InputStream is = new FileInputStream("kochamMalekotki.dat");
- DataInputStream dis = new DataInputStream(is);
- String s=null;
- double odczytanaMacierz[][] = null;
- s=dis.readUTF();
- int liczbaWierszy=dis.readInt();
- int liczbaKolumn=dis.readInt();
- odczytanaMacierz = new double[liczbaWierszy][liczbaKolumn];
- int i=0;
- int j=0;
- while(s.equals("Macierz")&& i<liczbaWierszy)
- {
- odczytanaMacierz[i][j] = dis.readDouble();
- j++;
- if(j>liczbaKolumn-1)
- {
- j=0;
- i++;
- }
- }
- for(int a=0;a<liczbaWierszy-1;a++)
- { PrintWriter wyj = new PrintWriter(System.out, true);
- for(int b=0; b<liczbaKolumn-1;b++)
- {
- wyj.printf("%f"+" ", macierz[a][b]);
- }
- wyj.printf("\n");
- }
- }
- catch (FileNotFoundException e) {
- System.out.println("Nie znaleziono pliku");
- }
- catch (IOException e) {
- System.out.println("Blad wejscia/wyjscia");
- }
- }
- public static void main(String[]args)
- {
- StrumienB macierz1 = new StrumienB(4,5);
- macierz1.wypelnijmacierz();
- macierz1.wydruk();
- macierz1.zapisBinarny();
- System.out.println("\n");
- macierz1.odczytBinarny();
- //macierz1.odczytajPlik();
- System.out.println("\n");
- System.out.println("Średnia wartość elementow tablicy wynosi: ");
- macierz1.wartsred();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement