关于
我的项目
相关阅读
热度排行
- [转] 宫崎骏用动漫教给我们的人生哲理,每一句都能说到心里! - (日期:[八月 24, 2013] 点击:[53,528])
- Google 网页爬虫报告无法连接站点解决办法 - (日期:[七月 20, 2014] 点击:[38,659])
- 架设Tiny Tiny RSS(TTRSS)阅读器,找回Google Reader! - (日期:[九月 27, 2013] 点击:[27,792])
- SkyDrive、DropBox和Google Drive三大公有云存储服务对比 - (日期:[六月 25, 2013] 点击:[25,633])
- 升级到至强E5440后,与i5 CPU笔记本性能对比 - (日期:[二月 18, 2014] 点击:[23,797])
- 公钥私钥加密解密数字证书数字签名详解 - (日期:[四月 19, 2014] 点击:[22,973])
- 本站建站技术合集 - (日期:[九月 20, 2013] 点击:[22,535])
- 使用OpenerDNS解决无法访问Google的问题 - (日期:[七月 5, 2014] 点击:[21,841])
- WordPress博客添加“返回顶部”按钮 - (日期:[七月 14, 2013] 点击:[21,246])
- Linux文件系统基础之inode和dentry - (日期:[三月 13, 2015] 点击:[20,198])
- 云存储中的HTTP鉴权算法分析 - (日期:[二月 7, 2014] 点击:[18,650])
- 存储基础知识之——磁盘阵列原理及操作实战 - (日期:[二月 9, 2014] 点击:[17,531])
- 精选37条强大的常用linux shell命令组合 - (日期:[九月 4, 2013] 点击:[17,456])
- DNS原理、架构和配置详解 - (日期:[九月 6, 2013] 点击:[16,848])
- Netty和Jetty的Java NIO 网络框架模型分析 - (日期:[七月 13, 2013] 点击:[16,342])
- CoreOS 初识之安装 - (日期:[十一月 16, 2014] 点击:[16,208])
- Windows与Linux文件系统互访的几种方法 - (日期:[八月 21, 2014] 点击:[15,738])
- Dijkstra算法求解最短路径分析 - (日期:[七月 12, 2014] 点击:[14,937])
- NAS解决方案实现多媒体文件共享播放 - (日期:[十二月 21, 2014] 点击:[13,957])
- 简介 - (日期:[九月 1, 2012] 点击:[13,781])
- 如何编程实现 2 + 2 = 5? - (日期:[六月 2, 2014] 点击:[13,275])
- 搭建了一个iNews程序 - (日期:[十月 15, 2013] 点击:[13,249])
- 2014年9月曝出的Bash ShellShock漏洞简析 - (日期:[九月 26, 2014] 点击:[13,163])
- 彻底解决WordPress博客垃圾评论的问题 - (日期:[八月 5, 2013] 点击:[13,134])
- 如何使用1M的内存排序100万个8位数 - (日期:[三月 27, 2014] 点击:[12,570])
- 全部日志列表 - (日期:[十一月 11, 2012] 点击:[12,408])
- 关于回调函数和this指针探讨 - (日期:[八月 24, 2014] 点击:[12,239])
- 开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南 - (日期:[四月 23, 2022] 点击:[11,728])
- 给定一个long型常量,其值为x,给定long型变量a,要求a & x 的取值集合 - (日期:[九月 8, 2012] 点击:[11,722])
- WordPress建站必备实用插件 - (日期:[八月 7, 2014] 点击:[11,381])
分类目录
文章归档
- 2025年一月 (1)
- 2024年十二月 (1)
- 2024年四月 (1)
- 2024年二月 (1)
- 2023年九月 (1)
- 2023年一月 (1)
- 2022年十月 (1)
- 2022年八月 (2)
- 2022年四月 (1)
- 2022年三月 (1)
- 2021年十二月 (2)
- 2021年十月 (2)
- 2021年九月 (1)
- 2021年八月 (1)
- 2021年五月 (1)
- 2021年三月 (2)
- 2021年一月 (2)
- 2020年十二月 (5)
- 2020年十一月 (2)
- 2020年十月 (2)
- 2020年九月 (1)
- 2020年八月 (5)
- 2020年七月 (2)
- 2019年九月 (1)
- 2018年八月 (1)
- 2018年七月 (1)
- 2018年六月 (1)
- 2018年五月 (1)
- 2018年三月 (1)
- 2018年二月 (1)
- 2018年一月 (2)
- 2017年十二月 (3)
- 2017年十月 (4)
- 2017年九月 (1)
- 2017年七月 (1)
- 2017年六月 (1)
- 2016年十二月 (1)
- 2016年十月 (1)
- 2016年九月 (1)
- 2016年七月 (2)
- 2016年六月 (1)
- 2016年二月 (3)
- 2015年十二月 (3)
- 2015年十一月 (2)
- 2015年十月 (1)
- 2015年八月 (2)
- 2015年七月 (4)
- 2015年六月 (1)
- 2015年三月 (2)
- 2015年二月 (1)
- 2015年一月 (4)
- 2014年十二月 (2)
- 2014年十一月 (2)
- 2014年十月 (5)
- 2014年九月 (8)
- 2014年八月 (11)
- 2014年七月 (17)
- 2014年六月 (7)
- 2014年五月 (15)
- 2014年四月 (16)
- 2014年三月 (14)
- 2014年二月 (5)
- 2013年十二月 (5)
- 2013年十一月 (3)
- 2013年十月 (13)
- 2013年九月 (13)
- 2013年八月 (13)
- 2013年七月 (9)
- 2013年六月 (8)
- 2013年五月 (1)
- 2013年三月 (3)
- 2013年一月 (1)
- 2012年十一月 (1)
- 2012年九月 (12)
- 2012年八月 (3)
- 2011年二月 (1)
- 2009年三月 (1)
- 2009年二月 (1)
- 2008年十一月 (1)
- 2008年六月 (1)
- 2008年四月 (1)
- 2008年三月 (1)
写过的真正意义上的第一个C++程序
博客的发布记录为:
记得当时刚过完年回到学校,看了一个寒假的书,迫不及待的想实践一下,于是参照《Exceptional C++》上面反复拿出来举例的Complex类,自己重新写了一个,虽然不是独创的。首先规划好了想要实现哪些接口,然后一条一条地到《C++ Primer》中去找相应的注意事项和一般约定。整个代码几乎是自己独立写出的。后来在面试过程中,还重新将这份代码又写了一遍提交给考官,呵呵,当时给他的感觉估计还好,但是他哪知道我只会这几行代码 :-)
感觉现在写代码的风格几乎都是那个时候定型的。喜欢比较整洁的风格,看到乱七八糟的代码总有重构的冲动。
#include <iostream>
using namespace std;
class Complex
{
public:
Complex()
{
}//无形参的构造函数
Complex(double _r, double _i = 0) : _real(_r), _img(_i)
{
}//constructor fun.
Complex(const Complex&);//copy constructor fun.
Complex& operator=(const Complex&);//赋值操作符
Complex& operator+=(const Complex&);
Complex& operator-=(const Complex&);
Complex& operator*=(const Complex&);
Complex& operator/=(const Complex&);//复合赋值操作符
Complex& operator++(); //前缀式的自增操作符
Complex operator++(int);//后缀式
Complex& operator–();
Complex operator–(int);
ostream& ops(ostream&) const;
istream& ips(istream&);//输入操作需要更改类的数据,其辅助函数就不能定义为const的
~Complex()
{
}
private:
double _real, _img;
};
//定义赋值操作符
Complex& Complex::operator=(const Complex& cr)
{
this->_real = cr._real;
this->_img = cr._img;
return *this;
}
//定义输入输出操作
ostream& Complex::ops(ostream& os) const
{
if (_img > 0)
{
return os << _real << " + " << _img << "i";
}
else if (_img == 0)
{
return os << _real;
}
else
{
return os << _real << " + " << "(" << _img << ")" << "i";
}
}
ostream& operator<<(ostream& os, const Complex& cr)
{
return cr.ops(os);
//借助于类的成员函数来实现操作符函数访问私有数据。
//由于这里是常引用,辅助的成员函数也应定义为const成员函数。
}
istream& Complex::ips(istream& os)
{
return os >> _real >> _img;
}
istream& operator>>(istream& os, Complex& cr)
//注意这里需要改变形参cr,因此不能将其定为const
{
return cr.ips(os);
//只有非const的对象才能调用非const的成员函数。
}
//定义复制构造函数
Complex::Complex(const Complex& rhs)
{
_real = rhs._real;
_img = rhs._img;
//注意这里直接访问类中的数据成员,而不是通过对象来调用
}
//定义相应的复合赋值运算
Complex& Complex::operator+=(const Complex& cl)
{
_real += cl._real;
_img += cl._img;
return *this;
}
Complex& Complex::operator-=(const Complex& cl)
{
_real -= cl._real;
_img -= cl._img;
return *this;
}
Complex& Complex::operator*=(const Complex& cl)
{
double _t = _real;
_real = (_real * cl._real – _img * cl._img);
_img = (_t * cl._img + _img * cl._real);
return *this;
}
Complex& Complex::operator/=(const Complex& cr)
{
double _r = _real, _i = _img, _m = cr._real* cr._real + cr._img* cr._img;
if (_m == 0)
cout << "Error" << endl;
else
{
_real = (_r * cr._real + _i * cr._img) / _m;
_img = (-_r * cr._img + _i * cr._real) / _m;
}
return *this;
}
//利用复合赋值操作来实现四则远算
Complex operator+(const Complex& cl, const Complex& cr)
{
return Complex(cl) += cr;
}
Complex operator-(const Complex& cl, const Complex& cr)
{
return Complex(cl) -= cr;
}
Complex operator*(const Complex& cl, const Complex& cr)
{
return Complex(cl) *= cr;
}
Complex operator/(const Complex& cl, const Complex& cr)
{
return Complex(cl) /= cr;
}
//先定义前缀式的,后缀式根据此操作实现
Complex& Complex::operator++()
{
++_real;
return *this;
}
Complex Complex::operator++(int)
{
Complex _ct(*this);
++(*this);
return _ct;
}
Complex& Complex::operator--()
{
--_real;
return *this;
}
Complex Complex::operator--(int)
{
Complex _ct(*this);
--(*this);
return _ct;
}
int main()
{
Complex c1(2, 1), c2(4, 3);
Complex c0 = 0;
Complex c4(c1);
Complex c5(2.4, 3.5);
c5 *= c2;
Complex c6;
Complex c3 = c1 + c2;
cout << "c1: " << c1 << endl;
cout << "c2: " << c2 << endl;
cout << "c3: " << c3 << endl;
cout << "c4: " << c4 << endl;
cout << "c5: " << c5 << endl;
cout << "c5++: " << c5++ << endl;
cout << "c5: " << c5 << endl;
cout << "++c5: " << ++c5 << endl;
cout << "--c5: " << --c5 << endl;
cout << "c5: " << c5 << endl;
cout << "c2/c1: " << c2 / c1 << endl;
cout << "c1*c2: " << c1 * c2 << endl;
cout << "c2-c1: " << c2 - c1 << endl;
cout << "c5+c1: " << c5 + c1 << endl;
cout << "Please input c6: ";
cin >> c6;
cout << "c6: " << c6 << endl;
return 0;
}
2 条评论
赞一个。
提个小问题:
//定义输入输出操作
ostream& Complex::ops(ostream& os) const
这里的else if 中double数直接和0做相等比较了,感觉应该处理下。
谢谢指出问题!浮点数精度问题,是需要修改一下。