第七次作业反馈及参考答案

第七次的模板类编写要求挺高的,鉴于大家的作业完成得不理想,决定本次作业不纳入最后的作业考核中,只供大家练习(大家没意见吧?)。

对于模板类的几点解释:

一:模板类比较特殊,目前绝大多数编译器不支持模板的分离编译模式,所以有3个方案,

(1)将模板类的声明和定义放到一个头文件(.H文件)中;

(2)或者声明和定义还是分开来写,可以在.h文件的最后将.cpp文件包含进来(参考代码选用这种方式);

(3)将类和主函数写在一个CPP文件里。(个人比较推荐这种方式应付考试)

(之前几位同学问我的时候我没有去试过,说的不对,误导了,先道个歉,以这次反馈为准)

PS:不多说了,大家直接看参考答案吧。。

Matrix.h

#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>
using namespace std;

template <typename elemType, int row = 4, int col = 4>
class Matrix
{
	friend class Matrix;

	template <typename elemType, int row, int col>
	friend ostream& operator << (ostream&, const Matrix<elemType, row, col>&);

public:
	Matrix();
	Matrix(const Matrix&);
	~Matrix();

	void ReadData();

	elemType Max() const;
	elemType Min() const;

	template <int irow, int icol>
	bool operator == (const Matrix<elemType, irow, icol>&) const;

	Matrix operator + (const Matrix&) const;

	template<int icol>
	Matrix<elemType, row, icol> operator * (const Matrix<elemType, col, icol>&) const;

private:
	int _row, _col;
	elemType **_matrix;
};

#include "Matrix.cpp"

#endif

Matrix.cpp

#ifndef MATRIX_CPP
#define MATRIX_CPP

#include "Matrix.h"
#include <iostream>
#include <cstdlib>
using namespace std;

template <typename elemType, int row, int col>
Matrix<elemType, row, col>::Matrix()
	: _row(row), _col(col)
{
	_matrix = new elemType*[_row];
	for (int i = 0; i < _row; i ++)
	{
		_matrix[i] = new elemType[_col];
		for (int j = 0; j < _col; j ++)
			_matrix[i][j] = elemType();
	}
}

template <typename elemType, int row, int col>
Matrix<elemType, row, col>::Matrix(const Matrix& rhs)
	: _row(rhs._row), _col(rhs._col)
{
	_matrix = new elemType*[_row];
	for (int i = 0; i < _row; i ++)
	{
		_matrix[i] = new elemType[_col];
		for (int j = 0; j < _col; j ++)
			_matrix[i][j] = rhs._matrix[i][j];
	}
}

template <typename elemType, int row, int col>
Matrix<elemType, row, col>::~Matrix()
{
	for (int i = 0; i < _row; i ++)
		delete[] _matrix[i];
	delete[] _matrix;
}


template <typename elemType, int row, int col>
void Matrix<elemType, row, col>::ReadData()
{
	cout << "Please Input An Matrix Of " << _row << " * " << _col << endl;
	for (int i = 0; i < _row; i ++)
		for (int j = 0; j < _col; j ++)
			cin >> _matrix[i][j];
}

template <typename elemType, int row, int col>
elemType Matrix<elemType, row, col>::Max() const
{
	elemType max(_matrix[0][0]);
	for (int i = 0; i < _row; i ++)
		for (int j = 0; j < _col; j ++)
			if (_matrix[i][j] > max)
				max = _matrix[i][j];
	return max;
}

template <typename elemType, int row, int col>
elemType Matrix<elemType, row, col>::Min() const
{
	elemType min(_matrix[0][0]);
	for (int i = 0; i < _row; i ++)
		for (int j = 0; j < _col; j ++)
			if (min > _matrix[i][j])
				min = _matrix[i][j];
	return min;
}

template <typename elemType, int row, int col>
template <int irow, int icol>
bool Matrix<elemType, row, col>::operator == (const Matrix<elemType, irow, icol>& rhs) const
{
	if (_row != irow || _col != icol)
		return false;
	for (int i = 0; i < _row; i ++)
		for (int j = 0; j < _col; j ++)
			if (_matrix[i][j] != rhs._matrix[i][j])
				return false;
	return true;
}

template <typename elemType, int row, int col>
Matrix<elemType, row, col> Matrix<elemType, row, col>::operator + (const Matrix& rhs) const
{
	Matrix<elemType, row, col> tmp;
	for (int i = 0; i < _row; i ++)
		for (int j = 0; j < _col; j ++)
			tmp._matrix[i][j] = _matrix[i][j] + rhs._matrix[i][j];
	return tmp;
}

template <typename elemType, int row, int col>
template <int icol>
Matrix<elemType, row, icol> Matrix<elemType, row, col>::operator * (const Matrix<elemType, col, icol>& rhs) const
{
	Matrix<elemType, row, icol> tmp;
	for (int i = 0; i < _row; i ++)
		for (int j = 0; j < rhs._col; j ++)
		{
			bool plus = false;
			for (int k = 0; k < _col; k ++)
				if (!plus)
				{
					tmp._matrix[i][j] = _matrix[i][k] * rhs._matrix[k][j];
					plus = true;
				}
				else
					tmp._matrix[i][j] = tmp._matrix[i][j] + _matrix[i][k] * rhs._matrix[k][j];
		}	
		return tmp;
}

template <typename elemType, int row, int col>
inline ostream& operator << (ostream& os, const Matrix<elemType, row, col>& rhs)
{
	for (int i = 0; i < rhs._row; i ++)
	{
		for (int j = 0; j < rhs._col; j ++)
			os << rhs._matrix[i][j] << " ";
		os << endl;
	}
	return os;
}

#endif

main.cpp

#include "Matrix.h"
#include <iostream>
using namespace std;

int main()
{
	Matrix<int, 2, 3> matrix_1;
	matrix_1.ReadData();

	Matrix<int, 2, 3> matrix_2(matrix_1);

	Matrix<int, 3, 2> matrix_3;
	matrix_3.ReadData();

	cout << "matrix_1:\n" << matrix_1;
	cout << "matrix_1's maximum number: " << matrix_1.Max() << endl
		<< "matrix_1's minimum number: " << matrix_1.Min() << endl;

	cout << "matrix_1 " << (matrix_1 == matrix_2 ? "" : "not") << "equal to matrix_2" << endl;
	cout << "matrix_1:\n" << matrix_1;
	cout << "matrix_2:\n" << matrix_2;

	cout << "matrix_1 plus matrix_2" << endl;
	cout << matrix_1 + matrix_2 << endl;

	cout << "matrix_1 " << (matrix_1 == matrix_3 ? "" : "not") << "equal to matrix_3" << endl;
	cout << "matrix_1:\n" << matrix_1;
	cout << "matrix_3:\n" << matrix_3;
	cout << "matrix_1 multi matrix_3" << endl
		<< matrix_1 * matrix_3;
	return 0;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 【优秀课程设计】主要针对计算机相
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值