Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #ifndef MATRIX_H
  2. #define MATRIX_H
  3. class matrix{
  4. public:
  5.     matrix(size_t rows, size_t cols);
  6.     matrix(const matrix& other);
  7.     ~matrix();
  8.     void addAt(size_t i, size_t j, int num){
  9.         arr[i][j] = num;
  10.     }
  11.     matrix& operator*(const matrix& rhs);
  12.     void print() const{
  13.         for (size_t i = 0; i < rows; i++){
  14.             for (size_t j = 0; j < cols; j++){
  15.                 std::cout << arr[i][j] << ", ";
  16.             }
  17.             std::cout << std::endl;
  18.         }
  19.     }
  20.     int **arr;
  21. private:
  22.     void delMatrix();
  23.     void copyMatrix(const matrix& other);
  24.     size_t rows;
  25.     size_t cols;
  26. };
  27. matrix::matrix(size_t rows, size_t cols) : rows(rows), cols(cols){
  28.     arr = new int*[rows];
  29.     for (size_t i = 0; i < rows; i++){
  30.         arr[i] = new int[cols];
  31.     }
  32.     for (size_t i = 0; i < rows; i++){
  33.         for (size_t j = 0; j < cols; j++){
  34.             arr[i][j] = 0;
  35.         }
  36.     }
  37. }
  38. matrix::matrix(const matrix& other){
  39.     copyMatrix(other);
  40. }
  41. matrix::~matrix(){
  42.     delMatrix();
  43. }
  44. matrix& matrix::operator=(const matrix& other){
  45.     if (this != &other){
  46.         delMatrix();
  47.         copyMatrix(other);
  48.     }
  49.     return *this;
  50. }
  51. /*matrix& matrix::operator*=(const matrix& rhs){
  52.     matrix temp = rhs;
  53.     return temp;
  54. }*/
  55. matrix& matrix::operator*(const matrix& rhs){
  56.     matrix temp(rows, rhs.cols);
  57.     for (size_t i = 0; i < temp.rows; i++){
  58.         for (size_t j = 0; j < temp.cols; j++){
  59.             for (size_t k = 0; k < temp.rows; k++){
  60.                 temp.arr[i][j] += arr[i][k] * rhs.arr[k][j];
  61.             }
  62.         }
  63.     }
  64.     return temp;
  65. }
  66. void matrix::delMatrix(){
  67.     for (size_t i = 0; i < rows; i++){
  68.         delete[] arr[i];
  69.     }
  70.     delete arr;
  71. }
  72. void matrix::copyMatrix(const matrix& other){
  73.     rows = other.rows;
  74.     cols = other.cols;
  75.     arr = new int*[rows];
  76.     for (size_t i = 0; i < rows; i++){
  77.         arr[i] = new int[cols];
  78.     }
  79.     for (size_t i = 0; i < rows; i++){
  80.         for (size_t j = 0; j < cols; j++){
  81.             arr[i][j] = other.arr[i][j];
  82.         }
  83.     }
  84. }
  85. #endif
  86.  
  87. #include <iostream>
  88. #include "matrix.h"
  89. int main() {
  90.     matrix *A, *B;
  91.     A = new matrix(3, 3);
  92.     B = new matrix(3, 2);
  93.     A->addAt(0, 0, 1);
  94.     A->addAt(0, 1, 2);
  95.     A->addAt(0, 2, 3);
  96.     A->addAt(1, 0, 4);
  97.     A->addAt(1, 1, 5);
  98.     A->addAt(1, 2, 6);
  99.     A->addAt(2, 0, 7);
  100.     A->addAt(2, 1, 8);
  101.     A->addAt(2, 2, 9);
  102.     A->print();
  103.     std::cout << std::endl;
  104.     B->addAt(0, 0, 1);
  105.     B->addAt(0, 1, 2);
  106.     B->addAt(1, 0, 3);
  107.     B->addAt(1, 1, 4);
  108.     B->addAt(2, 0, 5);
  109.     B->addAt(2, 1, 6);
  110.     B->print();
  111.     std::cout << std::endl;
  112.     std::cout << (*A**B).arr[0][1];
  113.     std::cout << std::endl;
  114.     system("pause");
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement