COMPLEJOS.c



// OPERACIONES CON NUMEROS COMPLEJOS

#include <stdio.h>
#include <math.h>

struct Complejo
{
	float Real;	// Real
	float Imag;	// Imaginaria
	float Magn;	// Magnitud
	float Angu;	// Angulo
};

void Menu();
void Ejecuta(int*);
void PideDatos();
struct Complejo Lectura(struct Complejo, int*);
void LeeDato(float*, char*);
struct Complejo PolarARectangular(struct Complejo);
struct Complejo RectangularAPolar(struct Complejo);
void Sumar();

struct Complejo CA, CB;

main() { Menu(); printf("\nF I N\n"); }

void Menu()
{
	int Opc = 0;
	while(Opc != 4)
	{
		system("clear");
		printf("1.- Pedir Datos\n");
		printf("2.- Sumar\n");
		printf("3.- Producto\n");
		printf("4.- Salir\n");
		printf("Selecciona una opcion: ");
		scanf("%d", &Opc);
		Ejecuta(&Opc);
	}
	
}

void Ejecuta(int *Opc)
{
	switch (*Opc)
	{
		case 1: PideDatos(); break;
		case 2: Sumar(); break;
		//case 3: Producto(); break;
	}
}

void Sumar()
{
	float Real=0, Imag=0;
	
	Real = CA.Real + CB.Real;
	Imag = CA.Imag + CB.Imag;

	printf("R = %f + j%f\n", Real, Imag);
	scanf("%f", &Real);	// Esperar un momento
}

void PideDatos()
{
	int Formato = 0;

	printf("CA en que formato? (0=Rect, 1=Polar) ");
	scanf("%d", &Formato);
	CA = Lectura(CA, &Formato);

	printf("CB en que formato? (0=Rect, 1=Polar) ");
        scanf("%d", &Formato);
        CB = Lectura(CB, &Formato);

	printf("CA = %f + j%f\n", CA.Real, CA.Imag);
	printf("CB = %f + j%f\n", CB.Real, CB.Imag);
	scanf("%d", &Formato);
}
struct Complejo Lectura(struct Complejo NC, int *Formato)
{
	if (*Formato)
	{
		LeeDato(&NC.Magn, "CA Magnitud ");
		LeeDato(&NC.Angu, "CA Angulo ");
		NC = PolarARectangular(NC);
	}
	else
	{
		LeeDato(&NC.Real, "CA Real ");
		LeeDato(&NC.Imag, "CA Imag ");
		NC = RectangularAPolar(NC);
	}
	return NC;
}

struct Complejo RectangularAPolar(struct Complejo NC)
{
	NC.Magn = sqrt(pow(NC.Real,2) + pow(NC.Imag,2));
	NC.Angu = atan2(NC.Imag, NC.Real);
}

struct Complejo PolarARectangular(struct Complejo NC)
{
	NC.Angu = M_PI * NC.Angu / 180;
	NC.Real = NC.Magn * cos(NC.Angu);
	NC.Imag = NC.Magn * sin(NC.Angu);
	return NC;
}

void LeeDato(float *X, char *CAD)  // (float X, char *CAD)
{
	printf(CAD);
	scanf("%f", X);		// scanf("%f", &X);
}