1552 简单的Matrix运算
来源:互联网 发布:php三级分销思路 编辑:程序博客网 时间:2024/06/08 02:15
Task
完善Matrix类的定义,实现简单的Matrix运算,类的定义,main函数已给出,你需要编写Matrix.h文件实现具体函数,记得包含类的定义。
Detail
class Matrix //定义Matrix类 {public: Matrix(); //默认构造函数 ~Matrix(); //析构函数 Matrix(const Matrix &); //拷贝构造函数 Matrix(int row, int col); //含参数构造函数 Matrix operator+(const Matrix &)const; //重载运算符“+” Matrix& operator=(const Matrix &); //重载运算符“=” Matrix transpose()const; //矩阵的转置 void display()const; //输出数据函数 private: int row; //矩阵的行 int col; //矩阵的列 int** mat; //用于储存矩阵};
Hint
类的构造函数与析构函数,运算符重载,new与delete
Sample Input 1
2 31 2 31 2 31 2 31 2 3
Sample Output 1
Matrix a:1 2 31 2 3Matrix b:1 2 31 2 3Matrix c = Matrix a + Matrix b :2 4 62 4 6Matrix a transpose to Matrix d:1 12 23 3
Sample Input 2
2 21 23 44 32 1
Sample Ouput 2
Matrix a:1 23 4Matrix b:4 32 1Matrix c = Matrix a + Matrix b :5 55 5Matrix a transpose to Matrix d:1 32 4
Provided Codes
Matrix.cpp
#include <iostream>#include"Matrix.h"using namespace std;int main() { int row, col; cout << "input the row and the col for Matrix a, b" << endl; cin >> row >> col; Matrix a(row, col), b(row, col), c(a), d; cout << endl << "Matrix a:" << endl; a.display(); cout << endl << "Matrix b:" << endl; b.display(); c = a + b;//用重载运算符“+”实现两个矩阵相加 cout << endl << "Matrix c = Matrix a + Matrix b :" << endl; c.display(); cout << endl << "Matrix a transpose to Matrix d:" << endl; d = a.transpose(); d.display(); return 0;}
Submission
Matrix.h
#ifndef MATRIX_H#define MATRIX_H#include <iostream>#include <assert.h>using namespace std;class Matrix //定义Matrix类 {public: Matrix(); //默认构造函数 ~Matrix(); //析构函数 Matrix(const Matrix &); //拷贝构造函数 Matrix(int row, int col); //含参数构造函数 Matrix operator+(const Matrix &)const; //重载运算符“+” Matrix& operator=(const Matrix &); //重载运算符“=” Matrix transpose()const; //矩阵的转置 void display()const; //输出数据函数 private: int row; //矩阵的行 int col; //矩阵的列 int** mat=NULL; //用于储存矩阵};Matrix::Matrix() : row(0), col(0) {}Matrix::Matrix(int Row, int Col) { row = Row; col = Col; mat = new int*[row]; for (int i = 0; i < row; i++) { mat[i] = new int[col]; } assert(mat != NULL); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cin>>mat[i][j]; } }}Matrix::Matrix(const Matrix &m) { row = m.row; col = m.col; mat = new int*[row]; for (int i = 0; i < row; i++) { mat[i] = new int[col]; } assert(mat != NULL); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { mat[i][j]=m.mat[i][j]; } }}Matrix::~Matrix() { for (int i = 0; i < row; i++) { delete[]mat[i]; } delete[]mat;}Matrix Matrix::operator+(const Matrix &b)const{ Matrix c; c.row = row; c.col = col; c.mat = new int*[row]; for (int i = 0; i < row; i++) { c.mat[i] = new int[col]; } assert(c.mat != NULL); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { c.mat[i][j] = mat[i][j]+b.mat[i][j]; } } return c;}Matrix& Matrix::operator=(const Matrix &p) { if (row != p.row||col != p.col) { for (int i = 0; i < row; i++) { delete[]mat[i]; } delete[]mat; row = p.row; col = p.col; mat = new int*[row]; for (int i = 0; i < row; i++) { mat[i] = new int[col]; } assert(mat != NULL); } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { mat[i][j] = p.mat[i][j]; } } return *this;}Matrix Matrix::transpose()const { Matrix a; a.row = col; a.col = row; a.mat = new int*[col]; for (int i = 0; i < col; i++) { a.mat[i] = new int[row]; } assert(a.mat != NULL); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { a.mat[j][i] = mat[i][j]; } } return a;}void Matrix::display()const { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout << mat[i][j]<<" "; if (j == col - 1) { cout << endl; } } }}#endif
Standard Answer
Matrix.h
#include <iostream>using namespace std;class Matrix //定义Matrix类 {public: Matrix(); //构造函数 ~Matrix(); //析构函数 Matrix(const Matrix &); //拷贝构造函数 Matrix(int row, int col); //默认构造函数 Matrix operator+(const Matrix &)const; //重载运算符“+” Matrix operator=(const Matrix &); //重载运算符“=” Matrix transpose()const; //矩阵的转置 void display()const; //输出数据函数 private: int row; //矩阵的行 int col; //矩阵的列 int** mat; //用于储存矩阵};Matrix::Matrix() { row = 0; col = 0; mat = NULL;}Matrix::~Matrix() { if(mat != NULL){ for (int i = 0; i < row; i++) { delete[]mat[i]; } delete[]mat; mat = NULL; }}Matrix::Matrix(const Matrix& a) { row = a.row; col = a.col; mat = new int*[row]; for (int i = 0; i < row; i++) { mat[i] = new int[col]; } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { mat[i][j] = a.mat[i][j]; } }}Matrix::Matrix(int row, int col)//定义构造函数 { this->row = row; this->col = col; mat = new int*[row]; for (int i = 0; i < row; i++) { mat[i] = new int[col]; } for (int i = 0; i<row; i++) for (int j = 0; j<col; j++) cin >> mat[i][j];}Matrix Matrix::operator+(const Matrix &b)const//定义重载运算符“+”函数 { Matrix c(b); for (int i = 0; i<b.row; i++) for (int j = 0; j<b.col; j++) { c.mat[i][j] = mat[i][j] + b.mat[i][j]; } return c;}Matrix Matrix::operator=(const Matrix& a){ row = a.row; col = a.col; if (mat == NULL) { mat = new int*[row]; for (int i = 0; i < row; i++) { mat[i] = new int[col]; } } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { mat[i][j] = a.mat[i][j]; } } return *this;}Matrix Matrix::transpose()const { Matrix d; d.row = col; d.col = row; d.mat = new int*[col]; for (int i = 0; i < col; i++) { d.mat[i] = new int[row]; } for (int j = 0; j < col; j++) for (int i = 0; i < row; i++) { d.mat[j][i] = mat[i][j]; } return d;}void Matrix::display()const//定义输出数据函数 { for (int i = 0; i<row; i++) { for (int j = 0; j<col; j++) { cout << mat[i][j] << " "; } cout << endl; }}
阅读全文
0 0
- 1552 简单的Matrix运算
- 矩阵的运算 Matrix Operations
- 一个简单的Matrix实现
- android matrix的简单操作
- 简单的阶乘运算。
- 简单的字符串运算
- 简单的级数运算
- 复数的简单运算
- 简单的加减乘除运算
- 十六进制的简单运算
- 简单的位运算
- 简单的位运算
- android中的matrix的简单应用
- Matrix Chain Multiplication -uva简单的数据结构
- 简单介绍Matrix的图形特效
- 简单介绍Matrix的图形特效
- BitmapShader、ShapeDrawable、Matrix的简单学习
- Emgu Matrix[Double] matrix = new Matrix[Double](height, width)的简单应用
- Python如何使用装饰器(Decorator)修改yield function返回值
- [软件人生]人格美容与素质和教养
- 接口和抽象类的比较
- 阻塞非阻塞,同步和异步的区别
- 系统限制和选项limit(一)
- 1552 简单的Matrix运算
- Awesome Resources
- 1061. 判断题(15)
- JS类型判断全总结
- Codeforces 459E. Pashmak and Graph (DP)
- linux gcc
- windows和Ubuntu14.04间通过vmware tools实现文件共享的问题
- 1019. 数字黑洞 (20)
- Shader学习笔记(2)- 基本概念与基础效果(1)