Last active
February 20, 2017 03:43
-
-
Save antoniojxk/eb9fef379a5d3a6dbd1e6d4013fe60bb to your computer and use it in GitHub Desktop.
Taller 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 | |
6 | |
12 | |
18 | |
36 | |
60 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 | |
3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 | |
3 | |
4 | |
1 | |
6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package co.edu.poligran.ingenieria.de.software.taller1; | |
public class InputInvalidoException extends Exception { | |
private static final long serialVersionUID = 1L; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package co.edu.poligran.ingenieria.de.software.taller1; | |
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
public class LectorDeInput { | |
private BufferedReader fileReader; | |
private static Scanner scanner = new Scanner(System.in);; | |
public LectorDeInput(String ruta) throws FileNotFoundException { | |
this.fileReader = new BufferedReader(new FileReader(ruta)); | |
} | |
public long[] parseNumeros() throws IOException, InputInvalidoException { | |
String linea; | |
List<Long> numeros = new ArrayList<>(); | |
while ((linea = fileReader.readLine()) != null) { | |
if (!linea.trim().equals("")) { | |
Long numero = Long.parseLong(linea); | |
numeros.add(numero); | |
} | |
} | |
validarInput(numeros); | |
return numeros.stream().mapToLong(l -> l).toArray(); | |
} | |
private void validarInput(List<Long> numeros) throws InputInvalidoException { | |
if (numeros == null || numeros.size() < 3) { | |
arrojarExcepcion("el input no tiene la longitud mínima de 3"); | |
} | |
for (int i = 0; i < numeros.size(); i++) { | |
if (numeros.get(i) <= 1) { | |
arrojarExcepcion("el input contiene números que no son naturales mayores que 1"); | |
} | |
} | |
} | |
private void arrojarExcepcion(String mensaje) throws InputInvalidoException { | |
System.err.println("Error: " + mensaje); | |
throw new InputInvalidoException(); | |
} | |
public static String promptString(String mensaje) { | |
System.out.println(mensaje); | |
return scanner.next(); | |
} | |
public static String promptString(String mensaje, String valorPorDefecto) { | |
System.out.print(mensaje + ": "); | |
String input = scanner.nextLine(); | |
if (input.trim().equals("")) { | |
return valorPorDefecto; | |
} else { | |
return input; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package co.edu.poligran.ingenieria.de.software.taller1; | |
import java.io.IOException; | |
import java.util.Arrays; | |
public class Main { | |
public static void main(String args[]) throws IOException, InputInvalidoException { | |
System.out.println("Este programa calcula el MCM y MCD de varios números.\n" | |
+ "Debe ingresar la ruta del archivo donde cada número está en una línea separada.\n" | |
+ "Puede aceptar los valores por defecto al oprimir enter.\n\n"); | |
String nombreArchivo = LectorDeInput.promptString("Ingrese el nombre del archivo [input.txt]", "input.txt"); | |
LectorDeInput lector = new LectorDeInput(nombreArchivo); | |
long[] numeros = lector.parseNumeros(); | |
System.out.println("El input fue: " + Arrays.toString(numeros)); | |
System.out.println("El mínimo común múltiplo es: " + MotorDeCalculo.minimoComunMultiplo(numeros)); | |
System.out.println("El máximo común divisor es: " + MotorDeCalculo.maximoComunDivisor(numeros)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package co.edu.poligran.ingenieria.de.software.taller1; | |
public class MotorDeCalculo { | |
public static long maximoComunDivisor(long numero1, long numero2) { | |
while (numero2 > 0) { | |
long temporal = numero2; | |
numero2 = numero1 % numero2; | |
numero1 = temporal; | |
} | |
return numero1; | |
} | |
public static long maximoComunDivisor(long[] numeros) { | |
long resultado = numeros[0]; | |
for (int i = 1; i < numeros.length; i++) { | |
resultado = maximoComunDivisor(resultado, numeros[i]); | |
} | |
return resultado; | |
} | |
public static long minimoComunMultiplo(long numero1, long numero2) { | |
return numero1 * (numero2 / maximoComunDivisor(numero1, numero2)); | |
} | |
public static long minimoComunMultiplo(long[] numeros) { | |
long resultado = numeros[0]; | |
for (int i = 1; i < numeros.length; i++) { | |
resultado = minimoComunMultiplo(resultado, numeros[i]); | |
} | |
return resultado; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment