C++ Complex复数类
来源:互联网 发布:云上贵州大数据比赛 编辑:程序博客网 时间:2024/05/17 08:43
#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>using namespace std;class Complex{public:Complex(double real = 0, double image = 0):_real(real),_image(image){}Complex(const Complex& d):_real(d._real),_image(d._image){}void Display(){cout<<"real:"<<_real<<"image:"<<_image<<endl;}Complex operator +(const Complex& d)//复数相加{Complex tmp = *this;tmp._real += d._real;tmp._image += d._image;return tmp;}Complex& operator +=(const Complex& d)//复数的加等,返回值用引用{_real += d._real;_image += d._image;return *this;}Complex& operator ++()//复数的前置++,返回值用引用{_real++;return *this;}Complex operator ++(int)//复数的后置++{Complex tmp = *this;_real++;return tmp;}//减法与加法相似//Complex operator -(const Complex& d)//Complex& operator -=(const Complex& d)//Complex& operator --()//Complex operator --(int)//复数不能比较大小(复数有方向)所以这里就不实现了^-^//operator >//operator >=//operator <//operator <=bool operator ==(const Complex& d)//复数的=={return (_real == d._real)&& (_image == d._image);}bool operator !=(const Complex& d)//复数的!={return (_real != d._real)|| (_image != d._image);}Complex operator *(const Complex& d)//复数相乘{Complex tmp;tmp._real = (_real*d._real) - (_image*d._image);tmp._image = (_real*d._image) + (_image*d._real);return tmp;}Complex operator /(const Complex& d)//复数相除{Complex tmp;tmp._real= ((_real*d._real) - (_image*d._image)) /((d._real)*(d._real) + (d._image)*(d._image));tmp._image = (_real*d._image) + (_image*d._real) /((d._real)*(d._real) + (d._image)*(d._image));return tmp;}private:double _real;double _image;};void test1()//测试加减相关函数{Complex d1(1, 1);Complex d2(2, 2);Complex d3;d3 = d1 + d2;d3.Display();d3 = d1++;d3.Display();d3 = d1+=(d2);d3.Display();d3 = ++d1;d3.Display();}void test2()//测试乘除/==/!={int a = -1;Complex d1(1, 1);Complex d2(2, 2);Complex d3;d3 = d1*(d2);d3.Display();d3 = d1/(d2);d3.Display();a = d1==(d2);cout<<"operator ==():"<<a<<endl;a = d1!=(d2);cout<<"operator !=():"<<a<<endl;}int main(){test1();test2();return 0;}
阅读全文
0 0
- 【C++】c++复数类Complex
- 设计复数类complex
- 设计复数类Complex
- 设计复数类Complex
- 复数类Complex
- 复数类Complex
- complex复数类
- C++ Complex复数类
- 复数类:class Complex
- 复数类:complex c++
- 复数类 Complex
- Complex复数类
- Complex复数类
- 复数类Complex
- 复数类 complex
- 【C++】用拷贝函数实现一个复数类(complex)
- 复数类模板(Complex Class)
- C++中的复数Complex类
- JavaScript中的存储对象
- xmlHttp之post提交
- Failed to load the JNI shared library 解决方法
- 字符串转JSON对象
- Mixture of conda2 and conda3 problem and A guidance of environment set-up for windows users
- C++ Complex复数类
- Canvas用法
- 【网络】网络基础知识总结
- docker命令,一些常用的docker命令
- Python2.7和Python3.6的和平相处,pip冲突的解决办法
- Android设备兼容
- HTML+CSS编写静态网站-22 组织css文件并添加注释
- maven
- 线段树总结(二) 进阶篇