언어/c++

c++ 행렬

파아랑새 2016. 5. 21. 21:08

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

#define _ROW_ 2
#define _COL_ 2
#define MAT int

using namespace std;
class MATRIX {
public:
 MAT A_rray[_ROW_][_COL_];
 MAT B_rray[_ROW_][_COL_];
 MAT A_plus_B[_ROW_][_COL_];
 MAT A_sub_B[_ROW_][_COL_];
 MAT A_mul_B[_ROW_][_COL_];
public:
 MATRIX();
 void dataInput();
 void addition();
 void subtraction();
 void multiplication();
};

int main(void) {
 MATRIX* M = NULL;
 M = new MATRIX;
 M->dataInput();
 M->addition();
 M->subtraction();
 M->multiplication();
 return 0;
}
MATRIX::MATRIX() {

 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   this->A_rray[i][j] = 0;
   this->B_rray[i][j] = 0;
   this->A_plus_B[i][j] = 0; // _'+'_
   this->A_sub_B[i][j] = 0; // _'-'_
   this->A_mul_B[i][j] = 0; // _'X'_
  }
 }
 cout << " ================================================== " << endl;
 // [ A_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_rray[" << i << "]" << "[" << j << "] = "
    << setw(2) << this->A_rray[i][j] << "   ";
  }
  cout << endl;
 }
 cout << endl;
 // [ B_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " B_rray[" << i << "]" << "[" << j << "] = "
    << setw(2) << this->B_rray[i][j] << "   ";
  }
  cout << endl;
 }
 cout << endl;
 // [ A+B_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_plus_B[" << i << "]" << "[" << j << "] = "
    << setw(2) << this->A_plus_B[i][j] << "   ";
  }
  cout << endl;
 }
 cout << endl;
 // [ A-B_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_sub_B[" << i << "]" << "[" << j << "] = "
    << setw(2) << this->A_sub_B[i][j] << "   ";
  }
  cout << endl;
 }
 cout << endl;
 // [ AXB_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_mul_B[" << i << "]" << "[" << j << "] = "
    << setw(2) << this->A_mul_B[i][j] << "   ";
  }
  cout << endl;
 }
 cout << " ================================================== " << endl;
} // END OF MATRIX()

void MATRIX::dataInput() {

 // [ A_RRAY ] DATA SETTING
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_rray[" << i << "]" << "[" << j << "] = ";
   cin >> this->A_rray[i][j];
  }
 }
 // [ A_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_rray[" << i << "]" << "[" << j << "] = "
    << this->A_rray[i][j] << "   ";
  }
  cout << endl;
 }
 cout << endl;

 // [ B_RRAY ] DATA SETTING
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " B_rray[" << i << "]" << "[" << j << "] = ";
   cin >> this->B_rray[i][j];
  }
 }
 // [ B_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " B_rray[" << i << "]" << "[" << j << "] = "
    << this->B_rray[i][j] << "   ";
  }
  cout << endl;
 }
}// END OF dataInput()

void MATRIX::addition() {
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   this->A_plus_B[i][j] = this->A_rray[i][j] + this->B_rray[i][j];
  }
 }
 cout << " ==================[ Addition ]==================== " << endl;
 // [ A+B_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_plus_B[" << i << "]" << "[" << j << "] = "
    << setw(2) << this->A_plus_B[i][j] << "   ";
  }
  cout << endl;
 }
 cout << " ================================================== " << endl;
}
void MATRIX::subtraction() {
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   this->A_plus_B[i][j] = this->A_rray[i][j] - this->B_rray[i][j];
  }
 }
 cout << " ================[ Subtraction ]================== " << endl;
 // [ A+B_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_sub_B[" << i << "]" << "[" << j << "] = "
    << setw(2) << this->A_sub_B[i][j] << "   ";
  }
  cout << endl;
 }
 cout << " ================================================== " << endl;
}
void MATRIX::multiplication() {
 int sum = 0;
 for (int k = 0; k < 2; k++){
  for (int i = 0; i < _ROW_; i++) {
   for (int j = 0; j < _COL_; j++) {
    sum += this->A_rray[k][j] * this->B_rray[j][i];
   }
   this->A_mul_B[k][i] = sum;
   sum = 0;
  }
 }
 cout << " ===============[ Multiplication ================= " << endl;
 // [ A+B_RRAY ] test printf
 for (int i = 0; i < _ROW_; i++) {
  for (int j = 0; j < _COL_; j++) {
   cout << " A_sub_B[" << i << "]" << "[" << j << "] = "
    << setw(2) << this->A_mul_B[i][j] << "   ";
  }
  cout << endl;
 }
 cout << " ================================================== " << endl;
}