-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinaryMatrix.cpp
100 lines (76 loc) · 2.45 KB
/
BinaryMatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
-------------------------------------------------------------------------------
Laboratoire : Labo1-POO2
Fichier : BinaryMatrix.cpp
Auteur(s) : Théo Gallandat & Pierre-Samuel Rochat
Date : 16/03/2017
But : Implemation of the BinaryMatrix class
Remarque(s) : R.A.S.
Compilateur : MinGW-g++ 4.8.1
-------------------------------------------------------------------------------
*/
#include <iostream>
#include <stdlib.h>
#include "BinaryMatrix.hpp"
#define RANDOM bool(int((1 + rand() / (RAND_MAX + 1.0)*2) - 1))
using namespace std;
BinaryMatrix::BinaryMatrix(int size): size(size) {
matrix = new bool*[size];
for(int i = 0; i < size; i++){
matrix[i] = new bool[size];
for(int j = 0; j < size; ++j){
matrix[i][j] = RANDOM;
}
}
}
BinaryMatrix::BinaryMatrix(const BinaryMatrix& m) {
matrix = m.matrix;
for(int i = 0; i < size; i++){
matrix[i] = m.matrix[i];
}
}
BinaryMatrix::~BinaryMatrix(){
for(int i = 0; i < size; i++){
delete matrix[i];
}
delete matrix;
}
void BinaryMatrix::display()const {
for(int i = 0; i < size; ++i){
for(int j = 0; j < size; ++j){
cout << matrix[i][j];
if (j > 0 or j < size)
cout << " ";
}
cout << endl;
}
cout << endl;
}
void BinaryMatrix::opOnMatrix(const BinaryMatrix& m, const Operation& op) {
applyOperation(*this, m, op);
}
BinaryMatrix BinaryMatrix::opReturnVal(const BinaryMatrix& m,
const Operation& op) {
BinaryMatrix resultMatrix(size);
applyOperation(resultMatrix, m, op);
// The resultMatrix is returned by value (copy of the object) and not by
// reference because when leaving the method the original resultMatrix no
// longer exists and by consequence the returned reference would have refered
// to nothing.
return resultMatrix;
}
BinaryMatrix* BinaryMatrix::opReturnPtr(const BinaryMatrix& m,
const Operation& op) {
BinaryMatrix* resultMatrix = new BinaryMatrix(size);
applyOperation(*resultMatrix, m, op);
return resultMatrix;
}
void BinaryMatrix::applyOperation(BinaryMatrix& resultMatrix,
const BinaryMatrix& m, const Operation& op) {
for(int i = 0; i < size; ++i){
for(int j = 0; j < size; ++j){
(resultMatrix.matrix)[i][j] =
op.applyOperator(matrix[i][j], (m.matrix)[i][j]);
}
}
}