代码只给展示这么长,分上下集,就将就着看吧
#include<iostream>
#include<cmath>
using namespace std;
/*
template<class T> class complex;
template<> class complex<float>;
template<> class complex<double>;
template<> class complex<long double>;
*/
/*
long double sqrt_long_double(const long double& n){
long double x=n;
long double px=0;
while(px!=x){
px=x;
x=(x+n/x)/2.0;
}
return x;
}
*/
template<class T> //ONLY: float,double,long double
class complex{
private:
T m_arg; //Arg(z)
T m_abs; //sqrt(Re(z)^2+Im(z)^2)
T m_real; //Re(z)
T m_imag; //Im(z)
public:
// const T e=2.718281828459045235;
// const T pi=3.141592653589793438;
// const T INF=1.7976931348e+150;
complex(long double a=0.0,long double b=0.0,bool typ=true){ //true=real+imag,false=arg+abs
if(typ){
this->m_real=a;
this->m_imag=b;
if(a==0){
if(b>0){
this->m_arg=3.141592653589793438/2;
}
else if(b==0){
this->m_arg=0;
}
else{
this->m_arg=-3.141592653589793438/2;
}
}
else{
this->m_arg=atan(b/a);
if(a<0&&b>=0){
this->m_arg=this->m_arg+3.141592653589793438;
}
else if(a<0&&b<0){
this->m_arg=this->m_arg-3.141592653589793438;
}
}
this->m_abs=sqrt(a*a+b*b);
}
else{
this->m_arg=a;
this->m_abs=b;
this->m_real=b*cos(a);
this->m_imag=b*sin(a);
}
}
complex(const complex<T>& x){
this->m_abs=x.m_abs;
this->m_arg=x.m_arg;
this->m_imag=x.m_imag;
this->m_real=x.m_real;
}
~complex(){
}
T real(){
return this->m_real;
}
T imag(){
return this->m_imag;
}
T arg(){
return this->m_arg;
}
T abs(){
return this->m_abs;
}
T norm(){
return this->m_real*this->m_real+this->m_imag*this->m_imag;
}
complex conj(){
return complex(m_real,-m_imag);
}
const complex operator+(const complex& t){
double a=this->m_real;
double b=this->m_imag;
double c=t.m_real;
double d=t.m_imag;
complex res(a+c,b+d);
return res;
}
const complex operator-(const complex& t){
double a=this->m_real;
double b=this->m_imag;
double c=t.m_real;
double d=t.m_imag;
complex res(a-c,b-d);
return res;
}
const complex operator*(const complex& t){
double a=this->m_real;
double b=this->m_imag;
double c=t.m_real;
double d=t.m_imag;
complex res(a*c-b*d,a*d+b*c);
return res;
}
const complex operator/(const complex& t){
double a=this->m_real;
double b=this->m_imag;
double c=t.m_real;
double d=t.m_imag;
if(c==0&&d==0){
throw runtime_error("0 CANNOT BE USED AS A DIVISOR!");
}
else{
complex res((a*c+b*d)/(c*c+d*d),(b*c-a*d)/(c*c+d*d));
return res;
}
}
bool operator==(const complex& t){
return this->m_real==t.m_real&&this->m_imag==t.m_imag;
}
bool operator<(const complex& t){
if(this->m_imag!=0||t.m_imag!=0){
throw runtime_error("NO COMPARE RULE BETWEEN 2 COMPLEX!");
}
else{
return this->m_real<t.m_real;
}
}
bool operator>(const complex& t){
if(this->m_imag!=0||t.m_imag!=0){
throw runtime_error("NO COMPARE RULE BETWEEN 2 COMPLEX!");
}
else{
return this->m_real>t.m_real;
}
}
bool operator<=(const complex& t){
if(this->m_imag!=0||t.m_imag!=0){
throw runtime_error("NO COMPARE RULE BETWEEN 2 COMPLEX!");
}
else{
return this->m_real<=t.m_real;
}
}
bool operator>=(const complex& t){
if(this->m_imag!=0||t.m_imag!=0){
throw runtime_error("NO COMPARE RULE BETWEEN 2 COMPLEX!");
}
else{
return this->m_real>=t.m_real;
}
}
friend istream& operator>>(istream& in,complex& z){
in>>z.m_real>>z.m_imag;
return in;
}
friend ostream& operator<<(ostream& out,const complex& z){
if(z.m_real!=0.0){
out<<to_string(z.m_real);
if(z.m_imag>0.0){
out<<"+";
}
}
if(z.m_imag!=0.0&&z.m_imag!=-0.0&&z.m_imag!=-1.0&&z.m_imag!=1.0){
out<<to_string(z.m_imag)<<"i";
}
if(z.m_imag==1.0){
out<<"i";
}
if(z.m_imag==-1.0){
out<<"-i";
}
if(z.m_real==0.0&&z.m_imag==0.0){
out<<z.m_real;
}
return out;
}
};
template<class T> //ONLY: float,double,long double
const complex<T> type_i=complex<T>(0.0,1.0);
template<class T> //ONLY: float,double,long double
T Re(complex<T> z){
return z.real();
}
template<class T> //ONLY: float,double,long double
T Im(complex<T> z){
return z.imag();
}
template<class T> //ONLY: float,double,long double
T abs(complex<T> z){
return z.abs();
}
template<class T> //ONLY: float,double,long double
T Arg(complex<T> z){
return z.arg();
}
template<class T> //ONLY: float,double,long double
//sqrt(z) has 2 solutions,this function shows the first one(+,+),but another one(-,-) does not show
complex<T> sqrt(complex<T> z){ // come from: Euler's formula.
T r=z.real();
T i=z.imag();
if(r==0.0){
T t=sqrt(0.5*abs(i));
return complex<T>(t,i<0?-t:t);
}
else if(i!=0.0){
T t=2*z.abs(); // Thank https:// www.bilibili.com/opus/810197348175052823/ gives me the proof
return complex<T>(sqrt(t+2*r)/2,sqrt(t-2*r)*abs(i)/2*i);
}
else{
if(r>=0.0){
return complex<T>(sqrt(r),0.0);
}
else{
return complex<T>(0.0,sqrt(-r));
}
}
}
