复数类的实现
2016-07-04 16:31
393 查看
#include <iostream> using namespace std; //5+2i class complex { public: complex(int real = 0,int image = 0) :_real(real) ,_image(image) { } complex operator+(const complex& c) { complex tmp; tmp._real = this->_real + c._real ; tmp._image = this->_image + c._image ; return tmp; } complex& operator++() { this->_real = this->_real +this->_real ; this->_image += this->_image ; return *this; } complex& operator+=(const complex& c) { this->_real = this->_real +c._real ; this->_image += c._image ; return *this; } complex operator*(const complex& c) { complex tmp; tmp._real = this->_real * c._real - this->_image * c._image; tmp._image = this->_image * c._real + this->_real * c._image; return tmp; } complex operator-(const complex& c) { complex tmp; tmp._real = this->_real - c._real ; tmp._image = this->_image - c._image ; return tmp; } complex operator/(const complex& c) { complex tmp; tmp._real = (this->_real * c._real + this->_image * c._image)/ (c._real * c._real +c._image * c._image); tmp._image = (this->_image * c._real - this->_real * c._image)/ (c._real * c._real +c._image * c._image); return tmp; } void printf() { cout<<"复数:"<<this->_real <<"+"<<this->_image <<"i"<<endl; } ~complex() {} private: int _real; int _image; }; int main() { complex c1(78,2); complex c2(2,3); c1.printf (); c2.printf (); complex tmp; cout<<"+:"<<endl; tmp = c1+ c2; tmp.printf (); cout<<"-:"<<endl; tmp = c1 - c2; tmp.printf (); cout<<"*:"<<endl; tmp = c1 * c2; tmp.printf (); cout<<"/:"<<endl; tmp = c1 / c2; tmp.printf (); cout<<"++:"<<endl; c1++; c1.printf (); cout<<"+=:"<<endl; c1 += c2; c1.printf (); return 0; }
相关文章推荐
- js获取所选中复选框的值s
- 为什么在tomcat6.0下显示正常的jsp页面在tomcat 7.0里中文全是乱码?怎么样才能解决
- getaccesstoken方法
- (38) 日志运用
- 16.5节练习
- Android使用ViewPager实现左右循环滑动及轮播效果
- C# 获取一个13位的时间戳
- $.ajax()方法详解
- 装饰者设计模式----IO流
- 【IT我们来了】每一个程序员要遵守的一些优秀编程风格~
- Ubuntu下通过ppa安装jdk
- Android出现“Conversion to Dalvik format failed with error 1”的几种情况及解决办法
- Linux Redis副本集安装(redis-3.0.7)
- 互联网版本(支持手机APP)云天售后服务软件上线
- js作用域和作用域链
- postgresql数据库的备份和还原
- mysql 主键自增
- 文章标题
- Android 蓝牙4.0连接几次后出现连接不了,搜索不到服务的情况
- 方法访问权限控制(public,protected,package,private)