用C++实现可视化的矩阵类(可以进行加,减,乘运算)

        最近学习了c++中类的封装和函数重载知识,于是编写一个普通的矩阵类来练习,呵呵。

这是代码:

#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
using namespace std;

class Matrix {//一个矩阵类
public:
	Matrix(int x,int y):row(x),column(y){
		vector<int>base(column, 0);
		for (int i = 0; i < row; i++)matrix.push_back(base);
	}

	int row, column;
	vector<vector<int>>matrix;//使用vector二维数组存储
};

Matrix rand_matrix(int r, int c) {
	Matrix ret(r, c);
	for (int i = 0; i < r; i++) {
		for (int j = 0; j < c; j++) {
			ret.matrix[i][j] = rand() % 10;
		}
	}//给矩阵ret随机赋值
	return ret;
}



Matrix operator*(const Matrix& a, const Matrix& b) {//重载*,实现矩阵乘法
	int row_a = a.row, col_a = a.column, row_b = b.row, col_b = b.column;

	if (col_a != row_b) {
		cout << "Error:Your fool matrix can't be producted !" << endl;
		exit(0);
	}//判断矩阵是否可以相乘

	Matrix c(row_a, col_b);//结果矩阵

	for (int i = 0; i < row_a; i++) {
		for (int j = 0; j < col_b; j++) {
			for (int k = 0; k < row_b; k++) {
				c.matrix[i][j] += (a.matrix[i][k] * b.matrix[k][j]);
			}
		}
	}
	return c;
}


Matrix operator+(const Matrix& a, const Matrix& b) {
	int ra = a.row, ca = a.column, rb = b.row, cb = b.column;
	if (ca != cb||ra!=rb) {
		cout << "Error:Your fool matrix can't be added !" << endl;
		exit(0);
	}
	Matrix c(ra, cb);
	for (int i = 0; i < ra; i++) {
		for (int j = 0; j < cb; j++) {
			c.matrix[i][j] = a.matrix[i][j] + b.matrix[i][j];
		}
	}
	return c;
}

Matrix operator-(const Matrix& a, const Matrix& b) {
	int ra = a.row, ca = a.column, rb = b.row, cb = b.column;
	if (ca != cb || ra != rb) {
		cout << "Error:Your fool matrix can't be subtracted !" << endl;
		exit(0);
	}
	Matrix c(ra, cb);
	for (int i = 0; i < ra; i++) {
		for (int j = 0; j < cb; j++) {
			c.matrix[i][j] = a.matrix[i][j] - b.matrix[i][j];
		}
	}
	return c;
}



ostream& operator<<(ostream& out, Matrix& m) {//重载左移运算符,使矩阵可以用cout输出
	int ro = m.row, co = m.column;
	for (int i = 0; i < ro; i++) {
		out << "    |";
		for (int j = 0; j < co; j++) {
			printf("%4d", m.matrix[i][j]);
		}
		out << "   |" << endl;
	}
	return out;//返回cout的引用out,方便<<后接着输出
}







int main() {
	srand(time(0));
	Matrix a(5, 5);//定义一个5*5的矩阵
	a.matrix = { {1,2,3,4,5 },
			     {6,7,8,9,10},
			     {5,9,8,7,4 },
			     {3,2,1,4,5 },
			     {5,4,8,7,6 } };//手动给矩阵赋值


	cout << "Matrix a:" << endl;
	cout << a<<endl;//输出矩阵

	Matrix A = rand_matrix(3, 3);
	Matrix B=rand_matrix(3, 3);
	Matrix C=rand_matrix(3, 5);
	//生成A,B,C三个随机矩阵


	cout << "Matrix A:" << endl;
	cout << A<<endl;
	cout << "Matrix B:" << endl;
	cout << B<<endl;
	cout << "Matrix C:" << endl;
	cout << C << endl;


	cout << "A+B:" << endl;
	Matrix add_AB = A + B;
	cout << add_AB << endl;

	cout << "A-B:" << endl;
    Matrix subtract_AB = A - B;
	cout << subtract_AB << endl;

	cout << "A*B:" << endl;
	Matrix product_AB = A * B;
	cout << product_AB << endl;

	cout << "A*C:" << endl;
	Matrix product_AC = A * C;
	cout << product_AC << endl;

	cout << "A+C:" << endl;
	Matrix add_AC = A + C;
	cout << add_AC << endl;


	return 0;
}

运行效果

欢迎各位评论区的大佬指点(‘—’)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值