Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package MatricesMatematicas;
- import utiles.Teclado;
- /**
- * @author AntonioLuque
- *
- */
- public class MatricesMatematicas {
- public static void main(String[] args) {
- int opc;
- int[][] m1 = null, m2 = null;// las creo para poder mandarlas como
- // argumento
- do {
- mostrarMenu();
- opc = comprobarOpcion(0, 6);
- int m;
- if (opc == 1) {
- m = elegirMatriz();
- if (m == 1) {
- m1 = inicializarMatriz(crearMatriz());
- } else {
- m2 = inicializarMatriz(crearMatriz());
- }
- }
- realizarOpcion(opc, m1, m2);
- } while (opc != 0);
- }
- private static int elegirMatriz() {
- System.out.println("\nElige una matriz: ");
- System.out.println("\t1. Matriz 1");
- System.out.println("\t2. Matriz 2");
- int opc = comprobarOpcion(1, 2);
- return opc;
- }
- private static void mostrarMenu() {
- System.out.println("\n\tMENÚ");
- System.out.println("0. Salir");
- System.out.println("1. Crear matriz");
- System.out.println("2. Sumar matrices");
- System.out.println("3. Restar matrices");
- System.out.println("4. Multiplicar matrices");
- System.out.println("5. Trasponer matriz");
- System.out.println("6. Ayuda");
- }
- private static void realizarOpcion(int opc, int[][] m1, int[][] m2) {
- switch (opc) {
- case 0:
- System.out.println("Adiós");
- break;
- case 1:
- break;
- case 2:
- sumarMatrices(m1, m2);
- break;
- case 3:
- restarMatrices(m1, m2);
- break;
- case 4:
- multiplicarMatrices(m1, m2);
- break;
- case 5:
- if (elegirMatriz() == 1)
- trasponerMatriz(m1);
- else
- trasponerMatriz(m2);
- break;
- case 6:
- mostrarAyuda();
- break;
- }
- }
- private static void mostrarAyuda() {
- System.out
- .println("\nEste programa le permite crear matrices y realizar las siguientes operaciones con ellas:"
- +
- "\n\n1. Crear matriz:\n\tLe permite crear dos matrices. Se le solicitará el número de filas y de columnas de la matriz. Recuerde que debe ser un número igual o mayor que 1. Si elige crear la matriz con números aleatorios, se le solicitará el número mínimo y máximo de los valores que contendrá. Si decide rellenarla por teclado se le pedirá el valor de cada celda."
- +
- "\n\n2. Sumar matrices:\n\tSi ha creado ambas matrices, se sumarán y se mostrarán. Deben tener el mismo número de filas y de columnas."
- +
- "\n\n3. Restar matrices:\n\tSi ha creado ambas matrices, se restará a la matriz 1, la matriz 2, y se mostrará. Deben tener el mismo número de filas y de columnas."
- +
- "\n\n4. Multiplicar matrices:\n\tSi ha creado ambas matrices, se multiplicarán y se mostrarán. La matriz 1 debe tener tantas columnas como filas tenga la matriz 2."
- +
- "\n\n5. Trasponer matriz:\n\tSe le solicitará cuál de las dos matrices desea trasponer, y si la ha creado, se mostrará su inversa.");
- }
- private static void trasponerMatriz(int[][] m) {
- if (m == null) {
- System.out.println("¡No has creado esa matriz!");
- return;
- }
- int[][] traspuesta = new int[m[0].length][m.length];
- for (int i = 0; i < traspuesta.length; i++) {
- for (int j = 0; j < traspuesta[i].length; j++) {
- traspuesta[i][j] = m[j][i];
- }
- }
- mostrarMatriz(m, "Matriz original");
- mostrarMatriz(traspuesta, "Traspuesta");
- }
- private static void multiplicarMatrices(int[][] m1, int[][] m2) {
- if (m1 == null || m2 == null) {
- System.out
- .println("¡Necesitas dos matrices! Créalas para poder multiplicar");
- return;
- }
- if (m1[0].length != m2.length) {
- System.out
- .println("¡La matriz 1 debe tener tantas columnas como filas tenga la matriz 2! Modifícalas para poder multiplicar");
- return;
- }
- int[][] producto = new int[m1.length][m2[0].length];
- for (int i = 0; i < producto.length; i++) {
- for (int j = 0; j < producto[i].length; j++) {
- for (int k = 0; k < m1[i].length; k++) {
- producto[i][j] += m1[i][k] * m2[k][j];
- }
- }
- }
- mostrarMatriz(m1, "Matriz producto 1");
- mostrarMatriz(m2, "Matriz producto 2");
- mostrarMatriz(producto, "Producto");
- }
- private static void restarMatrices(int[][] m1, int[][] m2) {
- if (m1 == null || m2 == null) {
- System.out
- .println("¡Necesitas dos matrices! Créalas para poder restar");
- return;
- }
- if (m1.length != m2.length || m1[0].length != m2[0].length) {
- System.out
- .println("¡Las matrices deben tener el mismo número de filas y columnas para poder restarse!");
- return;
- }
- int[][] resta = new int[m1.length][m1[0].length];
- for (int i = 0; i < resta.length; i++) {
- for (int j = 0; j < resta[i].length; j++) {
- resta[i][j] = m1[i][j] - m2[i][j];
- }
- }
- mostrarMatriz(m1, "Matriz minuendo");
- mostrarMatriz(m2, "Matriz sustraendo");
- mostrarMatriz(resta, "Resultado");
- }
- private static void sumarMatrices(int[][] m1, int[][] m2) {
- if (m1 == null || m2 == null) {
- System.out
- .println("¡Necesitas dos matrices! Créalas para poder sumar");
- return;
- }
- if (m1.length != m2.length || m1[0].length != m2[0].length) {
- System.out
- .println("¡Las matrices deben tener el mismo número de filas y columnas para poder sumarse!");
- return;
- }
- int[][] suma = new int[m1.length][m1[0].length];
- for (int i = 0; i < suma.length; i++) {
- for (int j = 0; j < suma[i].length; j++) {
- suma[i][j] = m1[i][j] + m2[i][j];
- }
- }
- mostrarMatriz(m1, "Matriz sumando 1");
- mostrarMatriz(m2, "Matriz sumando 2");
- mostrarMatriz(suma, "Resultado");
- }
- private static void mostrarMatriz(int[][] m, String msj) {
- if (m != null) {
- System.out.println("\n" + msj);
- for (int[] e : m) {
- for (int se : e)
- System.out.print("\t" + se);
- System.out.println();
- }
- }
- }
- private static int comprobarOpcion(int min, int max) {
- int opc;
- do {
- opc = elegirOpcion();
- } while (opc < min || opc > max);
- return opc;
- }
- private static int elegirOpcion() {
- return Teclado.leerEntero("Introduce una opción: ");
- }
- private static int[][] crearMatriz() {
- return new int[pedirDimension("Introduce el número de filas:")][pedirDimension("Introduce el número de columnas:")];
- }
- private static int pedirDimension(String msj) {
- int dim;
- do {
- dim = Teclado.leerEntero(msj);
- } while (dim < 1);
- return dim;
- }
- private static int[][] inicializarMatriz(int[][] matriz) {
- System.out.println("1. Inicializar con números aleatorios");
- System.out.println("2. Inicializar con números introducidos");
- int modo = comprobarOpcion(1, 2);
- if (modo == 1) {
- int min = Teclado.leerEntero("Introduce el mínimo:"), max = Teclado
- .leerEntero("Introduce el máximo:");
- for (int i = 0; i < matriz.length; i++) {
- for (int j = 0; j < matriz[i].length; j++) {
- matriz[i][j] = (int) (Math.random() * (max - min + 1) + min);
- }
- }
- } else {
- for (int i = 0; i < matriz.length; i++) {
- for (int j = 0; j < matriz[i].length; j++) {
- matriz[i][j] = Teclado.leerEntero("Introduce un número:");
- }
- }
- }
- return matriz;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement