复数类 complex
来源:互联网 发布:ayawawa打分软件 编辑:程序博客网 时间:2024/06/06 04:26
定义复数类Complex,包括两个double型的数据成员real和image,要求对+、-、、/、+=、-+、=、/=、几个运算符进行重载以实现复数的运算,并进行验证。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std;class Complex{public: //默认的构造函数(含缺省值,并初始化参数) Complex(const double real = 0.0, const double image = 0.0) :_real(real) //初始化 , _image(image) {} //拷贝构造函数 Complex(const Complex& c) { _real = c._real; _image = c._image; } //析构函数 ~Complex() {} //赋值运算符的重载 Complex& operator=(const Complex& c) { this->_real = c._real; //_real = c._real;//隐含的this指针 this->_image = c._image; //_image = c._image; return *this; } //加法运算符的重载 Complex operator+(const Complex& c) { Complex tmp; tmp._real = _real + c._real; tmp._image = _image + c._image; return tmp; } //减法 Complex operator-(const Complex& c) { Complex tmp; tmp._real = _real - c._real; tmp._image = _image - c._image; return tmp; } //乘法 Complex operator*(const Complex& c) { Complex tmp; tmp._real = _real * c._real - _image*c._image; tmp._image = _real * c._image + _image*c._real; return tmp; } //除法 Complex operator/(const Complex& c) { Complex tmp; tmp._real = (_real / c._real + _image*c._image) / (c._real*c._real + c._image*c._image); tmp._image = (_image*c._real - _real*c._image) / (c._real*c._real + c._image*c._image); return tmp; } Complex operator+=(const Complex& c) { _real += c._real; _image += c._image; return *this; } Complex operator-=(const Complex& c) { _real -= c._real; _image -= c._image; return *this;//返回当前对象 } Complex operator*=(const Complex& c) { double real0 = _real; double image0 = _image; _real = (real0 * c._real - image0*c._image); _image = (real0 * c._image + image0*c._real); return *this; } Complex operator/=(const Complex& c) { double real0 = _real; double image0 = _image; _real /= (real0 / c._real + image0*c._image) / (c._real*c._real + c._image*c._image); _image /= (image0*c._real - real0*c._image) / (c._real*c._real + c._image*c._image); return *this; } void Display() { cout << _real << " " << _image << endl; }private: double _real; double _image;};//测试函数 void Funtest1(){ Complex c1(2.0, 3.0); c1.Display(); Complex c2(c1); c2.Display(); Complex c3; c3 = c1+c2; c3.Display();}void Funtest2(){ Complex c1(2.0, 3.0); c1.Display(); Complex c2(c1); c2.Display(); c1 *= c2; c1.Display();}int main(){ cout << "Funtest1:" << endl; Funtest1(); cout << "Funtest2:" << endl; Funtest2(); getchar(); return 0;}
运行结果
阅读全文
0 0
- 设计复数类complex
- 设计复数类Complex
- 设计复数类Complex
- 复数类Complex
- 复数类Complex
- complex复数类
- C++ Complex复数类
- 复数类:class Complex
- 复数类:complex c++
- 复数类 Complex
- Complex复数类
- Complex复数类
- 复数类Complex
- 复数类 complex
- 复数类模板(Complex Class)
- 【C++】c++复数类Complex
- C++中的复数Complex类
- 用java写一个复数类Complex
- VS2017 新版创建空的win32控制台项目
- mysql5.5初识和基本安装配置
- Hibernate--二级缓存
- hello作业
- 程序员的使命是让程序员失业 —— Gemini
- 复数类 complex
- leetcode 之 Merge Two Sorted Lists
- Gradle 环境搭建
- C#学习笔记6-关于||和&&的注意项
- 使用eclipse alt+/注解提示的时候报错卡死解决方法
- 文件夹拷贝,判断,生成当前用户权限的文件夹
- keras Hello world
- ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库
- 单片机详细教程链接(单片机教程网很好_LSH)