document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /* AUTOR: d3n3k4 (Dnk!)
  2.  * WEB: http://d3n3k4.blogspot.com/
  3.  * FECHA: 28/NOV/2010
  4.  * DESCRIPCION:
  5.  *  - Clase para trabajar con quebrados (suma) incluido:
  6.  *      - mcd: Calcula el maximo comun divisor.
  7.  *      - mcm: Calcula el minimo comun multiplo.
  8.  *      - reducir: Simplifica el quebrado.
  9.  *  - En un futuro se iran aƱadiendo mas opciones, si quieres mantenerte
  10.  *    informado no dudes en visitar mi blog.
  11.  *  - Esta contenido la clase main, con un ejemplo de uso de la clase.
  12.  * NOTA: Este codigo es libre y puede ser usado,modificado... siempre y cuando se
  13.  *  mantenga los creditos y comentarios del autor.
  14.  */
  15.  
  16. import java.util.Scanner;
  17.  
  18. public class opQuebrados {
  19.     public static int[] leeQuebrado() {
  20.         int[] quebrado = new int[2];
  21.         Scanner entrada = new Scanner(System.in);
  22.        
  23.         quebrado[0] = entrada.nextInt();
  24.         quebrado[1] = entrada.nextInt();
  25.        
  26.         return quebrado;
  27.     }
  28.     public static String escribeQuebrado(int[] q_Original) {
  29.         String cadQuebrado = q_Original[0]+"/"+q_Original[1];
  30.         return cadQuebrado;
  31.     }
  32.     public static int mcd(int a,int b) {
  33.         int aux;
  34.         while (b != 0) {
  35.             aux = b;
  36.             b = a%b;
  37.             a = aux;
  38.         }
  39.         return a;
  40.     }
  41.     public static int mcm(int a,int b) {
  42.         int min = (a*b)/mcd(a,b);
  43.         return min;
  44.     }
  45.     public static int[] reducir(int[] q) {
  46.         int[] qReducido = new int[2];
  47.         int max = mcd(q[0],q[1]);;
  48.         qReducido[0] = q[0]/max;
  49.         qReducido[1] = q[1]/max;
  50.         return qReducido;
  51.     }
  52.     public static int[] sumar(int[] q1, int[] q2 ) {
  53.         int[] qSumado = new int[2];
  54.         int min = mcm(q1[1],q2[1]);
  55.         qSumado[1]=min;
  56.         qSumado[0]=q1[0]*(min/q1[1])+q2[0]*(min/q2[1]);
  57.         return qSumado;
  58.     }
  59.     public static void main(String[] args) {
  60.         int[] quebrado1 = new int[2];
  61.         int[] quebrado2 = new int[2];
  62.         int[] resultado = new int[2];
  63.         int[] reducido = new int[2];
  64.        
  65.         System.out.print("1er Qebrado: ");
  66.         quebrado1 = leeQuebrado();
  67.         System.out.print("2do Qebrado: ");
  68.         quebrado2 = leeQuebrado();
  69.        
  70.         resultado = sumar(quebrado1,quebrado2);
  71.         reducido = reducir(resultado);
  72.        
  73.         System.out.print(escribeQuebrado(quebrado1) + " + " + escribeQuebrado(quebrado2) + " = " + escribeQuebrado(resultado));
  74.         if (resultado[1] != reducido[1]) {
  75.             System.out.println(" = " + escribeQuebrado(reducido));
  76.         }
  77.     }
  78.  
  79. }
');