-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraspuesta2.java
More file actions
145 lines (112 loc) · 3.97 KB
/
Traspuesta2.java
File metadata and controls
145 lines (112 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Traspuesta2 {
public static void main(String[] args){
File f = pedirNombreFichero();
if(f != null){
int[][] matriz = leerFichero(f);
if(matriz!=null){
if(esMatrizSimetrica(matriz)){
trasponerMatrizSimetrica(matriz);
}else{
trasponerMatrizNoSimetrica(matriz);
}
File fGuardar = new File(f.getName());
guardarMatrizFichero(fGuardar, matriz);
System.out.println("Fichero guardado...");
}else{
System.out.println("Comprueba el fichero");
}
}else{
System.out.println("El fichero no existe");
}
}
public static File pedirNombreFichero(){
Scanner sc = new Scanner(System.in);
System.out.println("Escribe el nombre del fichero: ");
String nombre = sc.next();
File f = new File(nombre);
if(f.exists()){
return f;
}else{
return null;
}
}
public static int[][] leerFichero(File f){
int[][] matriz = null;
try{
Scanner sn = new Scanner(f);
int filas = 0, columnas = 0, contador = 0;
//Mientras haya un siguiente número, sigo...
while(sn.hasNextInt()){
switch(contador){
case 0:
filas = sn.nextInt();
break;
case 1:
columnas = sn.nextInt();
matriz = new int[filas][columnas];
break;
default:
matriz[(contador - 2) / columnas][(contador - 2) % columnas] = sn.nextInt();
}
contador++;
}
}catch(FileNotFoundException e){
Logger.getLogger(Traspuesta2.class.getName()).log(Level.SEVERE, null, e);
}
return matriz;
}
public static boolean esMatrizSimetrica(int[][] matriz){
return matriz.length == matriz[0].length;
}
public static void trasponerMatrizSimetrica(int[][] matriz){
boolean[][] cambios = new boolean[matriz.length][matriz[0].length];
for(int i = 0; i < matriz.length; i++){
for(int j = 0; j < matriz.length; j++){
if(!cambios[i][j]){
intercambio(matriz, i, j);
cambios[i][j] = true;
cambios[j][i] = true;
}
}
}
}
public static void trasponerMatrizNoSimetrica(int[][] matriz){
int[][] matrizTras = new int[matriz[0].length][matriz.length];
//boolean[][] cambios = new boolean[matriz.length][matriz[0].length];
for(int i = 0; i < matriz.length; i++){
for(int j = 0; j < matriz.length; j++){
intercambio(matriz, matrizTras, i, j);
}
}
matriz = matrizTras;
}
public static void intercambio(int[][] matriz, int i, int j){
int aux = matriz[i][j];
matriz[i][j] = matriz[j][i];
matriz[j][i] = aux;
}
public static void intercambio(int[][] matriz, int[][] matrizT, int i, int j){
matriz[i][j] = matrizT[j][i];
}
public static void guardarMatrizFichero(File f, int[][] matriz) {
try{
PrintWriter pw = new PrintWriter(f);
pw.print(matriz.length + " ");
pw.print(matriz[0].length+" ");
for(int i = 0; i < matriz.length; i++){
for(int j = 0; j < matriz[0].length; j++){
pw.print(matriz[i][j]+" ");
}
}
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}