Vous êtes sur la page 1sur 4

//Archivos de cabecera

#include <iostream>
#include <cmath>

using namespace std;

//Estructura
struct Complejo
{
double Re;
double Im;
};

//Prototipos
int Opcion(void);
bool ValO(const int Op);
Complejo Leer(void);
bool ValC(const double Re, const double Im);
Complejo Suma(const Complejo* AptC, const Complejo* AptC2);
Complejo Resta(const Complejo* AptC, const Complejo* AptC2);
Complejo Multi(const Complejo* AptC, const Complejo* AptC2);
Complejo Divi(const Complejo* AptC, const Complejo* AptC2);
void Imprimir(const Complejo* AptC, const Complejo* AptC2, const Complejo* AptC3,
char Signo);

int main(void)
{
Complejo* AptC=new Complejo;
Complejo* AptC2=new Complejo;
Complejo* AptC3=new Complejo;
int O;
string Operacion;
char Sig;
char Res;
do
{
O=Opcion();

if (O!=5)
{
*AptC=Leer();
*AptC2=Leer();
}

switch (O)
{
case 1:
*AptC3=Suma(AptC,AptC2);
Sig='+';
Operacion="Suma";
Imprimir(AptC, AptC2, AptC3, Sig);
break;

case 2:
*AptC3=Resta(AptC,AptC2);
Sig='-';
Operacion="Resta";
Imprimir(AptC, AptC2, AptC3, Sig);
break;
case 3:
*AptC3=Multi(AptC,AptC2);
Sig='*';
Operacion="Multiplicasion";
Imprimir(AptC, AptC2, AptC3, Sig);
break;

case 4:
*AptC3=Divi(AptC,AptC2);
Sig='/';
if(AptC->Im!=0&&AptC2->Im!=0)
{
Operacion="Division";
Imprimir(AptC, AptC2, AptC3, Sig);
}
break;

case 5:
cout<<endl;
cout<<endl<<"FIN DEL PROGRAMA";

break;

default:
cout<<endl<<"NO SE PUEDE :,(";
}

if(AptC2==0&&O==4||O==5)

cout<<endl<<"NO SE PUEDE";

cout<<endl<<endl<<"QUIERES VOLVER A INTENTAR(S/N)?";


cin>>Res;
Res=toupper(Res);
}while(Res=='S');
cout<<endl;

delete AptC;
delete AptC2;
delete AptC3;

system ("PAUSE");
return 0;
}

//Funciones

int Opcion(void)
{
int O;

bool Piano=false;

while(Piano==false)
{
cout<<endl<<"\t\t\t\tCOMPLEJOS";
cout<<endl<<"1.-SUMA DE COMPLEJOS";
cout<<endl<<"2.-RESTA DE COMPLEJOS";
cout<<endl<<"3.-MULTIPLICACION DE COMPLEJOS";
cout<<endl<<"4.-DIVISION DE COMPLEJOS";
cout<<endl<<"5.-SALIR";
cout<<endl<<endl<<"ELIGE QUE OPERACION VAS A REALIZAR: ";
cin>>O;

Piano=ValO(O);
}
return O;
}

bool ValO(const int O)


{
bool Piano=true;

if (O<1||O>5)
Piano=false;

return Piano;
}

Complejo Leer(void)
{
Complejo M;

cout<<endl<<"INGRESA NUMERO REAL: ";


cin>>M.Re;
cout<<endl<<"INGRESA NUMERO IMAGINARIO: ";
cin>>M.Im;

return M;
}

Complejo Suma(const Complejo* AptC, const Complejo* AptC2)


{
Complejo M3;

M3.Re=AptC->Re+AptC2->Re;
M3.Im=AptC->Im+AptC2->Im;

return M3;
}

Complejo Resta(const Complejo* AptC, const Complejo* AptC2)


{
Complejo M3;

M3.Re=AptC->Re-AptC2->Re;
M3.Im=AptC->Im-AptC2->Im;

return M3;
}

Complejo Multi(const Complejo* AptC, const Complejo* AptC2)


{
Complejo M3;

M3.Re=(AptC->Re*AptC2->Re)-(AptC->Im*AptC2->Im);
M3.Im=(AptC->Re*AptC2->Im)+(AptC->Im*AptC2->Re);
return M3;
}

Complejo Divi(const Complejo* AptC, const Complejo* AptC2)


{
Complejo M3;
bool Piano;

Piano=ValC(AptC->Re, AptC2->Im);
if(Piano==false)
{
cout<<endl<<"NO SE PUEDE REALIZAR LA OPERACION DEVIDO A QUE EL NUMERO IMAGINARIO ES
CERO";
cout<<endl;
M3=*AptC;
}

else
{
M3.Re=((AptC->Re*AptC2->Re)+(AptC->Im*AptC2->Im))/(pow(AptC2->Re,2)+pow(AptC2-
>Im,2));
M3.Im=((AptC->Im*AptC2->Re)-(AptC->Re*AptC2->Im))/(pow(AptC2->Re,2)+pow(AptC2-
>Im,2));
}

return M3;
}

bool ValC(const double Re, const double Im)


{
bool Piano=true;

if (Re==0||Im==0)
Piano=false;

return Piano;
}

void Imprimir(const Complejo* AptC, const Complejo* AptC2, const Complejo* AptC3,
char Sig)
{
cout<<endl;
cout<<"("<<AptC->Re<<"+"<<AptC->Im<<"i)"<<Sig<<"("<<AptC2->Re<<"+"<<AptC2-
>Im<<"i)"<<"="<<AptC3->Re<<"+("<<AptC3->Im<<"i)";

Vous aimerez peut-être aussi