Complejo.java



class Complejo
{
	// ======================================================================
	// Objetivo:	Permitir el uso y las operaciones entre nUmeros complejos
	// Autor:	J. Rafael R. Ochoa
	// Fecha:	Nov-7-2003, Dic-17-2003
	// Derechos:	SID - FIE - UMSNH
	// ======================================================================

	// DeclaraciOn de los elementos miembro
	double Real, Imag;

	// Se recibe un nUmero complejo en formato rectangular o polar en una cadena
	// Ej.
	// 	45.98+j13.23	caso rectangular
	// 	13.15 [60]	caso polar
	//
	// Observaciones:
	// 	No importa si se deja espacio o no entre ambas partes del nUmero y el operador
	//	El Angulo estA en grados si se trata en formato polar
	// 	Se supone que ambas partes del nUmero complejo son introducidos correctamente -por lo pronto-
	// 	23	==> parte vAlida -solo parte entera-
	// 	75.23	==> parte vAlida -solo un punto decimal-
	// 	74.2.3	==> parte invAlida -2 puntos decimales-

	// ---------------------------------------------------------------------------------------------------
	// METODOS RECONOCIDOS:
	// ===================================================================================================
	// ===================================================================================================
	// void Imprime()							==>	Imprime
	// Complejo Suma(Complejo N1, Complejo N2)				==>	Suma
	// Complejo Resta(Complejo N1, Complejo N2)				==>	Resta
	// Complejo Potencia(Complejo num, int pot)				==>	Potencia de un nUmero
	// Complejo Producto(Complejo Num_1, Complejo Num_2)			==>	Producto
	// Complejo Division(Complejo Numerador, Complejo Denominador)		==>	DivisiOn
	// Complejo Conjugado(Complejo Num)					==>	Conjugado
	// Complejo Negativo(Complejo Num)					==>	Cambio de signo
	// Complejo Reciproco(Complejo Num)					==>	Reciproco
	// ---------------------------------------------------------------------------------------------------

	//_________________________________________________
	// C   O   N   S   T   R   U  C   T   O   R   E   S
	//-------------------------------------------------

	Complejo()
	{
		this("0+j0");
	}

	Complejo(String CADENA)
	{
		// Se verifica si estA en forma rectangular
		if (CADENA.toUpperCase().indexOf('J') >= 0)
			Recibe_Rectangular(CADENA.toUpperCase());
		// o en polar
		if (CADENA.indexOf('[') > 0)
			Recibe_Polar(CADENA);
		if (CADENA.toUpperCase().indexOf('J') < 0 && (CADENA.indexOf('[') < 0))
			// solo parte real = rectangular
			Recibe_RECT(CADENA);

	}

	//_________________________________________
	// A  L  M  A  C  E  N  A  M  I  E  N  T  O
	//-----------------------------------------

	// SOlo se recibiO la parte real
	void Recibe_RECT(String CAD)
	{
		Real = Double.parseDouble(CAD);
		Imag = 0;
	}

	// La cadena recibida estA en rectangular y se separa en sus partes
	void Recibe_Rectangular(String CAD)
	{
		int PosicionDeJ = CAD.toUpperCase().indexOf('J');

		// Existe solo parte imaginaria y sin signo. -(+)-
		if (CAD.toUpperCase().indexOf('J') == 0)
		{
			Real = 0;
			Imag = Double.parseDouble(CAD.substring(1, CAD.length()));
		}
		// Existe solo parte imaginaria y con signo. (+ -)
		if (CAD.toUpperCase().indexOf('J') == 1)
		{
			Real = 0;
			Imag = Double.parseDouble(CAD.substring(2, CAD.length()));
			if (CAD.indexOf('-') == 0)
				Imag *= -1;
		}
		// Existen ambas partes (Real e Imaginario)
		if (CAD.toUpperCase().indexOf('J') > 1)
		{
			Real = Double.parseDouble(CAD.substring(0, PosicionDeJ-1));
			Imag = Double.parseDouble(CAD.substring(PosicionDeJ+1, CAD.length()));
			if (CAD.lastIndexOf('-') > 0)
				Imag *= -1;
		}
	}

	// La cadena recibida estA en polar y se separa en sus partes
	void Recibe_Polar(String CAD)
	{
		int Pos_O = CAD.indexOf('[');
		int Pos_C = CAD.indexOf(']');
		double Mag = Double.parseDouble(CAD.substring(0, Pos_O));
		double Ang = Double.parseDouble(CAD.substring(Pos_O+1, Pos_C));
		Real = Mag*Math.cos(Math.toRadians(Ang));
		Imag = Mag*Math.sin(Math.toRadians(Ang));
	}

	//________________________________________________________________________________________
	// H	E	R	R	A	M	I	E	N	T	A	S
	//----------------------------------------------------------------------------------------

	// Crea un Complejo nuevo como cadena
/*	String CreaComplejo(double Real, double Imag)
	{
		return (Imag < 0) ? Real + "-J" + Math.abs(Imag) : Real + "+J" + Math.abs(Imag);
	}*/
	String CreaComplejo(double Real, double Imag)	{ return (Imag < 0) ? CC_NEG(Real, Imag) : CC_POS(Real, Imag); }
	String CC_NEG(double Real, double Imag)		{ return (Imag != 0) ? Real + "-J" + Math.abs(Imag) : Real + ""; }
	String CC_POS(double Real, double Imag)		{ return (Imag != 0) ? Real + "+J" + Math.abs(Imag) : Real + ""; }

	Complejo Negativo(Complejo Num)			{ return new Complejo(CreaComplejo(Num.Real * -1, Num.Imag * -1)); }	// Cambio de signo
	Complejo Conjugado(Complejo Num)		{ return new Complejo(CreaComplejo(Num.Real, Imag * -1)); }		// Conjugado
	Complejo Reciproco(Complejo Num)		{ return Division(new Complejo("1"), Num); }				// Reciproco
	void	Imprime()				{ System.out.println(CreaComplejo(Real, Imag)); }			// Imprime
				//COMPLEJO += Math.sqrt(Real*Real + Imag*Imag) + " [" + Math.toDegrees(Math.atan(Imag/Real)) + " ]";
	Complejo Suma(Complejo N1, Complejo N2)		{ return new Complejo(CreaComplejo(N1.Real + N2.Real, N1.Imag + N2.Imag)); } // Suma
	Complejo Resta(Complejo N1, Complejo N2)	{ return new Complejo(CreaComplejo(N1.Real - N2.Real, N1.Imag - N2.Imag)); } // Resta
	Complejo Producto(Complejo N1, Complejo N2)	{										// Producto
		return new Complejo(CreaComplejo(N1.Real * N2.Real + N1.Imag * N2.Imag*-1, N1.Imag * N2.Real + N2.Imag * N1.Real)); }

	// Potencia
	Complejo Potencia(Complejo Num, int Orden)
	{
		if (Orden == 0) return new Complejo("1");
		return Producto(Num, Potencia(Num, --Orden));
	}

	// Division
	Complejo Division(Complejo Numerador, Complejo Denominador)
	{
		Complejo Num = Producto(Numerador, Conjugado(Denominador));
		double Divisor = Denominador.Real * Denominador.Real + Denominador.Imag * Denominador.Imag*-1;
		return new Complejo(CreaComplejo(Num.Real/Divisor, Num.Imag/Divisor));
	}
}