VS2019 C++实测可用
#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,const Complex& c2);
friend Complex operator-(Complex& c1,const Complex& c2);
friend Complex operator*(Complex& c1, const Complex& c2);
friend Complex operator/(Complex& c1, const 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, const Complex& c1);
friend Complex operator-(double c, const Complex& c1);
friend Complex operator*(double c, const Complex& c1);
friend Complex operator/(double c, const Complex& c1);
void display();
Complex ckf(Complex& c1,int n);
double cabs();
double csqrt();
private:
};
Complex operator+(Complex& c1,const Complex& c2)
{
return Complex(c2.real + c1.real, c2.imag + c1.imag);
}
Complex operator-(Complex& c1, const Complex& c2)
{
return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
Complex operator*(Complex& c1, const Complex& c2)
{
return Complex((c1.real * c2.real - c1.imag * c2.imag), (c1.real * c2.imag + c1.imag * c2.real));
}
Complex operator/(Complex& c1, const 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, const Complex& c1)
{
return Complex(c + c1.real, c1.imag);
}
Complex operator-(double c, const Complex& c1)
{
return Complex(c-c1.real, -c1.imag);
}
Complex operator*(double c, const Complex& c1)
{
return Complex((c * c1.real), (c1.imag * c));
}
Complex operator/(double c, const 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;
}
检验部分
#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();
c3 = c1 + c2;
cout << "复数与复数:" << endl;
cout << "c1+c2=";
c3.display();
c3 = c1 - c2;
cout << "c1-c2=";
c3.display();
c3 = c1 * c2;
cout << "c1*c2=";
c3.display();
c3 = c1 / c2;
cout << "c1/c2=";
c3.display();
cout << endl;
cout << "复数与实数:" << endl;
cout << "c=" << c << endl;
cout << "c1=";
c1.display();
c3 = c1 + c;
cout << "c1+c=";
c3.display();
c3 = c1 - c;
cout << "c1-c=";
c3.display();
c3 = c1 * c;
cout << "c1*c=";
c3.display();
c3 = c1 / c;
cout << "c1/c=";
c3.display();
cout << endl;
cout << "实数与复数:" << endl;
cout << "c=" << c << endl;
cout << "c2=";
c2.display();
c3 = c + c2;
cout << "c + c2=";
c3.display();
c3 = c - c2;
cout << "c - c2=";
c3.display();
c3 = c * c2;
cout << "c * c2=";
c3.display();
c3 = c / c2;
cout << "c / c2=";
c3.display();
cout << "自定义:" << endl;
cout << "实部:" << endl;
cout << c3.real;
cout << "\n虚部:" << endl;
cout << c3.imag;
cout << "复数开方:" << endl;
c3.real=-1;
c3.imag = 0;
cout << "\n" << endl;
cout << "c3:" << endl;
c3.display();
c3 = csqrt(c3);
c3.display();
cout << "混合运算:" << endl;
Complex c4;
c1.display();
c2.display();
c3 = c1 + c2;
c4 = c1 * c2;
c4 = c + (c1 * c2);
c4.display();
return 0;
}