fork download
  1. // tester.cpp : Defines the entry point for the console application.
  2. //
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <algorithm>
  6.  
  7. typedef double tipoelem;
  8.  
  9. class matrice {
  10. public:
  11. matrice(int, int, tipoelem inizializzatore = 0); /* costruttore */
  12. matrice(const matrice& rhs);
  13. ~matrice(void);
  14. tipoelem leggiMatrice(int, int);
  15. void scriviMatrice(int, int, tipoelem);
  16. void prodottoScalare(tipoelem);
  17. matrice matriceTrasposta(void);
  18. matrice matriceProdotto(matrice& M);
  19. matrice& operator=(const matrice&);
  20. void rand(void);
  21. void stampa(void);
  22. private:
  23. int righe;
  24. int colonne;
  25. tipoelem **elementi;
  26. };
  27.  
  28. using namespace std;
  29.  
  30. matrice::matrice(int r, int c, tipoelem inizializzatore) {
  31. this->colonne = c;
  32. this->righe = r;
  33.  
  34. // allocazione dinamica della matrice
  35. elementi = new tipoelem*[righe];
  36. for (auto i = 0; i != righe; i++)
  37. this->elementi[i] = new tipoelem[colonne];
  38.  
  39. // inizializzazione degli elementi
  40. for (auto i = 0; i != righe; i++)
  41. for (auto j = 0; j != colonne; j++)
  42. this->elementi[i][j] = inizializzatore;
  43. }
  44.  
  45. matrice::matrice(const matrice& rhs) : colonne(rhs.colonne), righe(rhs.righe), elementi(new tipoelem*[rhs.righe])
  46. {
  47. for (auto i = 0; i != righe; i++)
  48. this->elementi[i] = new tipoelem[colonne];
  49.  
  50. for (auto i = 0; i != righe; i++)
  51. for (auto j = 0; j != colonne; j++)
  52. this->elementi[i][j] = rhs.elementi[i][j];
  53. }
  54.  
  55. matrice::~matrice(void) {
  56. for (auto j = 0; j < this->righe; ++j) {
  57. delete[] this->elementi[j];
  58. }
  59. delete[] this->elementi;
  60. }
  61.  
  62. tipoelem matrice::leggiMatrice(int i, int j) {
  63. return elementi[i][j];
  64. }
  65.  
  66. void matrice::scriviMatrice(int i, int j, tipoelem scrittura) {
  67. elementi[i][j] = scrittura;
  68. return;
  69. }
  70.  
  71. void matrice::prodottoScalare(tipoelem scalare) {
  72. for (auto i = 0; i < righe; i++)
  73. for (auto j = 0; j < colonne; j++)
  74. elementi[i][j] = elementi[i][j] * scalare;
  75. return;
  76. }
  77.  
  78. matrice matrice::matriceTrasposta(void) {
  79. matrice trasposta(colonne, righe);
  80.  
  81. for (auto i = 0; i < righe; i++)
  82. for (auto j = 0; j < colonne; j++)
  83. trasposta.scriviMatrice(j, i, leggiMatrice(i, j));
  84. return trasposta;
  85. }
  86.  
  87. matrice matrice::matriceProdotto(matrice& M) {
  88. matrice prodotto(righe, colonne);
  89.  
  90. for (auto i = 0; i < righe; i++)
  91. for (auto j = 0; j < righe; j++)
  92. prodotto.scriviMatrice(i, j, (matrice::leggiMatrice(i, j)*M.leggiMatrice(i, j)));
  93. return prodotto;
  94. }
  95.  
  96. matrice& matrice::operator=(const matrice &m)
  97. {
  98. matrice temp(m);
  99. std::swap(temp.colonne, colonne);
  100. std::swap(temp.righe, righe);
  101. std::swap(temp.elementi, elementi);
  102. return *this;
  103. }
  104.  
  105. void matrice::rand(void) {
  106. for (auto i = 0; i < righe; i++)
  107. for (auto j = 0; j < colonne; j++)
  108. matrice::scriviMatrice(i, j, 100.0);
  109. return;
  110. }
  111.  
  112. void matrice::stampa(void) {
  113. for (auto i = 0; i < righe; i++) {
  114. for (auto j = 0; j < colonne; j++)
  115. std::cout << elementi[i][j] << " ";
  116. std::cout << std::endl;
  117. }
  118. }
  119.  
  120. int main(void) {
  121.  
  122. matrice A(3, 2), T(2, 3);
  123. A.rand();
  124. std::cout << "Stampa A" << std::endl;
  125. A.stampa();
  126. std::cout << "Stampa Trasposta T" << std::endl;
  127. T = A.matriceTrasposta();
  128. T.stampa();
  129.  
  130. std::cout << std::endl;
  131.  
  132. std::cout << "Stampa B" << std::endl;
  133. matrice B(4, 4);
  134. B.stampa();
  135. std::cout << "Stampa copia t in b" << std::endl;
  136. B = T;
  137. B.stampa();
  138.  
  139. return (0);
  140. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
Stampa A
100 100 
100 100 
100 100 
Stampa Trasposta T
100 100 100 
100 100 100 

Stampa B
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 
Stampa copia t in b
100 100 100 
100 100 100