Cvičenie 2 - Matice (2.10.2008 extra)

Created: 2008-10-02 - 14:56

Tento kod je taky zaciatok do extra cviceni - operacie s maticami + dorobene scitanie a nasobenie matic a formatovany vystup toString().

package Matice;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Matica {
	
	private int[][] matica;
	private int pocetRiadkov;
	private int pocetStlpcov;
	
	Matica(int riadky, int stlpce){
		matica = new int[riadky][stlpce];
		pocetRiadkov = riadky;
		pocetStlpcov = stlpce;
	}
	
	Matica(File subor,int riadky, int stlpce){
		matica = new int[riadky][stlpce];
		pocetRiadkov = riadky;
		pocetStlpcov = stlpce;
		nacitajZoSuboru(subor);
	}
	
	public void nacitajZoSuboru(File suborSMaticou){
		try {
			Scanner citac = new Scanner(suborSMaticou);
			int poradieRiadku = 0;
			while(citac.hasNextLine()){
				String riadok = citac.nextLine();
				String[] polozky = riadok.split(" ");
				for (int i = 0; i < polozky.length; i++) {
					matica[poradieRiadku][i]=Integer.parseInt(polozky[i]);
				}
				poradieRiadku++;
			}
			citac.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void zapisSa(File subor){
		 FileWriter wr;
		try {
			wr = new FileWriter(subor);
			 wr.write(toString());
			 wr.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int getPocetRiadkov() {
		return pocetRiadkov;
	}
	
	public int getPocetStlpcov() {
		return pocetStlpcov;
	}

	public String toString() {
		int najdlhsi=0;
		for(int i=0;i<pocetRiadkov;i++){
			for(int j=0;j<pocetStlpcov;j++){
				String s = String.valueOf(this.matica[i][j]);
				if(s.length()>najdlhsi)
					najdlhsi = s.length();
			}
		}
		
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<pocetRiadkov;i++){
			for(int j=0;j<pocetStlpcov;j++){
				for(int x=0;x<najdlhsi-String.valueOf(this.matica[i][j]).length();x++)
					sb.append(" ");
				sb.append(this.matica[i][j]);
				sb.append(" ");
			}
			sb.append("\n");
		}
		
		return sb.toString();
	}
	
	public Matica pripocitaj(Matica m2){
		Matica m = null;
		Matica m1 = this;
		if(m1.getPocetRiadkov()==m2.getPocetRiadkov()&&
				m1.getPocetStlpcov()==m2.getPocetStlpcov()){
			m = new Matica(m1.getPocetRiadkov(),m1.getPocetStlpcov());
			for (int i = 0; i < m.matica.length; i++) {
				for (int j = 0; j < matica[i].length; j++) {
					m.matica[i][j]=m1.matica[i][j]+m2.matica[i][j];
				}
			}
		}
		else{
			System.out.println("Tieto matice nemozno spocitat");
		}
		return m;
	}
	
	public Matica vynasob(Matica m2){
		Matica m = null;
		Matica m1 = this;
		if(m1.getPocetStlpcov()==m2.getPocetRiadkov()){
			m = new Matica(m1.getPocetRiadkov(),m2.getPocetStlpcov());
			for (int i = 0; i < m1.getPocetRiadkov(); i++) {
				for (int j = 0; j < m2.getPocetStlpcov(); j++) {
					m.matica[i][j]=0;
					for (int l = 0; l < m2.getPocetRiadkov(); l++) {
						m.matica[i][j]+=m1.matica[i][l]*m2.matica[l][j];
					}
				}
			}
		}
		else if(m1.getPocetRiadkov()==m2.getPocetStlpcov()){
			m = m2.vynasob(m1);
			return m;
		}
		else{
			System.out.println("Tieto matice nemozno vynasobit");
		}
		return m;
	}



}