画像処理C ++ Foundation03クラス

導入コンセプト:

大きい。改訂された変数(ローカル変数とスコープ、関数パラメーターと実際のパラメーター、値とアドレスで渡される)、プログラミングスタイル、void *タイプ、変数の命名。プログラム編成(関数/ファイル/プロジェクト);デフォルトパラメータ

選択の概要:クラス(OOP)、クラスのカプセル化、new / delete演算子、構築、破棄の
演習:
クラスを使用して、最初のレッスンMatrixのタスクを繰り返します。ファイル:main()。セット2:matrix.h、matrix.cpp

請求:

行列の加算、行列の乗算。破壊時にスペースを解放することをお勧めします

(参照:C ++ Advanced Programmingバージョン2または3、第1章を参照している限り、他のバージョンを読まないでください!)


#include
   
    
using namespace std;

class Matrix
{
public:
	void Matrixnew();		/*创建矩阵*/
	void Matrixdel();		/*删除矩阵空间*/
	friend Matrix operator+(Matrix &, Matrix &);		/*矩阵加法*/
	friend Matrix operator*(Matrix &, Matrix &);		/*矩阵乘法*/
	void input();		/*输入矩阵元素*/
	void display();		/*输出矩阵元素*/
	int rows = 0;		/*矩阵的行、列数*/
	int columns = 0;

private:
	int **mat = NULL;	/*二维动态矩阵*/
};
#include
    
     
using namespace std;

class Matrix
{
public:
	void Matrixnew();		/*创建矩阵*/
	void Matrixdel();		/*删除矩阵空间*/
	friend Matrix operator+(Matrix &, Matrix &);		/*矩阵加法*/
	friend Matrix operator*(Matrix &, Matrix &);		/*矩阵乘法*/
	void input();		/*输入矩阵元素*/
	void display();		/*输出矩阵元素*/
	int rows = 0;		/*矩阵的行、列数*/
	int columns = 0;

private:
	int **mat = NULL;	/*二维动态矩阵*/
};

void Matrix::Matrixnew()
{
	mat = new int *[rows];
	for (int i = 0; i < rows; i++)
		mat[i] = new int[columns];

	for (int i = 0; i < rows; i++)
		for (int j = 0; j < columns; j++)
			mat[i][j] = 0;
}

void Matrix::Matrixdel()
{
	for (int i = 0; i < rows; i++)
		delete mat[i];
	delete mat;
}

Matrix operator+(Matrix &a, Matrix &b)
{
	Matrix c;
	
	c.rows = a.rows;
	c.columns = b.columns;
	c.Matrixnew();
	for (int i = 0; i < a.rows; i++)
		for (int j = 0; j < a.columns; j++)
			c.mat[i][j] = a.mat[i][j] + b.mat[i][j];

	return c;
}

Matrix operator*(Matrix &a, Matrix &b)
{
	Matrix c;

	c.rows = a.rows;
	c.columns = b.columns;
	c.Matrixnew();
	for (int i = 0; i < a.rows; i++)
		for (int j = 0; j < b.columns; j++)
			for (int k = 0; k < a.columns; k++)
				c.mat[i][j] += a.mat[i][k] * b.mat[k][j];

	return c;
}

void Matrix::input()
{
	cout << "Please enter the elements of the matrix:" << endl;
	for (int i = 0; i < rows; i++)
		for (int j = 0; j < columns; j++)
			cin >> mat[i][j];
}

void Matrix::display()
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
			cout << mat[i][j] << '\t';
		cout << endl;
	}
	cout << endl;
}

int main(void)
{
	Matrix a, b, c;
	int i, j, k, flag = 0;

	cout << "Please enter the rows and the columns of the first matrix:";
	cin >> i >> j;
	cout << "Please enter the columns of the second matrix:";
	cin >> k;
	cout << "Which result do you want to get?" << endl;
	cout << "1.a+b=" << endl << "2.a*b" << endl;
	cin >> flag;

	a.rows = i;
	a.columns = j;
	b.rows = j;
	b.columns = k;
	c.rows = i;
	c.columns = k;

	a.Matrixnew();
	b.Matrixnew();
	c.Matrixnew();

	a.input();
	b.input();

	if (flag == 1)
	{
		c = a + b;
		cout << "The result of Matrix a add Matrix b is:" << endl;
		c.display();
	}
	else if (flag == 2)
	{
		c = a*b;
		cout << "The result of Matrix a multiply Matrix b is:" << endl;
		c.display();
	}

	a.Matrixdel();
	b.Matrixdel();
	c.Matrixdel();

	system("pause");
	return 0;
}
void Matrix::Matrixnew()
{
	mat = new int *[rows];
	for (int i = 0; i < rows; i++)
		mat[i] = new int[columns];

	for (int i = 0; i < rows; i++)
		for (int j = 0; j < columns; j++)
			mat[i][j] = 0;
}

void Matrix::Matrixdel()
{
	for (int i = 0; i < rows; i++)
		delete mat[i];
	delete mat;
}

Matrix operator+(Matrix &a, Matrix &b)
{
	Matrix c;
	
	c.rows = a.rows;
	c.columns = b.columns;
	c.Matrixnew();
	for (int i = 0; i < a.rows; i++)
		for (int j = 0; j < a.columns; j++)
			c.mat[i][j] = a.mat[i][j] + b.mat[i][j];

	return c;
}

Matrix operator*(Matrix &a, Matrix &b)
{
	Matrix c;

	c.rows = a.rows;
	c.columns = b.columns;
	c.Matrixnew();
	for (int i = 0; i < a.rows; i++)
		for (int j = 0; j < b.columns; j++)
			for (int k = 0; k < a.columns; k++)
				c.mat[i][j] += a.mat[i][k] * b.mat[k][j];

	return c;
}

void Matrix::input()
{
	cout << "Please enter the elements of the matrix:" << endl;
	for (int i = 0; i < rows; i++)
		for (int j = 0; j < columns; j++)
			cin >> mat[i][j];
}

void Matrix::display()
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
			cout << mat[i][j] << '\t';
		cout << endl;
	}
	cout << endl;
}

int main(void)
{
	Matrix a, b, c;
	int i, j, k, flag = 0;

	cout << "Please enter the rows and the columns of the first matrix:";
	cin >> i >> j;
	cout << "Please enter the columns of the second matrix:";
	cin >> k;
	cout << "Which result do you want to get?" << endl;
	cout << "1.a+b=" << endl << "2.a*b" << endl;
	cin >> flag;

	a.rows = i;
	a.columns = j;
	b.rows = j;
	b.columns = k;
	c.rows = i;
	c.columns = k;

	a.Matrixnew();
	b.Matrixnew();
	c.Matrixnew();

	a.input();
	b.input();

	if (flag == 1)
	{
		c = a + b;
		cout << "The result of Matrix a add Matrix b is:" << endl;
		c.display();
	}
	else if (flag == 2)
	{
		c = a*b;
		cout << "The result of Matrix a multiply Matrix b is:" << endl;
		c.display();
	}

	a.Matrixdel();
	b.Matrixdel();
	c.Matrixdel();

	system("pause");
	return 0;
}
    
   

おすすめ

転載: blog.csdn.net/qq_26751117/article/details/53447579
おすすめ