Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package MatricesMatematicas;
  5.  
  6. import utiles.Teclado;
  7.  
  8. /**
  9. * @author AntonioLuque
  10. *
  11. */
  12. public class MatricesMatematicas {
  13. public static void main(String[] args) {
  14.  
  15. int opc;
  16.  
  17. int[][] m1 = null, m2 = null;// las creo para poder mandarlas como
  18. // argumento
  19.  
  20. do {
  21.  
  22. mostrarMenu();
  23.  
  24. opc = comprobarOpcion(0, 6);
  25. int m;
  26. if (opc == 1) {
  27.  
  28. m = elegirMatriz();
  29.  
  30. if (m == 1) {
  31.  
  32. m1 = inicializarMatriz(crearMatriz());
  33.  
  34. } else {
  35.  
  36. m2 = inicializarMatriz(crearMatriz());
  37. }
  38. }
  39. realizarOpcion(opc, m1, m2);
  40.  
  41. } while (opc != 0);
  42.  
  43. }
  44.  
  45. private static int elegirMatriz() {
  46.  
  47. System.out.println("\nElige una matriz: ");
  48.  
  49. System.out.println("\t1. Matriz 1");
  50.  
  51. System.out.println("\t2. Matriz 2");
  52.  
  53. int opc = comprobarOpcion(1, 2);
  54.  
  55. return opc;
  56.  
  57. }
  58.  
  59. private static void mostrarMenu() {
  60.  
  61. System.out.println("\n\tMENÚ");
  62.  
  63. System.out.println("0. Salir");
  64.  
  65. System.out.println("1. Crear matriz");
  66.  
  67. System.out.println("2. Sumar matrices");
  68.  
  69. System.out.println("3. Restar matrices");
  70.  
  71. System.out.println("4. Multiplicar matrices");
  72.  
  73. System.out.println("5. Trasponer matriz");
  74.  
  75. System.out.println("6. Ayuda");
  76.  
  77. }
  78.  
  79. private static void realizarOpcion(int opc, int[][] m1, int[][] m2) {
  80. switch (opc) {
  81. case 0:
  82. System.out.println("Adiós");
  83. break;
  84. case 1:
  85. break;
  86. case 2:
  87. sumarMatrices(m1, m2);
  88. break;
  89. case 3:
  90. restarMatrices(m1, m2);
  91. break;
  92. case 4:
  93. multiplicarMatrices(m1, m2);
  94. break;
  95. case 5:
  96. if (elegirMatriz() == 1)
  97. trasponerMatriz(m1);
  98. else
  99. trasponerMatriz(m2);
  100. break;
  101. case 6:
  102. mostrarAyuda();
  103. break;
  104. }
  105. }
  106.  
  107. private static void mostrarAyuda() {
  108. System.out
  109. .println("\nEste programa le permite crear matrices y realizar las siguientes operaciones con ellas:"
  110. +
  111.  
  112. "\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."
  113. +
  114.  
  115. "\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."
  116. +
  117.  
  118. "\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."
  119. +
  120.  
  121. "\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."
  122. +
  123.  
  124. "\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.");
  125. }
  126.  
  127. private static void trasponerMatriz(int[][] m) {
  128. if (m == null) {
  129. System.out.println("¡No has creado esa matriz!");
  130. return;
  131. }
  132. int[][] traspuesta = new int[m[0].length][m.length];
  133. for (int i = 0; i < traspuesta.length; i++) {
  134. for (int j = 0; j < traspuesta[i].length; j++) {
  135. traspuesta[i][j] = m[j][i];
  136. }
  137. }
  138. mostrarMatriz(m, "Matriz original");
  139. mostrarMatriz(traspuesta, "Traspuesta");
  140. }
  141.  
  142. private static void multiplicarMatrices(int[][] m1, int[][] m2) {
  143. if (m1 == null || m2 == null) {
  144. System.out
  145. .println("¡Necesitas dos matrices! Créalas para poder multiplicar");
  146. return;
  147. }
  148. if (m1[0].length != m2.length) {
  149. System.out
  150. .println("¡La matriz 1 debe tener tantas columnas como filas tenga la matriz 2! Modifícalas para poder multiplicar");
  151. return;
  152. }
  153. int[][] producto = new int[m1.length][m2[0].length];
  154. for (int i = 0; i < producto.length; i++) {
  155. for (int j = 0; j < producto[i].length; j++) {
  156. for (int k = 0; k < m1[i].length; k++) {
  157. producto[i][j] += m1[i][k] * m2[k][j];
  158. }
  159. }
  160. }
  161. mostrarMatriz(m1, "Matriz producto 1");
  162. mostrarMatriz(m2, "Matriz producto 2");
  163. mostrarMatriz(producto, "Producto");
  164. }
  165.  
  166. private static void restarMatrices(int[][] m1, int[][] m2) {
  167. if (m1 == null || m2 == null) {
  168. System.out
  169. .println("¡Necesitas dos matrices! Créalas para poder restar");
  170. return;
  171. }
  172. if (m1.length != m2.length || m1[0].length != m2[0].length) {
  173. System.out
  174. .println("¡Las matrices deben tener el mismo número de filas y columnas para poder restarse!");
  175. return;
  176. }
  177. int[][] resta = new int[m1.length][m1[0].length];
  178. for (int i = 0; i < resta.length; i++) {
  179. for (int j = 0; j < resta[i].length; j++) {
  180. resta[i][j] = m1[i][j] - m2[i][j];
  181. }
  182. }
  183. mostrarMatriz(m1, "Matriz minuendo");
  184. mostrarMatriz(m2, "Matriz sustraendo");
  185. mostrarMatriz(resta, "Resultado");
  186. }
  187.  
  188. private static void sumarMatrices(int[][] m1, int[][] m2) {
  189. if (m1 == null || m2 == null) {
  190. System.out
  191. .println("¡Necesitas dos matrices! Créalas para poder sumar");
  192. return;
  193. }
  194. if (m1.length != m2.length || m1[0].length != m2[0].length) {
  195. System.out
  196. .println("¡Las matrices deben tener el mismo número de filas y columnas para poder sumarse!");
  197. return;
  198. }
  199. int[][] suma = new int[m1.length][m1[0].length];
  200. for (int i = 0; i < suma.length; i++) {
  201. for (int j = 0; j < suma[i].length; j++) {
  202. suma[i][j] = m1[i][j] + m2[i][j];
  203. }
  204. }
  205. mostrarMatriz(m1, "Matriz sumando 1");
  206. mostrarMatriz(m2, "Matriz sumando 2");
  207. mostrarMatriz(suma, "Resultado");
  208. }
  209.  
  210. private static void mostrarMatriz(int[][] m, String msj) {
  211. if (m != null) {
  212. System.out.println("\n" + msj);
  213. for (int[] e : m) {
  214. for (int se : e)
  215. System.out.print("\t" + se);
  216. System.out.println();
  217. }
  218. }
  219. }
  220.  
  221. private static int comprobarOpcion(int min, int max) {
  222. int opc;
  223. do {
  224. opc = elegirOpcion();
  225. } while (opc < min || opc > max);
  226. return opc;
  227. }
  228.  
  229. private static int elegirOpcion() {
  230. return Teclado.leerEntero("Introduce una opción: ");
  231. }
  232.  
  233. private static int[][] crearMatriz() {
  234. return new int[pedirDimension("Introduce el número de filas:")][pedirDimension("Introduce el número de columnas:")];
  235. }
  236.  
  237. private static int pedirDimension(String msj) {
  238. int dim;
  239. do {
  240. dim = Teclado.leerEntero(msj);
  241. } while (dim < 1);
  242. return dim;
  243. }
  244.  
  245. private static int[][] inicializarMatriz(int[][] matriz) {
  246. System.out.println("1. Inicializar con números aleatorios");
  247. System.out.println("2. Inicializar con números introducidos");
  248. int modo = comprobarOpcion(1, 2);
  249. if (modo == 1) {
  250. int min = Teclado.leerEntero("Introduce el mínimo:"), max = Teclado
  251. .leerEntero("Introduce el máximo:");
  252. for (int i = 0; i < matriz.length; i++) {
  253. for (int j = 0; j < matriz[i].length; j++) {
  254. matriz[i][j] = (int) (Math.random() * (max - min + 1) + min);
  255. }
  256. }
  257. } else {
  258. for (int i = 0; i < matriz.length; i++) {
  259. for (int j = 0; j < matriz[i].length; j++) {
  260. matriz[i][j] = Teclado.leerEntero("Introduce un número:");
  261. }
  262. }
  263. }
  264. return matriz;
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement