complex.h

来自「简单的复数类」· C头文件 代码 · 共 76 行

H
76
字号
#include <iostream>
using namespace std;
#ifndef COMPLEX_H
#define COMPLEX_H

class Complex
{
private:
	float a,b;
public:
	Complex(float x,float y);//构造函数

	float Get_a();//获得实部
	float Get_b();//获得虚部

	void Put_a(float x);//修改实部
	void Put_b(float y);//修改虚部

	Complex& operator+(Complex& k);
	Complex& operator-(Complex& k);
	Complex& operator*(Complex& k);//各种运算

	friend ostream& operator<<(ostream& out,Complex& p);//重载输出

};

Complex::Complex(float x=0,float y=0):a(x),b(y)
{}

float Complex::Get_a(){
	return a;
}

float Complex::Get_b(){
	return b;
}

void Complex::Put_a(float x){
	a=x;
}

void Complex::Put_b(float y){
	b=y;
}

Complex& Complex::operator *(Complex & k)
{

	a=a*k.a+-b*k.b;
	b=b*k.a+a*k.b;
	return *this;
}

Complex& Complex::operator +(Complex & k)
{
	a=a+k.a;
	b=b+k.b;
	return *this;
}


Complex& Complex::operator -(Complex& k)
{
	a=a-k.a;
	b=b-k.b;
	return *this;
}

ostream& operator<<(ostream& out,Complex& p)
{
	out<<p.a<<'+'<<p.b<<'i'<<endl;
	return out;
}

#endif

⌨️ 快捷键说明

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