// Op4: Operaciones números complejos
import java.util.Random;
import java.util.Scanner;
public class ComplexApp1{
static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
System.out.println("Bienvenido a su aplicación de números complejos");
ComplexDAO cd = new ComplexDAO();
Complex m = cd.leerComplex();
Complex n = cd.leerComplex();
System.out.printf("m = %.0f + i%.0f\n",m.x, m.y);
System.out.printf("n = %.0f + i%.0f\n",n.x, n.y);
operaciones(m, n);
}
static void operaciones(Complex m, Complex n){
int op; // op = opción
float mod, dist;
Complex r;
do {
op = menu();
switch(op){ // Ejecute de operación seleccionada.
case 1: r = Complex.suma(m,n);
System.out.printf("%.2f + i%.2f + %.2f + i%.2f = %.2f + i%.2f\n",
m.x, m.y, n.x, n.y, r.x, r.y); break;
case 2: r = Complex.resta(m,n);
System.out.printf("%.2f + i%.2f - %.2f + i%.2f = %.2f + i%.2f\n",
m.x, m.y, n.x, n.y, r.x, r.y); break;
case 3: r = Complex.multiplicacion(m,n);
System.out.printf("%.2f + i%.2f * %.2f + i%.2f = %.2f + i%.2f\n",
m.x, m.y, n.x, n.y, r.x, r.y); break;
case 4: if(m.x==0 && m.y==0) System.out.println("Lo sentimos no se puede dividir");
else {
r = Complex.division(m, n);
System.out.printf("%.2f + i%.2f / %.2f + i%.2f = %.2f + i%.2f\n",
m.x, m.y, n.x, n.y, r.x, r.y);
}
break;
case 5: mod = Complex.modulo(m);
System.out.printf("||%.2f + i%.2f|| * %.2f\n", m.x, m.y, mod); break;
case 6: dist = Complex.distancia(m,n);
System.out.printf("Distancia (%.2f + i%.2f) y (%.2f + i%.2f) = %.2f\n",
m.x, m.y, n.x, n.y, dist); break;
default: System.out.println("Gracias por su visita");
}
} while(op!=7);
}
static int menu(){
int op;
System.out.printf("\nOperación que requiere:\n");
System.out.printf("1) Sumar: m + n\n");
System.out.printf("2) Restar: m – n\n");
System.out.printf("3) Multiplicar: m * n\n");
System.out.printf("4) Dividir: m/n\n");
System.out.printf("5) Módulo de un número complejo \n");
System.out.printf("6) Distancia entre 2 complejos \n");
System.out.printf("7) Salir\n");
return Validator.getInt(sc, "Elija su opción: ", 1, 7);
}
}
class Complex{
float x, y;
Complex(float x, float y){
this.x = x;
this.y = y;
}
static Complex suma(Complex c1, Complex c2){
return new Complex(c1.x+c2.x, c1.y+c2.y);
}
static Complex resta(Complex c1, Complex c2){
return new Complex(c1.x-c2.x, c1.y-c2.y);
}
static Complex multiplicacion(Complex c1, Complex c2){
return new Complex(c1.x*c2.x - c1.y*c2.y, c1.x*c2.y + c1.y*c2.x);
}
static Complex division(Complex c1, Complex c2){
Complex cc = multiplicacion(c1, new Complex(c2.x, -c2.y));
float div = c2.x*c2.x + c2.y*c2.y;
return new Complex(cc.x/div, cc.y/div);
}
static float modulo(Complex c){
return((float)Math.sqrt(c.x*c.x+c.y*c.y));
}
static float distancia(Complex c1, Complex c2){
return modulo(resta(c1, c2));
}
}
class ComplexDAO {
Random ran = new Random();
Complex leerComplex(){
return new Complex(ran.nextInt(5), ran.nextInt(10));
}
}
class Validator {
public static String getString(Scanner sc, String prompt){
System.out.print(prompt);
String s = sc.next();
sc.nextLine();
return s;
}
public static int getInt(Scanner sc, String prompt){
int i = 0 ;
boolean isValid = false;
while (isValid == false){
System.out.print(prompt);
if (sc.hasNextInt()){
i = sc.nextInt();
isValid = true;
} else System.out.println("Err()rl Invalid integer value. Try again.");
sc.nextLine();
}
return i;
}
public static int getInt(Scanner sc, String prompt, int min, int max){
int i = 0;
boolean isValid = false;
while (isValid == false){
i = getInt(sc, prompt);
if (i < min) System.out.println("Error! El número debe ser mayor que " + min + ".");
else if (i > max) System.out.println("Error El número debe ser menor que " + max + ".");
else isValid = true;
}
return i ;
}
public static float getFloat(Scanner sc, String prompt){
float d = 0 ;
boolean isValid = false;
while (isValid == false){
System.out.print(prompt);
if (sc.hasNextFloat()){
d = sc.nextFloat();
isValid = true;
} else System.out.println("Error! Valor decimal no válido. Intente de nuevo.");
sc.nextLine();
}
return d;
}
public static float getFloat(Scanner sc, String prompt, float min, float max) {
float d = 0 ;
boolean isValid = false;
while (isValid == false){
d = getFloat(sc, prompt);
if (d < min) System.out.println("Error! El número debe ser mayor o igual que " + min + ".");
else if (d > max) System.out.println("Error! El número debe ser menor o igual que " + max + ".");
else isValid = true;
}
return d;
}
}