64,637
社区成员
发帖
与我相关
我的任务
分享
//下面定义用于测试的main()函数
#include "complex.h"
#include <iostream>
using namespace std;
int main()
{
double c = 5;
Complex c1(1,-2), c2(3,-4), c3;
cout << "c="<<c<<endl;
cout << "c1=";
c1.display();
cout << "c2=";
c2.display();
cout << "混合运算:" << endl;
Complex c4;
c3 = c1 + c2;
c4 = c1 * c2;
c4 = c1 + (c2 * c3);
return 0;
}
#include <iostream>
#define PI acos(-1)
using namespace std;
class Complex
{
public:
double real;
double imag;
// double cabs = double(sqrt(real * real + imag * imag));
// double ctheta = atan(imag / real);
Complex()
{
real = 0;
imag = 0;
}
Complex(double r, double i)
{
real = r;
imag = i;
}
friend Complex operator+(Complex& c1, Complex& c2);
friend Complex operator-(Complex& c1, Complex& c2);
friend Complex operator*(Complex& c1, Complex& c2);
friend Complex operator/(Complex& c1, Complex& c2);
friend Complex operator+(Complex& c1, double c);
friend Complex operator-(Complex& c1, double c);
friend Complex operator*(Complex& c1, double c);
friend Complex operator/(Complex& c1, double c);
friend Complex operator+(double c, Complex& c1);
friend Complex operator-(double c, Complex& c1);
friend Complex operator*(double c, Complex& c1);
friend Complex operator/(double c, Complex& c1);
void display();
Complex ckf(Complex& c1,int n);
double cabs();
double csqrt();
private:
};
Complex operator+(Complex& c1, Complex& c2)
{
return Complex(c2.real + c1.real, c2.imag + c1.imag);
}
Complex operator-(Complex& c1, Complex& c2)
{
return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
Complex operator*(Complex& c1, Complex& c2)
{
return Complex((c1.real * c2.real - c1.imag * c2.imag), (c1.real * c2.imag + c1.imag * c2.real));
}
Complex operator/(Complex& c1, Complex& c2)
{
Complex c;
c.real = ((c1.real * c2.real + c1.imag * c2.imag) / ((c2.real * c2.real) + (c2.imag * c2.imag)));
c.imag = ((c1.imag * c2.real - c1.real * c2.imag) / ((c2.real * c2.real) + (c2.imag * c2.imag)));
return c;
}
Complex operator+(Complex& c1, double c)
{
return Complex(c1.real+c, c1.imag);
}
Complex operator-(Complex& c1, double c)
{
return Complex(c1.real-c, c1.imag);
}
Complex operator*(Complex& c1, double c)
{
return Complex((c * c1.real), (c1.imag * c));
}
Complex operator/(Complex& c1, double c)
{
Complex c3;
c3.real = c1.real / c;
c3.imag = c1.imag / c;
return c3;
}
Complex operator+(double c, Complex& c1)
{
return Complex(c + c1.real, c1.imag);
}
Complex operator-(double c, Complex& c1)
{
return Complex(c-c1.real, -c1.imag);
}
Complex operator*(double c, Complex& c1)
{
return Complex((c * c1.real), (c1.imag * c));
}
Complex operator/(double c, Complex& c1)
{
Complex c3;
c3.real = (c * c1.real) / (c1.real * c1.real + c1.imag * c1.imag);
c3.imag = (-c * c1.imag) / (c1.real * c1.real + c1.imag * c1.imag);
return c3;
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
double cabs(Complex& c1)
{
return(sqrt(c1.real * c1.real + c1.imag * c1.imag));
}
Complex csqrt(Complex& c1)
{
Complex c3;
double ctheta;
if (c1.real > 0)
{
ctheta= atan(c1.imag / c1.real);
}
else if (c1.real == 0 && c1.imag > 0)
{
ctheta = PI / 2;
}
else if (c1.real == 0 && c1.imag < 0)
{
ctheta = -PI / 2;
}
else if (c1.real < 0 && c1.imag >= 0)
{
ctheta = atan(c1.imag / c1.real) + PI;
}
else if (c1.real < 0 && c1.imag < 0)
{
ctheta = atan(c1.imag / c1.real) - PI;
}
c3.real = sqrt(cabs(c1)) * cos(ctheta / 2);
c3.imag = sqrt(cabs(c1)) * sin(ctheta / 2);
return c3;
}