CPP0048复数类四则运算及插入/提取操作

Posted qian-heng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CPP0048复数类四则运算及插入/提取操作相关的知识,希望对你有一定的参考价值。

为复数类Complex重载实现+,-,*,/,<<,>>等运算符,main(void)函数完成对其的测试。@

Complex类结构说明:

 
Complex类的数据成员包括:
①私有数据成员:实部real(double型),虚部imag(double型)。
Complex类成员函数包括:
①有参构造函数Complex(double, double),其中参数默认值为0。
②重载运算符+以实现两个Complex的加法运算。
③重载运算符-以实现两个Complex的减法运算。
④重载运算符*以实现两个Complex的乘法运算。
⑤重载运算符/以实现两个Complex的除法运算。
⑥重载运算符<<以实现Complex对象的格式输出,输出格式要求:
<实部>+<虚部>i
例如:10.0,10.0+4.7i,10.0-4.7i,-4.7i,0等。
⑦重载运算符>>以实现Complex对象的格式输入,输入格式要求:
<实部>+<虚部>i
例如:10.0,10.0+4.7i,10.0-4.7i,-4.7i,0等。
 

###裁判测试程序样例:

 
#include <iostream>
using namespace std;

/*请在这里填写答案*/

int main()      //主函数

    Complex c1(1, 1), c2(-1, -1), c3;    //定义复数类的对象
    cin>>c1;
    cout <<c1<<endl;
    cout <<c2<<endl;
    c3 = c1 - c2;
    cout << c3<<endl;
    c3 = c1 + c2;
    cout << c3<<endl;
    c3=c1 * c2;
    cout << c3<<endl;
    c3=c1 / c2;
    cout << c3<<endl;
    return 0;

 

###输入样例:

10.0-4.7i
 

###输出样例:

10-4.7i
-1-1i
11-3.7i
9-5.7i
-14.7-5.3i
-2.65+7.35i
 代码实现:

#include <iostream>

using namespace std;

/*请在这里填写答案*/

class Complex

private:

double real;

double imag;

public:

Complex(double real1=0,double imag1=0):real(real1),imag(imag1)

Complex operator+(Complex &c2);

Complex operator-(Complex &c2);

Complex operator*(Complex &c2);

Complex operator/(Complex &c2);

friend ostream& operator<<(ostream &out,Complex &c);

friend istream& operator>>(istream &in,Complex &c);

;

Complex Complex::operator+(Complex &c2)

return Complex(real+c2.real,imag+c2.imag);

Complex Complex::operator-(Complex &c2)

return Complex(real-c2.real,imag-c2.imag);

Complex Complex::operator*(Complex &c2)

return Complex(real*c2.real-imag*c2.imag,imag*c2.real+real*c2.imag);

Complex Complex::operator/(Complex &c2)

return Complex((real*c2.real+imag*c2.imag)/(c2.imag*c2.imag+c2.real*c2.real),(imag*c2.real-real*c2.imag)/(c2.imag*c2.imag+c2.real*c2.real));

ostream& operator<<(ostream& out, Complex& c)

if(c.imag>0)

out << c.real << "+" << c.imag << "i";

else

out << c.real<< c.imag << "i";

return out;

istream& operator>>(istream& in, Complex& c)

in >> c.real >> c.imag;

return in;

int main() //主函数

Complex c1(1, 1), c2(-1, -1), c3; //定义复数类的对象

cin>>c1;

cout <<c1<<endl;

cout <<c2<<endl;

c3 = c1 - c2;

cout << c3<<endl;

c3 = c1 + c2;

cout << c3<<endl;

c3=c1 * c2;

cout << c3<<endl;

c3=c1 / c2;

cout << c3<<endl;

return 0;

 

C++运算符的重载

目录

一.重载运算符加减乘除“+”“-“*”“/”

函数类型 operator 运算符名称(形参表)
	对运算符的重载处理

例题1:复数类进行加减乘除

复数运算相关公式

1.加法运算
(a+bi)+(c+di)=(a+c)+(b+d)i

2.减法运算
(a+bi)-(c+di)=(a-c)+(b-d)i

3.乘法运算
(a+bi)(c+di)=(ac-bd)+(bc+ad)i

4.除法运算
(a+bi)/(c+di)=(ac + bd)/(c^2 + d ^2) +((bc - ad)/(c ^2 + d ^2)) i

#include<iostream>
using namespace std;
class Complex 
public:
	Complex(double a, double b) :real(a), imag(b) 
	void display();
	double get_real();
	double get_imag();
	Complex operator+(Complex&);
	Complex operator-(Complex&);
	Complex operator*(Complex&);
	Complex operator/(Complex&);
private:
	double real;
	double imag;
;
Complex Complex::operator+(Complex& c) 
	return Complex(this->get_real() + c.get_real(), this->get_imag() + c.get_imag());

Complex Complex::operator-(Complex& c)

	return Complex(this->get_real() - c.get_real(), this->get_imag() - c.get_imag());

Complex Complex::operator*(Complex& c)

	return Complex(this->get_real() * c.get_real()- this->get_imag() * c.get_imag(), this->get_imag() * c.get_real()+this->get_real() * c.get_imag());

Complex Complex::operator/(Complex& c)

	return Complex((real * c.get_real() + imag * c.get_imag())/(pow(c.get_real(),2)+(pow(c.get_imag(), 2))),
		(imag * c.get_real() - real * c.get_imag()) / (pow(c.get_real(), 2) +(pow(c.get_imag(), 2))));

void Complex::display() 
	cout << "(" << real << "," << imag << "i)" << endl;

double Complex::get_real() 
	return real;

double Complex::get_imag() 
	return imag;

int main() 
	Complex c1(3, 4);
	Complex c2(5, -10);
	Complex c3 = c1 + c2;
	c3.display();//(8,-6i)
	c3 = c1 - c2;
	c3.display();//(-2,14i)
	c3 = c1 * c2;
	c3.display();//(55,-10i)
	c3 = c1 / c2;
	c3.display();//(-0.2,0.4i)
	return 0;

例题2:求两个复数之和(c1+c2)、整数和复数之和(c+i或者i+c)

#include<iostream>
using namespace std;
class Complex 
public:
	Complex(double a, double b) :real(a), imag(b) 
	void display();
	double get_real();
	double get_imag();
	Complex operator+(int &i);
	Complex operator+(Complex &c);
	friend Complex operator+(int &,Complex&);
private:
	double real;
	double imag;
;
double Complex::get_real() 
	return real;

double Complex::get_imag() 
	return imag;

Complex Complex::operator+(int& i) 
	return Complex(real + i, imag);

Complex Complex::operator+(Complex &c) 
	return Complex(real + c.get_real(), imag+c.get_imag());

void Complex::display() 
	cout << "(" << real << "," << imag << "i)" << endl;

Complex operator+(int& i, Complex& c)

	return Complex(c.get_real() + i, c.get_imag());

int main() 
	Complex c1(3, 4);
	Complex c2(5, -10);
	int i = 5;
	Complex c3 = c1 + c2;
	c3.display();//(8,-6i)
	c3 = c1 + i;
	c3.display();//(8,4i)
	c3 = i + c1;
	c3.display();//(8,4i)
	return 0;



二.重载流插入运算符“<<”和流提取运算符“>>”

只能作为友元函数,不能将它们定义为成员函数
【个人理解:统一写成非成员函数可能是怕原来的流运算符会调用类库里的 ,无法重载自己定义的】

例题:输入两个矩阵,输出两个矩阵的和

istream& operator>>(istream &,自定义类 &);
ostream& operator<<(ostream &,自定义类 &);
#include<iostream>
using namespace std;
class Matrix 
public:
	Matrix();
	friend ostream& operator<<(ostream& out,Matrix &m);
	friend istream& operator>>(istream& input, Matrix& m);
	friend Matrix operator+(Matrix& a, Matrix& b);
private:
	int a[2][3];
;

//重载运算符“++”————矩阵的相加
Matrix operator+(Matrix& x, Matrix& z) 
	Matrix t;
	for (int i = 0;i < 2;i++) 
		for (int j = 0;j < 3;j++) 
			t.a[i][j] = x.a[i][j] + z.a[i][j];
		
	
	return t;

//重载运算符“<<”————矩阵的输入
ostream& operator<<(ostream& out, Matrix& m) 
	for (int i = 0;i < 2;i++) 
		for (int j = 0;j < 3;j++) 
			out << m.a[i][j] << " ";
		
		out << endl;
	
	return out;

//重载运算符“<<”————矩阵的输出
istream& operator>>(istream& in, Matrix& m) 
	for (int i = 0;i < 2;i++) 
		for (int j = 0;j < 3;j++) 
			in >> m.a[i][j];
		
	
	return in;

//构造函数
Matrix::Matrix() 
	for (int i = 0;i < 2;i++) 
		for (int j = 0;j < 3;j++) 
			a[i][j] = 0;
		
	



int main() 
	Matrix m1, m2, m3;
	cin >> m1 >> m2;
	m3 = m1 + m2;
	cout << m3 << endl;
	return 0;

三.重载类型转换符

将一个类的对象转换成另一类型的数据。

  • 函数名前不能指定函数类型,函数没有参数
  • 类型转换函数只能作为成员函数,因为转换的主体是本类的对象,不能作为友元函数或普通函数。
operator 类型名
	实现转换的语句

例题:处理一个复数和一个double数相加的运算

#include<iostream>
using namespace std;
class Complex 
public:
	Complex() 
		real = 0;
		imag = 0;
	
	Complex(double r) 
		real = r;
		imag = 0;
	
	Complex(double a, double b) :real(a), imag(b) 
	void display();
	double get_real();
	double get_imag();
	operator double()  return real; //重载类型转换符
private:
	double real;
	double imag;
;
void Complex::display() 
	cout << "(" << real << "," << imag << ")" << endl;

int main() 
	Complex c1(3, 4), c2;
	double d;
	d = 2.5 + c1;
	cout << d << endl;
	c2 = Complex(d);
	c2.display();
	return 0;

以上是关于CPP0048复数类四则运算及插入/提取操作的主要内容,如果未能解决你的问题,请参考以下文章

C++运算符的重载

C++运算符的重载

[ C++ ] 类与对象(下)日期类Date补充及流提取和流插入运算符重载

用java 编写一个复数类

java 中实现复数的加减

复数类的运算