학교 과제

c++로 작성한 간단한 행렬 클래스

꾸준함. 2017. 12. 26. 15:00

자료구조 프로그래밍 과목을 배우면서 c++로 작성한 간단한 행렬 클래스입니다.(2가지 버전)


2*2 행렬 클래스

matrixa.h

#ifndef MATRIX_H

#define MATRIX_H

 

#include <iostream>

using namespace std;

 

class Matrix

{

private:

        int m[2][2];

public:

        Matrix(int a = 0, int b = 0, int c = 0, int d = 0);

        ~Matrix()

        {

        }

        void ShowMatrix();

        Matrix Transpose();

        Matrix operator+(const Matrix &a);

        Matrix operator-(const Matrix &a);

        Matrix operator*(const Matrix &a);

        void operator=(const Matrix &a);

};

 

#endif


matrixa.cpp

#include "matrixa.h"

#include <iomanip> //setw 위해

 

#define MAX 2

 

Matrix::Matrix(int a, int b, int c, int d)

{

        //초기화

        m[0][0] = a;

        m[0][1] = b;

        m[1][0] = c;

        m[1][1] = d;

}

 

Matrix Matrix::Transpose()

{

        Matrix copy; //새로운 Matrix 생성

        //행렬 복사

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       copy.m[i][j] = m[i][j];

        //전치

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       m[j][i] = copy.m[i][j];

        return *this;

}

 

Matrix Matrix::operator+(const Matrix &a)

{

        Matrix b; //새로운 Matrix 생성

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       b.m[i][j] = m[i][j] + a.m[i][j]; //덧셈

        return b;

}

 

Matrix Matrix::operator-(const Matrix &a)

{

        Matrix b; //새로운 Matrix 생성

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       b.m[i][j] = m[i][j] - a.m[i][j]; //뺄셈

        return b;

}

 

Matrix Matrix::operator*(const Matrix &a)

{

        Matrix b; //새로운 Matrix 생성

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       for (int k = 0; k < MAX; k++)

                              b.m[i][k] += (m[i][j] * a.m[j][i]); //곱셈

        return b;

}

 

void Matrix::operator=(const Matrix &a)

{

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       m[i][j] = a.m[i][j]; //복사한다

}

 

void Matrix::ShowMatrix()

{

        for (int i = 0; i < MAX; i++)

        {

               for (int j = 0; j < MAX; j++)

                       cout << setw(4) << m[i][j]; //일정한 간격

               cout << endl;

        }

}


hw3a.cpp

#include "matrixa.h"

#include <iostream>

using namespace std;

 

int main(void)

{

        Matrix matrix1(1, 2, 3, 4);

        Matrix matrix2(1, 1, 1, 1);

        Matrix matrix3;

        cout << "----------------" << endl;

        cout << "Matrix Transpose" << endl;

        cout << "----------------" << endl;

        matrix1.Transpose();

        matrix1.ShowMatrix();

        matrix1.Transpose();

        cout << "Matrix Add" << endl;

        cout << "----------------" << endl;

        matrix3 = matrix1 + matrix2;

        matrix3.ShowMatrix();

        cout << "----------------" << endl;

        cout << "Matrix Sub" << endl;

        cout << "----------------" << endl;

        matrix3 = matrix1 - matrix2;

        matrix3.ShowMatrix();

        cout << "----------------" << endl;

        cout << "Matrix Multi" << endl;

        cout << "----------------" << endl;

        matrix3 = matrix1*matrix2;

        matrix3.ShowMatrix();

        cout << "----------------" << endl;

        return 0;

}

 



3*3 행렬 클래스

matrixb.h

#ifndef MATRIX_H

#define MATRIX_H

 

#include <iostream>

using namespace std;

 

class Matrix

{

private:

        int m[3][3];

public:

        Matrix(int a = 0, int b = 0, int c = 0, int d = 0, int e=0, int f=0, int g=0, int h=0, int i=0);

        ~Matrix()

        {

        }

        void ShowMatrix();

        Matrix Transpose();

        Matrix operator+(const Matrix &a);

        Matrix operator-(const Matrix &a);

        Matrix operator*(const Matrix &a);

        void operator=(const Matrix &a);

};

 

#endif


matrixb.cpp

#include "matrixb.h"

#include <iomanip> //setw 위해

 

#define MAX 3

 

Matrix::Matrix(int a, int b, int c, int d, int e, int f, int g, int h, int i)

{

        //초기화

        m[0][0] = a;

        m[0][1] = b;

        m[0][2] = c;

        m[1][0] = d;

        m[1][1] = e;

        m[1][2] = f;

        m[2][0] = g;

        m[2][1] = h;

        m[2][2] = i;

}

 

Matrix Matrix::Transpose()

{

        Matrix copy; //새로운 Matrix 생성

         //행렬 복사

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       copy.m[i][j] = m[i][j];

        //전치

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       m[j][i] = copy.m[i][j];

        return *this;

}

 

Matrix Matrix::operator+(const Matrix &a)

{

        Matrix b; //새로운 Matrix 생성

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       b.m[i][j] = m[i][j] + a.m[i][j]; //덧셈

        return b;

}

 

Matrix Matrix::operator-(const Matrix &a)

{

        Matrix b; //새로운 Matrix 생성

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       b.m[i][j] = m[i][j] - a.m[i][j]; //뺄셈

        return b;

}

 

Matrix Matrix::operator*(const Matrix &a)

{

        Matrix b; //새로운 Matrix 생성

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       for (int k = 0; k < MAX; k++)

                              b.m[i][k] += (m[i][j] * a.m[j][i]); //곱셈

        return b;

}

 

void Matrix::operator=(const Matrix &a)

{

        for (int i = 0; i < MAX; i++)

               for (int j = 0; j < MAX; j++)

                       m[i][j] = a.m[i][j]; //복사한다

}

 

void Matrix::ShowMatrix()

{

        for (int i = 0; i < MAX; i++)

        {

               for (int j = 0; j < MAX; j++)

                       cout << setw(4) << m[i][j]; //일정한 간격

               cout << endl;

        }

}


hw3b.cpp

#include "matrixb.h"

#include <iostream>

using namespace std;

 

int main(void)

{

        Matrix matrix1(1, 2, 3, 4, 5, 6, 7, 8, 9);

        Matrix matrix2(1, 1, 1, 1, 1, 1, 1, 1, 1);

        Matrix matrix3;

        cout << "----------------" << endl;

        cout << "Matrix Transpose" << endl;

        cout << "----------------" << endl;

        matrix1.Transpose();

        matrix1.ShowMatrix();

        matrix1.Transpose();

        cout << "Matrix Add" << endl;

        cout << "----------------" << endl;

        matrix3 = matrix1 + matrix2;

        matrix3.ShowMatrix();

        cout << "----------------" << endl;

        cout << "Matrix Sub" << endl;

        cout << "----------------" << endl;

        matrix3 = matrix1 - matrix2;

        matrix3.ShowMatrix();

        cout << "----------------" << endl;

        cout << "Matrix Multi" << endl;

        cout << "----------------" << endl;

        matrix3 = matrix1*matrix2;

        matrix3.ShowMatrix();

        cout << "----------------" << endl;

        return 0;

}

 



개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


반응형