Guest User

Untitled

a guest
Jun 18th, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. /*
  7. * Class of relation.
  8. * Main feature is to
  9. * check if it is reflexive.
  10. * (Relation is reflexive
  11. * if main diagonal of
  12. * matrix contains only 1)
  13. */
  14. class Relation
  15. {
  16. private:
  17. int **matrix; // Matrix of particular relation
  18. int size; // Size of relation matrix (number of elements in relation)
  19.  
  20. public:
  21. // Main constructor
  22. Relation(int size);
  23.  
  24. // Method to enter matrix of relation
  25. void input();
  26.  
  27. // Getter to check if relation is reflexive using its matrix
  28. bool isReflexive();
  29.  
  30. // Relation printing overloading
  31. friend ostream &operator<<(ostream &out, Relation relation);
  32.  
  33. ~Relation();
  34. };
  35.  
  36. Relation::Relation(int size)
  37. {
  38. this->size = size;
  39.  
  40. this->matrix = new int *[size];
  41. for (size_t i = 0; i < size; i++)
  42. {
  43. matrix[i] = new int[size];
  44.  
  45. for (size_t j = 0; j < size; j++)
  46. {
  47. matrix[i][j] = 0;
  48. }
  49. }
  50.  
  51. cout << *this << endl;
  52. this->input();
  53. }
  54.  
  55. void Relation::input()
  56. {
  57. cout << "Enter relation matrix:" << endl;
  58. for (size_t i = 0; i < this->size; i++)
  59. {
  60. for (size_t j = 0; j < this->size; j++)
  61. {
  62. int number;
  63. cin >> number;
  64.  
  65. // Entered number validation
  66. if (number != 0 && number != 1)
  67. {
  68. cout << "Wrong number was entered: " << number << ", replaced by 1." << endl;
  69. number = 1;
  70. }
  71.  
  72. this->matrix[i][j] = number;
  73. }
  74. }
  75. }
  76.  
  77. bool Relation::isReflexive()
  78. {
  79. for (size_t i = 0; i < this->size; i++)
  80. {
  81. for (size_t j = 0; j < this->size; j++)
  82. {
  83. if (i == j)
  84. {
  85. if (this->matrix[i][j] != 1)
  86. {
  87. return false;
  88. }
  89. }
  90. }
  91. }
  92.  
  93. return true;
  94. }
  95.  
  96. ostream &operator<<(ostream &out, Relation relation)
  97. {
  98. out << "Matrix of relation:" << endl;
  99. for (size_t i = 0; i < relation.size; i++)
  100. {
  101. for (size_t j = 0; j < relation.size; j++)
  102. {
  103. out << setw(4) << relation.matrix[i][j];
  104. }
  105.  
  106. if (i != relation.size - 1)
  107. {
  108. out << endl;
  109. }
  110. }
  111.  
  112. return out;
  113. }
  114.  
  115. Relation::~Relation()
  116. {
  117. for (size_t i = 0; i < this->size; i++)
  118. {
  119. delete[] this->matrix[i];
  120. }
  121. delete[] this->matrix;
  122. }
  123.  
  124. int main()
  125. {
  126. int size;
  127. cout << "Enter relation size:" << endl;
  128. cin >> size;
  129. // Validating relation size
  130. if (size <= 0)
  131. {
  132. cout << "Wrong size." << endl;
  133. return 0;
  134. }
  135.  
  136. cout << endl;
  137.  
  138. Relation *relation = new Relation(size);
  139.  
  140. cout << endl;
  141.  
  142. cout << relation << endl;
  143. cout << "The relation is " << (relation->isReflexive() ? "" : "not ")
  144. << "reflexive." << endl;
  145.  
  146. delete relation;
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment