C++第十章习题

本文展示了如何在C++中重载运算符,包括复数类的加减乘除以及矩阵的相加。通过成员函数和非成员函数的方式实现复数运算,同时介绍了如何处理double型数据和Complex复数的相加,利用类型转换函数实现兼容。
摘要由CSDN通过智能技术生成

1.重载+函数,实现复数的加法运算,运算符重载为非成员 非友元的普通函数

#include <iostream>
using namespace std;
class Complex {
public:
	Complex() {
		real = 0;imag = 0;
	}
	Complex(double r, double i) { real = r;imag = i; }
	double get_r();
	double get_i();
	void display();
private:
	double real, imag;
};
double Complex::get_r() {
	return real;
}
double Complex::get_i() {
	return imag;
}
void Complex::display() {
	cout << "(" << real << "," << imag << "i" <<")" << endl;
}
//
Complex operator+(Complex& c1, Complex& c2) {
	return Complex(c1.get_r() + c2.get_r(), c1.get_i() + c2.get_i());
}
int main() {
	Complex c1(3, 4), c2(5, -10), c3;
	c3 = c1 + c2;
	c3.display();
	return 0;
}

2 重载 + - * /,使之能够用于复数类Complex的运算,运算符重载函数作为成员函数

#include <iostream>
using namespace std;
class Complex {
public:
	Complex() {
		real = 0;imag = 0;
	}
	Complex(double r, double i) { real = r;imag = i; }
	Complex operator +(Complex& c);
	Complex operator -(Complex& c);
	Complex operator *(Complex& c);
	Complex operator /(Complex& c);
	void display();
private:
	double real, imag;
};
Complex Complex:: operator +(Complex& c) {
	double r, i;
	r = real + c.real;
	i = imag + c.imag;
	return Complex(r, i);
}
Complex Complex::operator -(Complex& c) {
	double r, i;
	r = real - c.real;
	i = imag - c.imag;
	return Complex(r, i);
}
Complex Complex::operator *(Complex& c) {
	double r, i;
	r = real * c.real - imag * c.imag;
	i = imag * c.real + real * c.imag;
	return Complex(r, i);
}
Complex Complex::operator /(Complex& c) {
	double r, i;
	r = (real * c.real+imag * c.imag)/(c.real*c.real+c.imag*c.imag);
	i = (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag);
	return Complex(r, i);
}
void Complex::display() {
	cout << "(" << real << "," << imag << "i" <<")" << endl;
}
//

int main() {
	Complex c1(3, 4), c2(5, -10), c3;
	cout << "c1+c2=";
	c3 = c1 + c2;
	c3.display();
	cout << "c1-c2=";
	c3 = c1 - c2;
	c3.display();
	cout << "c1*c2=";
	c3 = c1 * c2;
	c3.display();
	cout << "c1/c2=";
	c3 = c1 / c2;
	c3.display();
	return 0;
}

4.重载运算符,使之能用于矩阵的相加

#include <iostream>
using namespace std;
class Matrix {
public:
	Matrix();
	friend Matrix operator+(Matrix& m1, Matrix& m2);
	void input();
	void output();
private:
	int m[2][3];
};
Matrix::Matrix() {
	for (int i = 0;i < 2;i++)
		for (int j = 0;j < 3;j++)
			m[i][j] = 0;
}
void Matrix::input() {
	cout << "input Matrix:" << endl;
	for (int i = 0;i < 2;i++)
		for (int j = 0;j < 3;j++)
			cin>>m[i][j];
}
void Matrix::output() {
	cout << "input Matrix:" << endl;
	for (int i = 0;i < 2;i++) {
		for (int j = 0;j < 3;j++)
			cout<< m[i][j]<<" ";
		cout << endl;
	}	
}
Matrix operator+(Matrix& m1, Matrix& m2) {
	Matrix temp;
	for (int i = 0;i < 2;i++)
		for (int j = 0;j < 3;j++)
			temp.m[i][j] = m1.m[i][j] + m2.m[i][j];
	return temp;
}
int main()
{
	Matrix m1, m2, m3;
	m1.input();
	m2.input();
	cout << "m1:" << endl;
	m1.output();
	cout << "m2:" << endl;
	m2.output();
	m3 = m1 + m2;
	cout << "m1+m2=:" << endl;
	m3.output();
	return 0;
}

5.重载流插运算符
记得重载函数,需要return 一个类型

#include <iostream>
using namespace std;
class Matrix {
public:
	Matrix();
	friend Matrix operator+(Matrix& m1, Matrix& m2);
	friend ostream& operator<<(ostream&, Matrix&);
	friend istream& operator>>(istream&, Matrix&);
	void input();
	void output();
private:
	int m[2][3];
};
Matrix::Matrix() {
	for (int i = 0;i < 2;i++)
		for (int j = 0;j < 3;j++)
			m[i][j] = 0;
}
void Matrix::input() {
	cout << "input Matrix:" << endl;
	for (int i = 0;i < 2;i++)
		for (int j = 0;j < 3;j++)
			cin>>m[i][j];
}
void Matrix::output() {
	cout << "input Matrix:" << endl;
	for (int i = 0;i < 2;i++) {
		for (int j = 0;j < 3;j++)
			cout<< m[i][j]<<" ";
		cout << endl;
	}	
}
Matrix operator+(Matrix& m1, Matrix& m2) {
	Matrix temp;
	for (int i = 0;i < 2;i++)
		for (int j = 0;j < 3;j++)
			temp.m[i][j] = m1.m[i][j] + m2.m[i][j];
	return temp;
}
ostream& operator <<(ostream &out, Matrix &m1) {
	for (int i = 0;i < 2;i++) {
		for (int j = 0;j < 3;j++)
			out << m1.m[i][j]<<" ";
		out << endl;
	}
	return out;
}
istream& operator>>(istream& in, Matrix &m1)
{
	cout << "input Matrix: ";
	for (int i = 0;i < 2;i++) {
		for (int j = 0;j < 3;j++)
			in >> m1.m[i][j];
	}
	return in;
}
int main()
{
	Matrix m1, m2, m3;
	cin >> m1;
	cin >> m2;
	cout << endl <<"m1= "<<endl << m1 << endl;
	cout << endl << "m2= "<<endl << m2 << endl;
	m3 = m1 + m2;
	cout << endl << "m1+m2= " << m3 << endl;
	return 0;
}

6.处理double型数据和Complex复数的相加
利用类型转换函数

#include <iostream>
using namespace std;
class Complex {
public:
	Complex() { real = 0;imag = 0; }
	Complex(double r) { real = r;imag = 0; }
	Complex(double r, double i) :real(r), imag(i) {}
	operator double() { return real; }
	void display();
private:
	double real, imag;
};
void Complex::display() {
	cout << "(" << real << "," << imag << ")" << endl;
}
int main() {
	Complex c1(3, 4), c2;
	double d1;
	d1 = 2.5 + c1;
	c2 = Complex(d1);
	c2.display();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值