技术标签: c++学习笔记 剑指offer面试题 复数 运算符重载 c++ 函数重载
<span style="font-size:24px;"><span style="font-size:24px;">#include <cstdlib>
using namespace std;
class Complex
{
public:
Complex(double r,double i);
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);
bool operator==(const Complex &c);
Complex operator++();
Complex operator++(int);
Complex operator--();
Complex operator--(int);
void print(Complex &c);
//~Complex();//析构函数
private:
double _real;
double _image;
};
Complex::Complex(double r=0.0,double i=0.0)
{
_real=r;//复数实部
_image=i;//复数虚部
}
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)//两复数相减 c的值在函数内部不需要改变,所以采用常引用
{
Complex tmp;
tmp._real =_real-c._real ;
tmp._image =_image-c._image ;
return tmp;
}
Complex Complex::operator*(const Complex &c)//(a+bi)(c+di)=(ac-bd)+(bc+ad)i.
{
Complex tmp;
tmp._real=_real*c._real-_image*c._image;
tmp._image= _image*c._real +_real*c._image;
return tmp;
}
Complex Complex::operator/(const Complex &c)//(a+bi)(c-di)/(c+di)(c-di)
(ac+bd)/(c^2+d^2)+(bc-ad)/(c^2+d^2)i
{
Complex tmp;
double deno=c._real*c._real+c._image*c._image;//有理化后的分母denominator
tmp._real=deno*((_real*c._real)+(_image*c._image));
tmp._image=deno*((_image*c._real )-(_real*c._image));
return tmp;
}
Complex& Complex ::operator+=(const Complex c)//加上自身
{
this->_real += c._real ;
this->_image += c._image ;
return *this;
}
Complex& Complex::operator-=(const Complex c)//加上自身
{
this->_real -= c._real ;
this->_image -= c._image ;
return *this;
}
bool Complex::operator==(const Complex &c)//判断两个复数是否相等
{
return(_real==c._real)&&(_image==c._image);
}
Complex Complex::operator++()//前置++
{
this->_real+=1;
this->_image+=1;
return *this;
}
Complex Complex::operator++(int)//后置++
{
Complex tmp(*this);//拷贝构造用来保存原来的值
this->_real++;
this->_image++;
return tmp;//返回临时变量所以不能以引用作为返回值
}
Complex Complex::operator--()//前置--
{
this->_real-=1;
this->_image-=1;
return *this;
}
Complex Complex::operator--(int)//后置--
{
Complex tmp(*this);//拷贝构造用来保存原来的值
this->_real--;
this->_image--;
return tmp;//返回临时变量所以不能以引用作为返回值
}
void Complex::print(Complex &c)
{
cout<<c._real<<"+ "<<c._image<<"i"<<endl;
}
int main()
{
Complex *c=NULL;
Complex c1(6.0,5.0);
Complex c2(3.0,4.0);
Complex ret;
cout<<(c1==c2)<<endl;//输出c1,c2是否相等
c->print(c1);//输出c1+c2
c->print(c2);
cout<<"c1+c2=";
ret=c1+c2;
c->print(ret);
c->print (c1);//输出c1自加
ret=++c1;
c->print(ret);//
system("pause");
return 0;</span>
}</span></span>
部分运行结果:
安装:http://letgoof.me/2011/install-ubuntu-11-04-from-harddisk-under-windows7/Grub:title Install Ubuntu 11.04 root (hd0,0) kernel (hd0,0)/vmlinuz boot=casper iso-scan/filename=/ubuntu-11.04-deskto...
深刻理解这些数据结构的思想,数据结构与算法是紧密联系在一起的一、队列新学期开始了,小哈是小哼的新同,小哼向小哈询问QQ号,小哈当然不会直接告诉小哼。所以小哈给了小哼一串加密过的数字,同时小哈也告诉了小哼解密规则。规则是这样的:首先将第1个数删除,紧接着将第2个数放到这串数的末尾,再将第3个数删除并将第4个数再放到这串数的末尾,再将第5个数删除……直到剩下最后一个数,将最后一个数也...
getquerystring 中文乱码、HTML获取链接中的参数--------解决中文乱码var points = position.split(',');var url = '"/mapView/patrol?name=event&lon=' + points[0] + '&lat=' + points[1] + '&id=' + value...
补充一下安装吧~因为发现很多人都很头疼那个这个问题,尤其是这种别人安装到半路,跑路的!!!目前我上手的机器以及有了这些个零件,我就只能看看配置了,这个过程很痛苦,因为你不知道,前人对他做了什么,你就像改bug一样,慢慢来~~~:PART1:查看已经安装的相关版本Ubuntu14.04python:[email protected]:~$ python --version
基于Matlab神经网络语音情感识别系统,五种基本情感:'生气','高兴','中性','悲伤','害怕'。
一些鲜为人知却非常实用的数据结构 作为程序猿(媛),你必须熟知一些常见的数据结构,比如栈、队列、字符串、链表、二叉树、哈希,但是除了这些常见的数据结构以外,还有没有其他不是很有名,但却非常实用的数据结构呢,有人在 stackoverflow 上问了这样一个问题,得到了很多热心观众的回答,我们今天就来看看那些鲜为人知却非常实用的数据结构吧。首先,维基百科上的一个页面列举了常见...
序列式容器:vector list deque stack queue heap priority_queue slist关联式容器:set map multiset multimap hashtable hash_set hash_map hash_multiset hash_multimap
Product---->Edit Scheme------>选择Info----->将Info里面的“Build Configuration”选择“debug”,将“Debugger”选择“LLDB”,解决xcode断点调试无效的问题。
如果当初看单调队列先从这题开始做的话,应该会快点理解吧。题意就是求区间最值,这是单调队列最简单的应用。Sliding Window题意:给你n个数和区间长度k,求区间遍历到第i个数时的最小和最大值,所以是n-k+1个值。由于n的范围是10^6,普通嵌套for肯定超时,这里就用到了单调队列。单调队列只需要遍历一遍,虽然中间有出队入队的操作具体的看代码和注释吧:
安装虚拟机 vm_wavare, 后提示安装系统 提示intel VT-x 未开启(intel 的vritual teanolig 虚拟技术 支持多操作系统 linux / windows等),解决方法 打开 开关 设置选为 enablle解决links:...
java.util.Optional是Java 8新增的类,可以帮我们把判空的代码写得更优雅,一起来了解一下吧。
尊重原创作者,转载请注明出处:http://blog.csdn.net/gemmem/article/details/7932655如果我们在 activity中start了一个HandlerThread,那么这个线程会启动一个looper消息循环,当activity退出了,这个HandlerThread线程并没有终止,还是在那里做looper死循环,这当然不是我们愿意看到的。我们