复数类:
**复数简单的运算,复数x被定义为二元有序实数对(a,b);记为z=a+bi;i是虚数单位。
**在复数a+bi中,a=Re(z)成为实部,b=lm(z)称为虚部。当虚部为0时,这个虚数可以视为实数。当虚部部位0,实部为0时,称z为纯虚数。
头文件:
#ifndef _COMPLEX_H__
#define _COMPLEX_H__
#include<stdlib.h>
#include<iostream>
using namespace std;
//复数类的申明
class Complex
{
public:
Complex(double real = 0.0, double image = 0.0);
Complex(const Complex& z);
~Complex();
Complex& operator=(Complex& z);
public:
Complex operator+(Complex& z);
Complex operator-(Complex& z);
public:
Complex& operator++();
Complex operator++(int);
bool operator==(Complex&z);
bool operator<(Complex&z);
bool operator<=(Complex&z);
bool operator>(Complex&z);
bool operator>=(Complex&z);
bool operator!=(Complex&z);
public:
void display();
private:
double _real;
double _image;
};
#endif// _COMPLEX_H__
函数实现部分:
#define _CRT_SECURE_NO_WARNINGS 1
#include"complex.h"
//构造函数
Complex::Complex(double real , double image )
{
_real = real;
_image = image;
}
//拷贝构造函数
Complex::Complex(const Complex& z)
{
_real = z._real;
_image = z._image;
}
//析构函数
Complex::~Complex()
{
//这里析构函数不用做任何操作
}
//运算符的重载
Complex& Complex:: operator=(Complex& z)
{
if (this != &z)
{
_real = z._real;
_image = z._image;
}
return *this;
}
//运算符重载 +
Complex Complex::operator+(Complex& z)
{
Complex tmp(*this);
tmp._real = _real + z._real;
tmp._image = _image + z._image;
return tmp;
}
Complex Complex::operator-(Complex& z)
{
Complex tmp(*this);
tmp._real = _real - z._real;
tmp._image = _image - z._image;
return tmp;
}
Complex& Complex::operator++()
{
_real++;
_image++;
return *this;
}
Complex Complex:: operator++(int)
{
Complex tmp(*this);
_real++;
_image++;
return tmp;
}
bool Complex:: operator==(Complex&z)
{
if (!z._image && !_image)
{
if (_real == _image)
{
return true;
}
}
else if (_image == z._image)
{
if (_real == z._real)
{
return true;
}
}
return false;
}
bool Complex::operator<(Complex&z)
{
if ((!_image) && (!z._image))
{
if (_real < z._real)
{
return true;
}
}
return false;
}
bool Complex::operator<=(Complex&z)
{
if ((!_image) && (!z._image))
{
if (_real < z._real)
{
return true;
}
}
else if (_real == z._real)
{
if (_image == z._image)
return true;
}
return false;
}
bool Complex::operator>(Complex&z)
{
if ((!_image) && (!z._image))
{
if (_real > z._real)
{
return true;
}
}
return false;
}
bool Complex::operator>=(Complex&z)
{
if ((!_image) && (!z._image))
{
if (_real > z._real)
{
return true;
}
}
else if (_real == z._real)
{
if (_image == z._image)
{
return true;
}
}
return false;
}
bool Complex::operator!=(Complex&z)
{
if (_real != z._real)
{
if (_image != z._image)
{
return true;
}
}
return false;
}
//打印函数
void Complex::display()
{
cout << _real << "+" << _image << "i" << endl;
}
测试函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include"complex.h"
void test2()
{
Complex z1(2.0, 3.0);
Complex z2(3.0, 4.0);
Complex z3(z2);
if (z1 == z2)
{
cout << "z1=z2" << endl;
}
if (z2 == z3)
{
cout << "z2=z3" << endl;
}
if (z2 > z3)
{
cout << "z2>z3" << endl;
}
if (z2 > z3)
{
cout << "z2>z3" << endl;
}
if (z2 < z3)
{
cout << "z2<z3" << endl;
}
if (z2 <= z3)
{
cout << "z2<=z3" << endl;
}
if (z2 >= z3)
{
cout << "z2>=z3" << endl;
}
}
//测试用例打印函数
void test1()
{
Complex z1(2.0, 3.0);
Complex z2 = z1;
Complex z3 = z2 + z1;
Complex z4 = z3 - z2;
++z4;
z1++;
z1.display();
z2.display();
z3.display();
z4.display();
}
int main()
{
//test1();
test2();
system("pause");
return 0;
}