public class Deter {
static final int N = 5;
static void obtener(double A[][], double temp[][],int p, int q, int n)
// Metodo para obtener cofactores, p y q es la fila y columna a // eliminar.
{ int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
temp[i][j++] = A[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
public double deterM(double A[][], int n)
{ double D = 0.0;
if (n == 1)
return A[0][0];
double temp[][] = new double[N][N];
int sign = 1;
for (int f = 0; f < n; f++) {
obtener(A, temp, 0, f, n);
D += sign * A[0][f]* deterM(temp, n - 1);
sign = -sign;
}
return D;
}
Clase Formulario
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class frmDeter2 extends javax.swing.JFrame {
DefaultTableModel model = new DefaultTableModel();
Deter D = new Deter();
public frmDeter2() {
initComponents();
tblB.setModel(model);
String titu[] = {"A","B","C"};
String dato1[] = {"0.55","1.55","2.55"};
String dato2[] = {"-0.55","3.55","-4.55"};
String dato3[] = {"5.55","-6.55","2.55"};
model.setColumnIdentifiers(titu);
model.addRow(dato1);
model.addRow(dato2);
model.addRow(dato3);
}
private void cargarM2() {
model.setRowCount(0);
String titu[] = {"A","B"};
String dato1[] = {"0.55","1.55"};
String dato2[] = {"-0.55","3.55"};
model.setColumnIdentifiers(titu);
model.addRow(dato1);
model.addRow(dato2);
}
private void cargarM3() {
model.setRowCount(0);
String titu[] = {"A","B","C"};
String dato1[] = {"0.55","1.55","2.55"};
String dato2[] = {"-3.55","4.55","-5.55"};
String dato3[] = {"5.55","-6.55","7.55"};
model.setColumnIdentifiers(titu);
model.addRow(dato1);
model.addRow(dato2);
model.addRow(dato3);
}
private void cargarM4() {
model.setRowCount(0);
String titu[] = {"A","B","C","D"};
String dato1[] = {"0.55","1.55","2.55","6.55"};
String dato2[] = {"-3.55","4.55","-5.55","7.55"};
String dato3[] = {"5.55","-6.55","7.55","8.55"};
model.setColumnIdentifiers(titu);
model.addRow(dato1);
model.addRow(dato2);
model.addRow(dato3);
model.addRow(dato1);
}
private void cargarM5() {
model.setRowCount(0);
String titu[] = {"A","B","C","D","E"};
String dato1[] = {"0.55","1.55","2.55","6.55","2.55"};
String dato2[] = {"-3.55","4.55","-5.55","7.55","-5.55"};
String dato3[] = {"5.55","-6.55","7.55","8.55","1.55"};
String dato4[] = {"5.55","-6.55","7.55","8.55","10.55"};
String dato5[] = {"-6.55","-6.55","7.55","8.55","1.55"};
model.setColumnIdentifiers(titu);
model.addRow(dato1);
model.addRow(dato2);
model.addRow(dato3);
model.addRow(dato4);
model.addRow(dato5);
}
private void btnDeterActionPerformed(java.awt.event.ActionEvent evt) { int i,j;
double R;
int N1 = model.getRowCount();
double A[][]=new double[N1][N1];
for(i=0;i<N1;i++) {
for(j=0;j<N1;j++) {
A[i][j] = Double.parseDouble(model.getValueAt(i, j).toString());
}
}
R = Math.round(D.deterM(A, N1)*1000.0)/1000.0;
lblR.setText(""+R);
}
private void cmbEActionPerformed(java.awt.event.ActionEvent evt) {
int op = cmbE.getSelectedIndex();
switch (op){
case 0: cargarM2(); break;
case 1: cargarM3(); break;
case 2: cargarM4(); break;
case 3: cargarM5(); break;
}
}
minuvasoft10@gmail.com
No hay comentarios:
Publicar un comentario