C++项目练手:矩阵类的功能实现

C++项目练手:矩阵类的功能实现

C++课程设计:矩阵类的相关功能实现

矩阵简述: 实数矩阵是由一个按照长方阵列排列的实数集合。除数据外,两个实数矩阵可以进行加法和乘法运算,一个矩阵也可以和一个实数相乘,得到一个新的矩阵,请基于抽象出的矩阵的属性和方法,定义和实现一个矩阵类,并编写主函数,测试矩阵对象的构造和测试。

功能实现要求:

  1. 在缺省情况下,构造一个4*4的数据全为0的矩阵
  2. 可以按给定的行数和列数,构造一个值为全0的矩阵
  3. 可以使用给定的矩阵构造一个新的矩阵(定义并使用拷贝构造函数)
  4. 可以获取矩阵中的给定行、列位置的值
  5. 可以对矩阵中给定行、列位置的元素进行赋值
  6. 矩阵对象构造完成后,不允许变更矩阵的行数和列数
  7. 可以获取矩阵对象的行数、列数
  8. 一个矩阵可以和另一个给定的矩阵进行加法、乘法运算
  9. 一个矩阵可以和给定的一个实数进行数乘运算

拓展功能实现:

  1. 在构造函数中,可以使用new构造一个一维实型数组来存储矩阵中的数据
  2. 根据矩阵相关功能的实现,对+,*,<<,>>,[ ],等相关运算符进行重载和使用
  3. 实现功能选择显现窗口
  4. 具有检查错误输入的功能

根据分析项目要求,可将该项目分解成多个模块,包括矩阵类的定义,矩阵类相关成员函数的实现,以及功能选择界面的显示

类的定义如下:

#pragma once
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

class Matrix { //矩阵类
	private:
		int x, y; //矩阵的行列
		int num[105][105]; //设置二维数组实现矩阵
	public:
		Matrix();
		Matrix(int, int); //矩阵类的构造函数
		Matrix(const Matrix& M); //矩阵类的拷贝构造函数
		~Matrix(); //矩阵类的析构函数

		void OutPutM(); //用类内的成员函数来输出矩阵
		void GetMatrix_xy(); //获取矩阵的行列数
		void AssMatrix_xy(); //对矩阵的某行某列赋值
		void GetMatrix_xyNum(); //获取矩阵某行某列的值
		int returnX(); //获取矩阵行数
		int returnY(); //获取矩阵列数

		int& operator [] (int); //类成员函数,重载"[]"运算符,实现对矩阵下标的访问

		friend Matrix operator + (Matrix& a, Matrix& b); //友元函数,重载"+"运算符,实现对矩阵加法的运算
		friend Matrix operator * (int num, Matrix a); //友元函数,重载"*"运算符,实现对矩阵乘法的运算
		friend Matrix operator * (Matrix a, int num); //友元函数,重载"*"运算符,实现对矩阵乘法的运算
		friend Matrix operator * (Matrix& a, Matrix& b); //友元函数,重载"*"运算符,实现矩阵之间乘法的运算

		friend ostream& operator << (ostream& outs, const Matrix& a); //重载"<<"流运算符,实现矩阵的输出
		friend istream& operator >> (istream& in, Matrix& a); //重载">>"流运算符,实现矩阵的输入
};

类的成员函数以及友元函数定义如下:

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

Matrix::Matrix() { //无参数的构造函数
	x = 4;
	y = 4;
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			num[i][j] = 0;
		}
	}
}

Matrix::Matrix(int x1 = 4, int y1 = 4) : x(x1),y(y1) { //在缺省的情况下构造一个4*4的基本矩阵
	for (int i = 1; i <= x; i++) {
		for (int j = 0; j <= y; j++) {
			num[i][j] = 0;
		}
	}
}

Matrix::Matrix(const Matrix& M) { //自定义拷贝构造函数
	x = M.x;
	y = M.y;
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			num[i][j] = M.num[i][j];
		}
	}
}

Matrix::~Matrix() {} //析构函数

void Matrix::OutPutM() { //用类内的成员函数来输出矩阵
	system("cls");
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			if (j == 1)
				cout << "|" << setw(3) << num[i][j] << setw(3);
			else if (j == y)
				cout << setw(3) << num[i][j] << setw(3) << "|" << endl;
			else 
				cout << setw(3) << num[i][j]<< setw(3);
		}
	}
}

void Matrix::GetMatrix_xy() { //获取矩阵的行列数
	system("cls");
	cout << "该矩阵的行列数:" << endl;
	cout << "行数:" << x << " 列数:" << y << endl;
	system("pause");
}

void Matrix::AssMatrix_xy() { //对矩阵的某行某列赋值
	int x1, y1;
	int ans;
start:
	cout << "请输入想要赋值矩阵的行,列位置:";
	cin >> x1 >> y1;
	cout << "请赋值:";
	cin >> ans;
	if (x1 > x || y1 > y) {
		if (x1 > x)
			cout << "该矩阵不存在" << x1 << "行" << endl;
		if (y1 > y)
			cout << "该矩阵不存在" << y1 << "列" << endl;
		cout << "请按下任意键,重新返回输入界面" << endl;
		system("pause");
		goto start;
	}
	else {
		for (int i = 1; i <= x; i++) {
			for (int j = 1; j <= y; j++) {
				if (i == x1 && j==y1) {
					num[i][j] = ans;
				}
			}
		}
	}
}

void Matrix::GetMatrix_xyNum() { //获取矩阵某行某列的值
	int x1, y1;
start:
	system("cls");
	cout << "请输入想要获取赋值矩阵该行该列位置的值:";
	cin >> x1 >> y1;
	if (x1 > x || y1 > y) {
		if (x1 > x)
			cout << "该矩阵不存在" << x1 << "行" << endl;
		if (y1 > y)
			cout << "该矩阵不存在" << y1 << "列" << endl;
		cout << "请按下任意键,重新返回输入界面" << endl;
		system("pause");
		goto start;
	}
	else {
		cout << "矩阵" << x1 << "行" << y1 << "列的值:" << num[x1][y1] << endl;
	}
}

int Matrix::returnX() { //获取矩阵行数
	return x;
}

int Matrix::returnY() { //获取矩阵列数
	return y;
}

int& Matrix::operator [] (int ans) { //类成员函数,重载"[]"运算符,实现对矩阵下标的访问
	int x1 = (ans / x) + 1;
	int y1 = ans % x;
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			if (i == x1 && j == y1)
				return num[i][j];
		}
	}
}

Matrix operator + (Matrix& a,Matrix& b) { //重载"+"运算符,实现对矩阵加法的运算
	int x = a.x, y = a.y;
	Matrix ans(x, y);
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			ans.num[i][j] = a.num[i][j] + b.num[i][j];
		}
	}
	return ans;
}

Matrix operator * (int num,Matrix a) { //重载"*"运算符,实现实数对矩阵的乘法
	int x = a.x, y = a.y;
	Matrix ans(x, y);
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			ans.num[i][j] = num * a.num[i][j];
		}
	}
	return ans;
}

Matrix operator * (Matrix a, int num) { //重载"*"运算符,实现实数对矩阵的乘法
	int x = a.x, y = a.y;
	Matrix ans(x, y);
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			ans.num[i][j] = num * a.num[i][j];
		}
	}
	return ans;
}

Matrix operator * (Matrix& a,Matrix& b) { //友元函数,重载"*"运算符,实现矩阵之间乘法的运算
	int x = a.x, y = b.y;
	Matrix ans(x, y);
	int n = 1;
	for (int i = 1; i <= a.x; i++) {
		for (int j = 1; j <= b.y; j++) {
			while (n <= b.y) {
				ans.num[i][j] += a.num[i][n] * b.num[n][j];
				n++;
			}
		}
		n = 1;
	}
	return ans;
}

ostream& operator << (ostream& outs, const Matrix& a) { //重载"<<"流运算符,实现矩阵的输出
	for (int i = 1; i <= a.x; i++) {
		for (int j = 1; j <= a.y; j++) {
			if (j == 1)
				cout << "|" << setw(3) << a.num[i][j] << setw(3);
			else if (j == a.y)
				cout << setw(3) << a.num[i][j] << setw(3) << "|" << endl;
			else
				cout << setw(3) << a.num[i][j] << setw(3);
		}
	}
	return outs;
}

istream& operator >> (istream& in,Matrix& a) { //重载">>"流运算符,实现矩阵的输入
	int x = a.x, y = a.y;
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			cin >> a.num[i][j];
		}
	}
	return in;
}

主函数代码如下:

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

int main()
{
	int a, b, c, d;
	cout << "-----矩阵类的计算-----" << endl;
	cout << "请输入矩阵A的行,列数:";
	cin >> a >> b;
	Matrix A(a, b);
	cout << "请输入矩阵A:" << endl;
	cin >> A;
	cout << "请输入矩阵B的行,列数:";
	cin >> c >> d;
	Matrix B(c, d);
	cout << "请输入矩阵B:" << endl;
	cin >> B;

	bool judge = true; //judge用来判断循环是否进行

	while (judge) {
		system("cls");
		cout << "-----矩阵类的功能-----" << endl;
		cout << "1.获取矩阵的行列数" << endl;
		cout << "2.对矩阵的某行某列赋值" << endl;
		cout << "3.获取矩阵某行某列的值" << endl;
		cout << "4.矩阵之间的运算" << endl;
		cout << "5.输出矩阵" << endl;
		cout << "6.退出系统" << endl;

		cout << endl << "请选择功能:";
		int choose;
		char ans1, ans2, ans3, ans4_2, ans5;
		int ans4;

		int ansxA, ansyA, ansxB, ansyB;

		cin >> choose;
		switch (choose)
		{
		case 1: //1.获取矩阵的行列数
		start1:
			system("cls");
			cout << "功能1:获取矩阵的行列数" << endl;
			cout << "请选择获取A或B的矩阵行列数:";
			cin >> ans1;
			if (ans1 == 'A') {
				A.GetMatrix_xy();
			}
			else if (ans1 == 'B') {
				B.GetMatrix_xy();
			}
			else {
				cout << "错误输入,按任意键将重新返回选择" << endl;
				system("pause");
				goto start1;
			}
			break;
		case 2: //2.对矩阵的某行某列赋值
		start2:
			system("cls");
			cout << "功能2:对矩阵的某行某列赋值" << endl;
			cout << "请选择矩阵A或矩阵B:" << endl;
			cin >> ans2;
			if (ans2 == 'A') {
				cout << A << endl;
				A.AssMatrix_xy();
			}
			else if (ans2 == 'B') {
				cout << B << endl;
				B.AssMatrix_xy();
			}
			else {
				cout << "错误输入,按任意键将重新返回选择" << endl;
				system("pause");
				goto start2;
			}
			break;
		case 3: //获取矩阵某行某列的值
			start3:
			system("cls");
			cout << "功能3:获取矩阵某行某列的值" << endl;
			cout << "请选择矩阵A或矩阵B:" << endl;
			cin >> ans3;
			if (ans3 == 'A') {
				A.GetMatrix_xyNum();
				system("pause");
			}
			else if (ans3 == 'B') {
				B.GetMatrix_xyNum();
				system("pause");
			}
			else {
				cout << "错误输入,按任意键将重新返回选择" << endl;
				system("pause");
				goto start3;
			}
			break;
		case 4: //矩阵之间的运算
			start4:
			system("cls");
			cout << "功能4:矩阵之间的运算" << endl;
			cout << "1.矩阵相加" << endl;
			cout << "2.实数与矩阵相乘" << endl;
			cout << "3.矩阵与矩阵之间相乘" << endl;
			cout << endl << "请选择:";
			cin >> ans4;
			switch (ans4) {
			case 1:
				ansxA = A.returnX();
				ansyA = A.returnY();
				ansxB = B.returnX();
				ansyB = B.returnY();
				if (ansxA != ansxB || ansyA != ansyB) {
					cout << "矩阵A与矩阵B的行列不相等,无法相加" << endl;
					system("pause");
				}
				else if (ansxA == ansxB && ansyA == ansyB) {
					cout << "A + B = " << endl;
					cout << A + B << endl;
					system("pause");
				}
				break;
			case 2:
				int x;
				start4_2:
				cout << "请选择被乘矩阵(A或B):";
				cin >> ans4_2;
				if (ans4_2 == 'A') {
					cout << "请输入乘数:";
					cin >> x;
					cout << x << " * A = " << endl;
					cout << x * A << endl;
					system("pause");
				}
				else if (ans4_2 == 'B') {
					cout << "请输入乘数:";
					cin >> x;
					cout << x << " * B = " << endl;
					cout << x * B << endl;
					system("pause");
				}
				else {
					cout << "错误输入,按任意键将重新返回选择" << endl;
					system("pause");
					goto start4_2;
				}
				break;
			case 3:
				ansyA = A.returnY();
				ansxB = B.returnX();
				if (ansyA != ansxB) {
					cout << "矩阵A的列与矩阵B的行不相等,无法相乘" << endl;
					system("pause");
				}
				else if (ansyA == ansxB) {
					cout << "A * B = " << endl;
					cout << A * B << endl;
					system("pause");
				}
				break;
			default:
				cout << "错误输入,按任意键将重新返回选择" << endl;
				system("pause");
				goto start4;
				break;
			}
			break;
		case 5: //输出矩阵
			start5:
			system("cls");
			cout << "请选择输出矩阵(A或B):";
			cin >> ans5;
			if (ans5 == 'A') {
				cout << "矩阵A:" << endl << A << endl;
				system("pause");
			}
			else if (ans5 == 'B') {
				cout << "矩阵B:" << endl << B << endl;
				system("pause");
			}
			else {
				cout << "错误输入,按任意键将重新返回选择" << endl;
				system("pause");
				goto start5;
			}
			break;
		case 6: //退出系统
			judge = false;
			exit(0);
		default:
			cout << "错误输入,按任意键将重新返回选择" << endl;
			system("pause");
			break;
		}
	}
	return 0;
}

其中,值得注意的知识点的是输入输出流操作符的重载,将矩阵输出重载到cout之后的流操作符中,具体实现代码如下:

istream& operator >> (istream& in,Matrix& a) { //重载">>"流运算符,实现矩阵的输入
	int x = a.x, y = a.y;
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			cin >> a.num[i][j];
		}
	}
	return in;
}
ostream& operator << (ostream& outs, const Matrix& a) { //重载"<<"流运算符,实现矩阵的输出
	for (int i = 1; i <= a.x; i++) {
		for (int j = 1; j <= a.y; j++) {
			if (j == 1)
				cout << "|" << setw(3) << a.num[i][j] << setw(3);
			else if (j == a.y)
				cout << setw(3) << a.num[i][j] << setw(3) << "|" << endl;
			else
				cout << setw(3) << a.num[i][j] << setw(3);
		}
	}
	return outs;
}

以及矩阵中将下标[ ]运算符通过类成员函数的方式进行重载,实现对矩阵下标访问的功能,代码如下:

int& Matrix::operator [] (int ans) { //类成员函数,重载"[]"运算符,实现对矩阵下标的访问
	int x1 = (ans / x) + 1;
	int y1 = ans % x;
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			if (i == x1 && j == y1)
				return num[i][j];
		}
	}
}

总结:该代码并有没有使用new动态数组进行矩阵的存储,而是采用的二维数组的方式进行存储,其优点在于能够较于理解,但并未具备动态存储的能力

  • 6
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值