complex.h

来自「易学c++源码」· C头文件 代码 · 共 64 行

H
64
字号
#include <iostream.h>
class Complex
{
public:
	Complex(Complex &a);
	Complex(double r=0,double i=0);
	void display();
	friend Complex operator +(Complex a,Complex b);
	friend Complex operator -(Complex a,Complex b);
	friend Complex operator +(Complex a,double r);
	friend Complex operator -(Complex a,double r);
	friend Complex& operator ++(Complex &a);
	friend Complex operator ++(Complex &a,int);
private:
	double real;
	double img;
};
Complex::Complex(Complex &a)
{
	real=a.real;
	img=a.img;
}
Complex::Complex(double r,double i)
{
	real=r;
	img=i;
}
void Complex::display()
{
	cout <<real <<(img>=0?"+":"") <<img <<"i";
}
Complex operator +(Complex a,Complex b)
{
	Complex temp(a.real+b.real,a.img+b.img);
	return temp;
}
Complex operator -(Complex a,Complex b)
{
	Complex temp(a.real-b.real,a.img-b.img);
	return temp;
}
Complex operator +(Complex a,double r)
{
	Complex temp(a.real+r,a.img);
	return temp;
}
Complex operator -(Complex a,double r)
{
	Complex temp(a.real-r,a.img);
	return temp;
}
Complex& operator ++(Complex &a)
{
	a.img++;
	a.real++;
	return a;
}
Complex operator ++(Complex &a,int)
{
	Complex temp(a);
	a.img++;
	a.real++;
	return temp;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?