⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.cpp

📁 Matrix implemented on C++
💻 CPP
字号:
#include <iostream>
using namespace std;

class matrix
{
public:
	matrix ();
	void getmatrix ();
    void print ();
    matrix operator+ (const matrix& othermatrix)const;
    matrix operator- (const matrix& othermatrix)const;
    matrix operator* (const matrix& othermatrix)const;
private:
	int rows;
	int cols;
	int **p;
};
matrix::matrix ()
{
	cout<<"Enter number of rows and columns of the matrix"<<endl;
	cin>>rows>>cols;
	p=new int* [rows];
	for (int i=0;i<rows;i++)
		p[i]=new int [cols];

	for ( i=0;i<rows;i++)
		for (int j=0;j<cols;j++)
			p[i][j]=0;
}
void matrix::getmatrix ()
{
		
	cout<<"Enter elements of the matrix"<<endl;
	for (int n=0;n<rows;n++)
		for (int j=0;j<cols;j++)
			cin>>p[n][j];
}
void matrix::print ()
{
	for (int i=0;i<rows;i++)
		for (int j=0;j<cols;j++)
			cout<<p[i][j];
}
matrix matrix::operator+ (const matrix& othermatrix)const
{
    matrix temp;
    if (rows==othermatrix.rows && cols==othermatrix.cols)
	for (int i=0;i<rows;i++)
		for (int j=0;j<cols;j++)
			temp.p[i][j]=p[i][j]+othermatrix.p[i][j];
		return temp;
}
matrix matrix::operator- (const matrix& othermatrix)const
{
    matrix temp;
	if (rows==othermatrix.rows && cols==othermatrix.cols)
    for (int i=0;i<rows;i++)
		for (int j=0;j<cols;j++)
		temp.p[i][j]=p[i][j]-othermatrix.p[i][j];
	return temp;
}
matrix matrix::operator* (const matrix& othermatrix)const
{
    matrix temp;
	if (cols==othermatrix.rows)
    for (int i=0;i<rows;i++)
		for (int j=0;j<cols;j++)
		
		temp.p[i][j]=p[i][j]*othermatrix.p[i][j];
		
	return temp;

}
int main ()
{
	matrix m1;
	m1.getmatrix ();
	matrix m2;
	m2.getmatrix ();
	m1.print ();
	m2.print ();
	matrix m3;
	m3=m1+m2;
	m3.print ();
	m3=m1-m2;
	m3.print();
	m3=m1*m2;
	m3.print();
return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -