运算符重载实现复数相关运算
设计一个复数类,可以实现有关复数操作,并应用该复数类求解一元二次方程的两个复根
complex.h
#include <iostream>
#include<math.h>
using namespace std;
class CComplex
{
private:
double real, image;//复数的内部表示
public:
//构造函数定义
CComplex(void) { real = 0;image = 0; }//默认构造函数
CComplex(double x) { real = x;image = 0; }//利用一个实数构造复数
CComplex(double x, double y) { real = x;image = y; }//利用实部及虚部系数构造复数
CComplex(const CComplex& z) //拷贝构造函数
{
real = z.real; image = z.image;
}
~CComplex(void) {}//析构函数
//有关加减乘除运算的重载
CComplex operator + (const CComplex& z) const;//两复数相加
friend CComplex operator +(double x, const CComplex& z);//实数加复数
CComplex operator -(const CComplex& z) const;//两复数相减
friend CComplex operator -(double x, const CComplex& z);//实数减复数
CComplex operator *(const CComplex& z) const;//两复数相乘
friend CComplex operator *(double x, const CComplex& z);//实数乘复数
CComplex operator /(const CComplex& z) const;//两复数相除
friend CComplex operator /(double x, const CComplex& z);//实数除以复数
//有关复合赋值运算的重载
CComplex& operator =(const CComplex& z);
CComplex& operator +=(const CComplex& z);
CComplex& operator -=(const CComplex& z);
CComplex& operator *=(const CComplex& z);
CComplex& operator /=(const CComplex& z);
int operator ==(const CComplex& z) const;//判断两个复数是否相等
int operator !=(const CComplex& z) const;//判断两个复数是否不等
CComplex operator!()const;//求共轭复数运算
CComplex operator-()const;//求复数相反数运算
void show()
{
cout << real;
if (image >= 0)
{
cout << "+";
}
cout <<image<< "i";
}
//取模
double getNorm()const
{
return sqrt(real * real + image * image);
}
//取幅角
double getAngle()const;
//取实部
double getReal()const
{
return real;
}
//取虚部系数
double getImage()const
{
return image;
}
};
comple.app
#include"complex.h"
//两复数相加
CComplex CComplex::operator + (const CComplex& z) const
{
CComplex tmp(real + z.real, image + z.image);
return tmp;
}
//两复数相减
CComplex CComplex::operator - (const CComplex& z) const
{
CComplex tmp(real - z.real, image - z.image);
return tmp;
}
//两复数相乘
//(a+bi)∗(c+di)=(ac−bd)+(ad+bc)i
CComplex CComplex::operator * (const CComplex& z) const
{
CComplex tmp(real * z.real - image * z.image, real * z.image + image * z.real);
return tmp;
}
//两复数相除
//(a + bi) / (c + di) = (ac + bd) / (c2 + d2) + (bc − ad) / (c2 + d2)i
CComplex CComplex::operator / (const CComplex& z) const
{
CComplex tmp;
tmp = *this * !z;
double x = z.real * z.real + z.image * z.image;
tmp.real /= x;
tmp.image /= x;
return tmp;
}
//实数加复数
CComplex operator +(double x, const CComplex& z) {
CComplex tmp(x + z.real, z.image);
return tmp;
}
//实数减复数
CComplex operator -(double x, const CComplex& z) {
CComplex tmp(x - z.real, -z.image);
return tmp;
}
//实数乘复数
CComplex operator *(double x, const CComplex& z) {
CComplex tmp(x * z.real, x*z.image);
return tmp;
}
//实数除复数
CComplex operator /(double x, const CComplex& z) {
CComplex tmp(x);
return tmp/z;
}
//有关复数的复合赋值运算的实现
CComplex& CComplex::operator = (const CComplex& z)
{
real = z.real;
image = z.image;
return *this;
}
CComplex& CComplex::operator += (const CComplex& z)
{
real += z.real;
image += z.image;
return *this;
}
CComplex& CComplex::operator -= (const CComplex& z)
{
real -= z.real;
image -= z.image;
return *this;
}
CComplex& CComplex::operator *= (const CComplex& z)
{
*this = *this * z;
return *this;
}
CComplex& CComplex::operator /= (const CComplex& z)
{
*this = *this / z;
return *this;
}
//有关复数的关系运算的实现
int CComplex::operator ==(const CComplex& z) const {
return real == z.real && image == z.image;
}
int CComplex::operator !=(const CComplex& z) const {
return *this==z;
}
//取共轭复数运算的实现
CComplex CComplex::operator !()const
{
CComplex tmp(real, -image);
return tmp;
}
//求复数相反数运算的实现
CComplex CComplex::operator -()const
{
CComplex tmp(-real, -image);
return tmp;
}
//取复数的幅角
double CComplex::getAngle()const
{
const double PI = 3.1415926;
double nrm = getNorm();
if (nrm < 1e-6) return 0;
double a = asin(fabs(image) / nrm);
if (image > 0)
{
if (real > 0) a = a;
else a = PI - a;
}
else if (image < 0)
{
if (real > 0) a = 2 * PI - a;
else a = PI + a;
}
if (real > 0) a = 0;
else a = PI;
return a;
}
main.cpp
#include"complex.h"
#include<math.h>
#include<iostream>
using namespace std;
//定义sqlt函数,计算复数的平方根
CComplex sqlt(const CComplex& z)
{
double n = sqrt(z.getNorm()), angle = z.getAngle() / 2;
CComplex d(n * cos(angle), n * sin(angle));
return d;
}
int main()
{
//ax^2+bx+c=0
//求解x^2+x+3=0的根
CComplex a(1), b(1), c(3), z1, z2, d;
d = b * b - 4 * a * c;
z1 = (-b + sqlt(d)) / (2 * a);
z2= (-b - sqlt(d)) / (2 * a);
cout << "z1=";z1.show();cout << endl;
cout << "z2=";z2.show();cout << endl;
system("pause");
return 0;
}
转载自:黑凤梨の博客