#include <iostream.h>
#include <stdio.h>
#include <math.h>
#define ZERO 1.0E-20
#define PI 3.141592654
#define true 1
#define false 0
class complex { // º¹¼Ò¼öÇü¿¡ ´ëÇÑ ¿¬»êÀÚÀÇ ´ÙÁßÁ¤ÀÇ.
private :
double x, y;
public :
complex(double r=0, double i=0) { x=r; y=i;
} // »ý¼ºÀÚ
void print();
friend complex operator+(complex, complex);
friend complex operator-(complex, complex);
friend complex operator*(complex, complex);
friend complex operator/(complex, complex);
friend double abs(complex);
friend complex sqrt(complex);
};
void complex::print()
{
printf("%5.2f+%5.2fi", x, y);
}
complex operator+(complex a, complex b) // ´õÇϱ⠿¬»êÀÇ ´ÙÁßÁ¤ÀÇ
{
return (complex(a.x+b.x, a.y+b.y));
}
complex operator-(complex a, complex b) // »©±â ¿¬»êÀÇ ´ÙÁßÁ¤ÀÇ
{
return (complex(a.x-b.x, a.y-b.y));
}
complex operator*(complex a, complex b) // °öÇϱ⠿¬»êÀÇ ´ÙÁßÁ¤ÀÇ
{
return (complex(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x));
}
complex operator/(complex a, complex b) // ³ª´©±â ¿¬»êÀÇ ´ÙÁßÁ¤ÀÇ
{
return ( complex( (a.x*b.x+a.y*b.y)/(b.x*b.x+b.y*b.y), \
(a.y*b.x-a.x*b.y)/(b.x*b.x+b.y*b.y)) );
}
double abs(complex a)
{
return (sqrt((a.x*a.x+a.y*a.y)));
}
complex sqrt(complex a)
{
double r, t;
double hp = 0.5 * PI;
if (fabs(a.x) <= ZERO) {
if (fabs(a.y) <= ZERO) {
r=0.; t=0.;
} else {
t = hp;
if (a.y < 0.) t = -t;
r = fabs(a.y);
}
} else {
r = sqrt(a.x*a.x+a.y*a.y);
if(fabs(a.y) < ZERO) {
t = 0.;
if(a.x < 0.) t = PI;
}
}
return ( complex(sqrt(r)*cos(0.5*t), sqrt(r)*sin(0.5*t))
);
}
void main()
{
complex a, b;
a = complex(1,3);
b=sqrt(a);
b.print();
}