复数类的完整实现(C++版)
2016-08-04 07:45
766 查看
实现一个完整的复数类,包括复数的 + ,-,*,/,+=,-=,*=,/=,前置++,后置++,前置--,后置--, ==(条件判断),!=,>,<=,=(赋值操作符)
1.复数的+,-,*,/,+=,-=,*=,/=
在这里复数的加减乘除就不做叙述了(中学知识),而加等,减等,乘等,除等与复数的加减乘除类似,不同的是加等,减等,乘等,除等是将相加,相减,相乘,相除的结果直接+,-,*,/到原本的数据处,在这里也不加叙述了,结果如下:2.复数的前置++,后置++,前置--,后置--
我们知道在C++中是允许重载的,那仫如何区分前置++,和后置++呢?只需要在构成重载的基础上多加一个参数就可以了,经过测试只能传一个整形(int)参数; 由前置++(先加再使用)和后置++(先使用再加)的特性可知,要模拟实现前置++是很容易的只需要实部和虚部直接加并返回相加之后的复数就可以了,而模拟实现后置++就不太容易了,其实只需要我们先利用拷贝构造函数构造出新的复数,再相加实部,虚部,最后返回的是拷贝构造的复数Complex.h
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __COMPLEX_H__ #define __COMPLEX_H__ #include<iostream> using namespace std; class Complex { public: Complex(double real,double image); void Print(); bool operator==(const Complex &c); Complex& operator++(); //前置++ Complex operator++(int); //后置++ Complex& operator--(); //前置-- Complex operator--(int); //后置-- bool operator>(const Complex &c); Complex& operator=(const Complex &c); Complex operator+(const Complex &c); Complex& operator+=(const Complex &c); Complex operator-(const Complex &c); Complex& operator-=(const Complex &c); Complex operator*(const Complex &c); Complex& operator*=(const Complex &c); Complex operator/(const Complex &c); Complex& operator/=(const Complex &c); private: double _real; double _image; }; #endif //__COMPLEX_H__
Complex.cpp
#define _CRT_SECURE_NO_WARNINGS 1 #include"Complex.h" Complex::Complex(double real=0.0,double image=0.0) //构造函数 :_real(real) ,_image(image) {} void Complex::Print() { if(_image == 0.0) //虚部为0 { cout<<_real<<endl; } else { cout<<_real<<"+"<<_image<<"*i"<<endl; } } Complex Complex::operator+(const Complex &c) { Complex tmp; tmp._real=_real+c._real; tmp._image=_image+c._image; return tmp; } Complex Complex::operator-(const Complex &c) { Complex tmp; tmp._real=_real-c._real; tmp._image=_image-c._image; return tmp; } Complex 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 Complex::operator/(const Complex &c) { Complex tmp; double t=c._real*c._real+c._image*c._image; tmp._real=(_real*c._real-_image*(-c._image))/t; tmp._image=(_real*(-c._image)+_image*c._real)/t; return tmp; } Complex& Complex::operator+=(const Complex &c) { _real+=c._real; _image+=c._image; return *this; } Complex& Complex::operator-=(const Complex &c) { _real-=c._real; _image-=c._image; return *this; } Complex& Complex::operator*=(const Complex &c) { Complex tmp(*this); //拷贝构造函数 _real=tmp._real*c._real-_image*c._image; _image=tmp._real*c._image+tmp._image*c._real; return *this; } Complex& Complex::operator/=(const Complex &c) { Complex tmp(*this); double t=c._real*c._real+c._image*c._image; _real=(tmp._real*c._real-tmp._image*(-c._image))/t; _image=(tmp._real*(-c._image)+tmp._image*c._real)/t; return *this; } bool Complex::operator==(const Complex &c) { return (_real == c._real)&& (_image == c._image); } Complex& Complex::operator++() //前置++ { _real++; _image++; return *this; } Complex Complex::operator++(int) //后置++ { Complex tmp(*this); //拷贝构造函数暂存this所指向的值 _real++; _image++; return tmp; } Complex& Complex::operator--() //前置-- { _real--; _image--; return *this; } Complex Complex::operator--(int) //后置-- { Complex tmp(*this); _real--; _image--; return tmp; } bool Complex::operator>(const Complex &c) { return (_real > c._real)&& (_image > c._image); } Complex& Complex::operator=(const Complex &c) { if(this != &c) { _real=c._real; _image=c._image; } return *this; }
测试代码:test.cpp
#define _CRT_SECURE_NO_WARNINGS 1 #include"Complex.h" void test1() { Complex c1(1.0,2.0); Complex c2(3.0,6.0); Complex c3=c1+c2; Complex c4=c1-c2; Complex c5=c1*c2; Complex c6=c1/c2; cout<<"c1:"; c1.Print(); cout<<"c2:"; c2.Print(); cout<<"c1+c2:"; c3.Print(); cout<<"c1-c2:"; c4.Print(); cout<<"c1*c2:"; c5.Print(); cout<<"c1/c2:"; c6.Print(); } void test2() { Complex c1(1.0,2.0); Complex c2(3.0,6.0); cout<<"c1:"; c1.Print(); cout<<"c2:"; c2.Print(); //Complex c3=c1+=c2; //cout<<"c1+=c2:"; //c1.Print(); //Complex c3=c1-=c2; //cout<<"c1-=c2:"; //c3.Print(); //c1.Print(); //Complex c3=c1*=c2; //cout<<"c1*=c2:"; //c3.Print(); //c1.Print(); Complex c3=c1/=c2; cout<<"c1/=c2:"; c3.Print(); } void test3() { Complex c1(1.0,2.0); Complex c2(3.0,6.0); Complex c3(c1); //c1.Print(); //1+2*i //Complex c4=++c1; //c4.Print(); //2+3*i //c1.Print(); //2+3*i //c1.Print(); //1+2*i //Complex c5=c1++; //c5.Print(); //1+2*i //c1.Print(); //2+3*i //c2.Print(); //3+6*i //Complex c4=--c2; //c4.Print(); //2+5*i //c2.Print(); //2+5*i c2.Print(); //3+6*i Complex c4=c2--; c4.Print(); //3+6*i c2.Print(); //2+5*i } void test4() { Complex c1(1.0,2.0); Complex c2(3.0,6.0); Complex c3(c1); //cout<<(c1 == c3)<<endl; //1 == //cout<<(c1 == c2)<<endl; //0 == //cout<<(!(c1 == c2))<<endl; //1 != //cout<<(c2 > c1)<<endl; //1 > //cout<<(!(c2 > c1))<<endl; //0 <= c3.Print(); //1+2*i c3=c2; c3.Print(); //3+6*i } int main() { //test1(); //test2(); test3(); //test4(); system("pause"); return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析
- C与C++之间相互调用实例方法讲解