Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // KvadratnaMatrica
- import java.io.IOException;
- public interface KvadratnaMatrica {
- public abstract double det();
- public abstract void ucitaj() throws IOException;
- public abstract void stampaj();
- public abstract void uctaijIzFajla(String ImeFajla) throws IOException;
- public abstract void upisiUFajl(String ImeFajla) throws IOException;
- }
- // PunaMatrica
- import java.io.*;
- public class PunaMatrica implements KvadratnaMatrica {
- private int n;
- private double[][] mat;
- public PunaMatrica(int x) throws Exception{
- if (x <= 0 )
- throw new Exception("Vrste i kolone moraju imati vecu vrednost od 0!");
- n = x;
- mat = new double[n][n];
- }
- public double det() {
- if (n == 1) {
- return mat[0][0];
- }
- if (n == 2) {
- return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
- }
- double vrednost = 0;
- double znak = -1;
- for (int i = 0; i < n; i++) {
- double[][] temp = new double[n-1][n-1];
- int iP = -1;
- for (int iT = 0; iT < n; iT++) {
- if (iT == i)
- continue;
- iP++;
- int jP = -1;
- for (int jT = 1; jT < n; jT++) {
- jP++;
- temp[iP][jP] = mat[iT][jT];
- }
- }
- znak = znak * -1;
- vrednost = vrednost + detGlavna(temp, n - 1) * znak * mat[i][0];
- }
- return vrednost;
- }
- private double detGlavna(double[][] matrica, int m) {
- if (m == 2) {
- return matrica[0][0] * matrica[1][1] - matrica[0][1] * matrica[1][0];
- }
- double vrednost = 0;
- double znak = -1;
- for (int i = 0; i < m; i++) {
- double[][] temp = new double[m-1][m-1];
- int iP = -1;
- for (int iT = 0; iT < m; iT++) {
- if (iT == i)
- continue;
- iP++;
- int jP = -1;
- for (int jT = 1; jT < m; jT++) {
- jP++;
- temp[iP][jP] = matrica[iT][jT];
- }
- }
- znak = znak * -1;
- vrednost = vrednost + detGlavna(temp, m - 1) * znak * matrica[i][0];
- }
- return vrednost;
- }
- public void ucitaj() throws IOException {
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader bfr = new BufferedReader(isr);
- System.out.print("Unesite ");
- System.out.print(n);System.out.print("*");System.out.print(n);
- System.out.println(" elemenata:");
- String samoC;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- samoC = bfr.readLine();
- mat[i][j] = Double.valueOf(samoC);
- }
- }
- }
- public void stampaj() {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(mat[i][j]);
- System.out.print(" ");
- }
- System.out.println();
- }
- }
- public void uctaijIzFajla(String ImeFajla) throws IOException {
- FileInputStream fin = new FileInputStream(ImeFajla);
- DataInputStream din = new DataInputStream(fin);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- mat[i][j] = din.readDouble();
- }
- }
- din.close();
- fin.close();
- }
- public void upisiUFajl(String imeFajla) throws IOException {
- FileOutputStream fos = new FileOutputStream(imeFajla);
- DataOutputStream dos = new DataOutputStream(fos);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- dos.writeDouble(mat[i][j]);
- }
- }
- dos.close();
- fos.close();
- }
- }
- // main
- import java.io.IOException;
- public class mejn {
- public static void main(String[] args) throws Exception {
- try{
- KvadratnaMatrica k = new PunaMatrica(3);
- k.uctaijIzFajla("lezgo");
- k.stampaj();
- System.out.print(k.det());
- }
- catch (IOException e) {
- System.out.println(e);
- }
- catch (Exception e) {
- System.out.print(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement