C++中较为常见的类—复数类,主要实现复数比较大小、加、减、乘、除等基本运算,通过C++类和对象进行实现。C++的类和对象是笔试、面试中容易问到的,因此,对其掌握一定要通透。下面是复数类功能的具体实现。
#define _CRT_SECURE_NO_WARNINGS 1
//复数类
#include <iostream>
#include <stdlib.h>
using namespace std;
class complex
{
public:
complex(double real = 0.0, double p_w_picpath = 0.0) //构造函数
{
//cout << "complex(double real, double p_w_picpath)" << endl;
//用于查看构造函数调用多少次
_real = real;
_p_w_picpath = p_w_picpath;
}
complex(const complex & c) //拷贝构造函数,注意引用符,若为值传递则可能会引发无穷递归
{
//cout << "complex(const complex & c)" << endl;
_real = c._real;
_p_w_picpath = c._p_w_picpath;
}
~complex() //析构函数
{
//cout << "~complex" << endl;
}
complex & operator=(const complex& c) //赋值操作符的重载
{
if (this != &c)
{
this->_real = c._real;
this->_p_w_picpath = c._p_w_picpath;
}
return *this;
}
bool operator==(const complex & c) //判相等
{
return _real == c._real && _p_w_picpath == c._p_w_picpath;
}
bool operator>(const complex & c) //大于
{
if ((_real > c._real) || (_real == c._real && _p_w_picpath > c._p_w_picpath))
{
return true;
}
else
return false;
}
bool operator<(const complex & c) //小于
{
if ((_real < c._real) || (_real == c._real && _p_w_picpath < c._p_w_picpath))
{
return true;
}
else
return false;
}
complex operator+(const complex & c) //复数加法
{
complex tmp;
tmp._real = _real + c._real;
tmp._p_w_picpath = _p_w_picpath + c._p_w_picpath;
return tmp;
}
complex & operator+=(const complex & c) //+=
{
_real += c._real;
_p_w_picpath += c._p_w_picpath;
return *this;
}
complex operator-(const complex & c) //复数减法
{
complex tmp;
tmp._real = _real - c._real;
tmp._p_w_picpath = _p_w_picpath - c._p_w_picpath;
return tmp;
}
complex & operator-=(const complex & c) //-=
{
_real -= c._real;
_p_w_picpath -= c._p_w_picpath;
return *this;
}
complex operator*(const complex & c) //复数乘法
{
complex tmp;
tmp._real = _real*c._real - _p_w_picpath*c._p_w_picpath;
tmp._p_w_picpath = _real*c._p_w_picpath + _p_w_picpath*c._real;
return tmp;
}
complex & operator*=(const complex & c) //复数*=
{
*this = *this * c;
return *this;
}
complex operator/(const complex & c) //复数除法
{
complex tmp;
// x = (ac + bd) / (cc + dd), y = (bc - ad) / (cc + dd);
tmp._real = (_real * c._real + _p_w_picpath * c._p_w_picpath)
/ (c._real*c._real + c._p_w_picpath*c._p_w_picpath);
tmp._p_w_picpath = (_p_w_picpath * c._real - _real * c._p_w_picpath)
/ (c._real*c._real + c._p_w_picpath*c._p_w_picpath);
return tmp;
}
complex operator/=(const complex & c) //复数/=
{
*this = *this / c;
return *this;
}
void Display() //打印
{
cout << _real << "-" << _p_w_picpath << endl;
}
private:
double _real;
double _p_w_picpath;
};
//测试用例
void Test()
{
complex p1;
p1.Display();
complex p2(1.0, 2.0);
p2.Display();
complex p3(p1);
p3.Display();
complex p4 = p1;
p4.Display();
}
void Text1()
{
complex p1(2.0, 3.0);
complex p2(4.0, 5.0);
p1.Display();
p2.Display();
bool ret;
ret = p1 == p2;
cout<< ret << endl;
ret = p1 > p2;
cout<< ret << endl;
ret = p1 < p2;
cout<< ret << endl;
}
void Text2()
{
//加减乘除
complex p1(1.0, 2.0);
complex p2(2.0, 3.0);
complex tmp;
tmp = p1 + p2;
tmp.Display();
tmp = p1 - p2;
tmp.Display();
tmp = p1 * p2;
tmp.Display();
tmp = p1 / p2;
tmp.Display();
}
int main()
{
//Test();
//Text1();
Text2();
system("pause");
return 0;
}
转载于:https://blog.51cto.com/10740590/1739372