Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package OperacionesMatriz;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Matriz{
- static BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
- public static int opc, fila, colum, fila2, colum2;
- public static double [][] m1;
- public static double [][] m2;
- public static void main(String[] args) throws IOException{
- do{
- System.out.println("Opciones:\n");
- System.out.println("1.- Introducir Matriz A");
- System.out.println("2.- Introducir Matriz B");
- System.out.println("3.- A + B");
- System.out.println("4.- A - B");
- System.out.println("5.- A * B");
- System.out.println("6.- Det(A)");
- System.out.println("7.- Det(B)");
- System.out.println("8.- Simet(A)");
- System.out.println("9.- Simet(B)");
- System.out.println("0.- SALIR\n");
- opc = Integer.parseInt(entrada.readLine());
- switch(opc){
- case 1: matriz1();
- break;
- case 2: matriz2();
- break;
- case 3: sumar();
- break;
- case 4: restar();
- break;
- case 5: multi();
- break;
- case 6: deta();
- break;
- case 7: detb();
- break;
- case 8: simA();
- break;
- case 9: simB();
- break;
- //default:
- // System.out.println("Opción incorrecta. \n\n\n|=======================================================|\n\n\n");
- // break;
- }
- }
- while(opc != 0);
- }
- private static void matriz1() throws IOException{
- System.out.print("Introduce tamaño de las filas:\n");
- fila = Integer.parseInt(entrada.readLine());
- System.out.print("Introduce tamaño de las columnas:\n");
- colum = Integer.parseInt(entrada.readLine());
- m1 = new double [fila][colum];
- for(int i = 0; i < fila; i++){
- for(int j = 0; j < colum; j++){
- System.out.print("Valor de la Matriz A en: [" + (i + 1) + "," + (j + 1) + "]\n");
- m1[i][j] = Double.parseDouble(entrada.readLine());
- }
- }
- }
- private static void matriz2() throws IOException{
- System.out.print("Introduce tamaño de las filas:\n");
- fila2 = Integer.parseInt(entrada.readLine());
- System.out.print("Introduce tamaño de las columnas:\n");
- colum2 = Integer.parseInt(entrada.readLine());
- m2= new double [fila2][colum2];
- for(int i = 0; i < fila2; i++){
- for(int j = 0; j < colum2; j++){
- System.out.print("Valor de la Matriz B en: [" + (i + 1) + "," + (j + 1) + "]\n");
- m2[i][j] = Double.parseDouble(entrada.readLine());
- }
- }
- }
- private static void sumar() throws IOException{
- System.out.print("Suma de Matriz:\n");
- if(fila == fila2 && colum == colum2){
- for(int x = 0; x < fila; x++){
- System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
- for(int y = 0; y < colum; y++){
- System.out.print((m1[x][y]) + (m2[x][y]) + "\t|\t");
- }
- System.out.print("\n"); //Controla la separación entre filas de la Matriz.
- }
- System.out.println("\n"); //Controla el espacio entre la función sumar() y el menú de opciones.
- }
- else{
- System.out.print("No se pueden sumar las Matrices.\n\n\n|=======================================================|\n\n\n");
- }
- }
- private static void sumar2(){ //Cambiar función en main() para no utilizar IOException.
- System.out.print("Suma de Matriz sin IOException:\n");
- if(fila == fila2 && colum == colum2){
- for(int x = 0; x < fila; x++){
- System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
- for(int y = 0; y < colum; y++){
- System.out.print((m1[x][y]) + (m2[x][y]) + "\t|\t");
- }
- System.out.print("\n"); //Controla la separación entre filas de la Matriz.
- }
- System.out.println("\n"); //Controla el espacio entre la función sumar2() y el menú de opciones.
- }
- else{
- System.out.print("No se pueden sumar las Matrices.\n\n\n|=======================================================|\n\n\n");
- }
- }
- private static void restar(){ //Función resta (Matriz A - Matriz B). Cambiar en el main().
- System.out.print("Resta de Matriz:\n");
- if(fila == fila2 && colum == colum2){
- for(int x = 0; x < fila; x++){
- System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
- for(int y = 0; y < colum; y++){
- System.out.print((m1[x][y]) - (m2[x][y]) + "\t|\t");
- }
- System.out.print("\n"); //Controla la separación entre filas de la Matriz.
- }
- System.out.println("\n"); //Controla el espacio entre la función restar() y el menú de opciones.
- }
- else{
- System.out.print("No se pueden restar las Matrices.\n\n\n|=======================================================|\n\n\n");
- }
- }
- private static void restar2(){ //Función resta con los operandos invertidos (Matriz B - Matriz A). Cambiar en el main().
- System.out.print("Resta de Matriz:\n");
- if(fila == fila2 && colum == colum2){
- for(int x = 0; x < fila; x++){
- System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
- for(int y = 0; y < colum; y++){
- System.out.print((m2[x][y]) - (m1[x][y]) + "\t|\t");
- }
- System.out.print("\n"); //Controla la separación entre filas de la Matriz.
- }
- System.out.println("\n"); //Controla el espacio entre la función restar2() y el menú de opciones.
- }
- else{
- System.out.print("No se pueden restar las Matrices.\n\n\n|=======================================================|\n\n\n");
- }
- }
- private static void multi() throws IOException{
- System.out.print("Multiplicación de Matriz:\n");
- if(colum == fila2){
- double [][] r1 = new double[fila][colum2];
- for(int x = 0; x < fila; x++){
- System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
- for(int y = 0; y < colum2; y++){
- for(int m = 0; m < colum; m++){
- r1[x][y] += m1[x][m] * m2[m][y];
- }
- System.out.print(r1[x][y] + "\t|\t");
- }
- System.out.print("\n"); //Controla la separación entre filas de la Matriz.
- }
- System.out.println("\n"); //Controla el espacio entre la función multi() y el menú de opciones.
- }
- else{
- System.out.print("No se pueden multiplicar las Matrices.\n\n\n|=======================================================|\n\n\n");
- String a = entrada.readLine();
- }
- }
- private static void multi2() throws IOException{ //Función multi() invertida.
- System.out.print("Multiplicación de Matriz:\n");
- if(colum2 == fila){
- double [][] r1 = new double[fila2][colum];
- for(int x = 0; x < fila2; x++){
- System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
- for(int y = 0; y < colum; y++){
- for(int m = 0; m < colum2; m++){
- r1[x][y] += m2[x][m] * m1[m][y];
- }
- System.out.print(r1[x][y] + "\t|\t");
- }
- System.out.print("\n"); //Controla la separación entre filas de la Matriz.
- }
- System.out.println("\n"); //Controla el espacio entre la función multi() y el menú de opciones.
- }
- else{
- System.out.print("No se pueden multiplicar las Matrices.\n\n\n|=======================================================|\n\n\n");
- String a = entrada.readLine();
- }
- }
- private static void deta() throws IOException{ //Determinante de Matriz A.
- if(fila == colum){
- System.out.print("La determinante es: " + determinante(m1));
- String a = entrada.readLine();
- }
- else{
- System.out.print("La Matriz no tiene determinante.");
- String a = entrada.readLine();
- }
- }
- private static void detb() throws IOException{ //Determinante de Matriz B.
- if(fila2 == colum2){
- System.out.print("La determinante es: " + determinante(m2));
- String a = entrada.readLine();
- }
- else{
- System.out.print("La Matriz no tiene determinante.");
- String a = entrada.readLine();
- }
- }
- public static double determinante(double[][] matriz){ //Función para calcular Determinantes de Matriz A y Matriz B.
- double det;
- if(matriz.length == 2){
- det = (matriz[0][0] * matriz[1][1]) - (matriz[1][0] * matriz[0][1]);
- return det;
- }
- double suma = 0;
- for(int i = 0; i < matriz.length; i++){
- double[][] nm = new double[matriz.length - 1][matriz.length - 1];
- for(int j = 0; j < matriz.length; j++){
- if(j != i){
- for(int k = 1; k < matriz.length; k++){
- int indice =- 1;
- if(j < i)
- indice = j;
- else if(j > i)
- indice = j - 1;
- nm[indice][k - 1] = matriz[j][k];
- }
- }
- }
- if(i % 2 == 0)
- suma += matriz[i][0] * determinante(nm);
- else
- suma -= matriz[i][0] * determinante(nm);
- }
- return suma;
- }
- private static void traa() throws IOException{ //Transpuesta de Matriz A. No usada en el programa.
- System.out.print("La Matriz original:");
- System.out.print("\n");
- for(int x = 0; x < fila; x++){
- for(int y = 0; y < colum; y++){
- System.out.print(m1[x][y] + "\t|\t");
- }
- System.out.print("\n");
- }
- System.out.print("\n\n");
- System.out.print("La Matriz transpuesta:");
- System.out.print("\n");
- for(int x = 0; x < colum; x++){
- for(int y = 0; y < fila; y++){
- System.out.print(m1[y][x] + "\t|\t");
- }
- System.out.print("\n");
- }
- String a = entrada.readLine();
- }
- private static void trab() throws IOException{ //Transpuesta de Matriz B. No usada en el programa.
- System.out.print("La Matriz original:");
- System.out.print("\n");
- for(int x = 0; x < fila2; x++){
- for(int y = 0; y < colum2; y++){
- System.out.print(m2[x][y] + "\t|\t");
- }
- System.out.print("\n");
- }
- System.out.print("\n\n");
- System.out.print("La Matriz transpuesta:");
- System.out.print("\n");
- for(int x = 0; x < colum2; x++){
- for(int y = 0; y < fila2; y++){
- System.out.print(m2[y][x] + "\t|\t");
- }
- System.out.print("\n");
- }
- String a = entrada.readLine();
- }
- private static void simA() throws IOException{ //Simetría de Matriz A.
- boolean esSimetrica = true;
- for(int i = 0; i < m1.length; i++){
- for(int j = 0; j < m1[0].length; j++){
- if(m1[i][j] != m1[j][i]){
- System.out.println("La Matriz A no es simétrica.\n\n\n");
- esSimetrica = false;
- break;
- }
- }
- if(!esSimetrica){
- break;
- }
- }
- if(esSimetrica){
- System.out.println("La Matriz A es simétrica.\n\n\n");
- }
- }
- private static void simB() throws IOException{ //Simetría de Matriz B.
- boolean esSimetrica = true;
- for(int i = 0; i < m2.length; i++){
- for(int j = 0; j < m2[0].length; j++){
- if(m2[i][j] != m2[j][i]){
- System.out.println("La Matriz B no es simétrica.\n\n\n");
- esSimetrica = false;
- break;
- }
- }
- if(!esSimetrica){
- break;
- }
- }
- if(esSimetrica){
- System.out.println("La Matriz B es simétrica.\n\n\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment