Advertisement
Guest User

Untitled

a guest
May 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstring>
  4. #include <stdexcept>
  5. #include <new>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. template <typename T>
  10. std::string PretvoriUString(T x){
  11. std::ostringstream s;
  12. s << x;
  13. return s.str();
  14. }
  15.  
  16. template<typename TipEl>
  17. class Matrica {
  18. int br_redova, br_kolona;
  19. TipEl** elementi;
  20. char ime_matrice;
  21. static TipEl **AlocirajMemoriju(int br_redova, int br_kolona);
  22. static void DealocirajMemoriju(TipEl **elementi, int br_redova);
  23. void KopirajElemente(TipEl **elementi);
  24. void TestIndeksa(int red) const{
  25. if(red < 0 || red>br_redova)
  26. throw std::range_error("Pogresan indeks!");
  27. }
  28. public:
  29. Matrica(int br_redova, int br_kolona, char ime= 0);
  30. Matrica(const Matrica &m);
  31. Matrica(Matrica &&m);
  32. ~Matrica() { DealocirajMemoriju(elementi, br_redova); }
  33. Matrica &operator=(const Matrica &m);
  34. Matrica &operator=(Matrica &&m);
  35. TipEl *operator [](int red) const{
  36. TestIndeksa(red);
  37. return elementi[red];
  38. }
  39. TipEl &operator ()(int red, int kolona){
  40. if(red < 1 || kolona < 1 || red > br_redova || kolona > br_kolona) throw std::range_error("Neispravan indeks");
  41. return elementi[red-1][kolona-1];
  42. }
  43.  
  44. operator std::string() const;
  45.  
  46. friend std::ostream &operator<<(std::ostream &tok, const Matrica &m){
  47. int duzina_ispisa(std::cout.width());
  48. for(int i = 0; i < m.br_redova; i++) {
  49. for(int j = 0; j < m.br_kolona; j++)
  50. tok << std::setw(duzina_ispisa) << m.elementi[i][j];
  51. tok << std::endl;
  52. }
  53. return tok;
  54. }
  55.  
  56. template<typename Tip2>
  57. friend Matrica<Tip2> operator *(const Matrica<Tip2> &m1, const Matrica<Tip2> &m2);
  58. template<typename Tip2>
  59. friend Matrica<Tip2> operator +(const Matrica<Tip2> &m1, const Matrica<Tip2> &m2);
  60. template<typename Tip2>
  61. friend Matrica<Tip2> operator -(const Matrica<Tip2> &m1, const Matrica<Tip2> &m2);
  62. template<typename Tip2>
  63. friend Matrica<Tip2> &operator *=(Matrica<Tip2> &m1, const Matrica<Tip2> &m2);
  64. template<typename Tip2>
  65. friend Matrica<Tip2> &operator +=(Matrica<Tip2> &m1, const Matrica<Tip2> &m2);
  66. template<typename Tip2>
  67. friend Matrica<Tip2> &operator -=(Matrica<Tip2> &m1, const Matrica<Tip2> &m2);
  68. };
  69.  
  70. template <typename TipEl>
  71. Matrica<TipEl> :: operator std::string() const{
  72. std::string s, pomocni;
  73. s += "{";
  74. for(int i=0; i<br_redova; i++){
  75. s += "{";
  76. for(int j=0; j<br_kolona; j++){
  77. pomocni = PretvoriUString(elementi[i][j]);
  78. s += pomocni;
  79. if(j != br_kolona-1) s += ",";
  80. }
  81. if(i != br_redova-1) s += "},";
  82. else s += "}";
  83. }
  84. s += "}";
  85. return s;
  86. }
  87.  
  88. template <typename TipEl>
  89. TipEl **Matrica<TipEl>::AlocirajMemoriju(int br_redova, int br_kolona) {
  90. TipEl **elementi(new TipEl*[br_redova]{});
  91. try{
  92. for(int i = 0; i < br_redova; i++) elementi[i] = new TipEl[br_kolona];
  93. }catch(...) {
  94. DealocirajMemoriju(elementi, br_redova);
  95. throw;
  96. }
  97. return elementi;
  98. }
  99.  
  100. template <typename TipEl>
  101. void Matrica<TipEl>::DealocirajMemoriju(TipEl **elementi, int br_redova) {
  102. for(int i = 0; i < br_redova; i++) delete[] elementi[i];
  103. delete[] elementi;
  104. }
  105.  
  106. template<typename TipEl>
  107. Matrica<TipEl>::Matrica(int br_redova, int br_kolona, char ime) : br_redova(br_redova), br_kolona(br_kolona), elementi(AlocirajMemoriju(br_redova, br_kolona)), ime_matrice(ime) {}
  108.  
  109. template<typename TipEl>
  110. void Matrica<TipEl>::KopirajElemente(TipEl **elementi) {
  111. for(int i = 0; i < br_redova; i++)
  112. for(int j = 0; j < br_kolona; j++)
  113. Matrica::elementi[i][j] = elementi[i][j];
  114. }
  115.  
  116. template<typename TipEl>
  117. Matrica<TipEl>::Matrica(const Matrica<TipEl> &m) : br_redova(m.br_redova), br_kolona(m.br_kolona), elementi(AlocirajMemoriju(m.br_redova, m.br_kolona)), ime_matrice(m.ime_matrice){
  118. KopirajElemente(m.elementi);
  119. }
  120.  
  121. template< typename TipEl>
  122. Matrica<TipEl>::Matrica(Matrica<TipEl> &&m) : br_redova(m.br_redova), br_kolona(m.br_kolona), elementi(m.elementi), ime_matrice(m.ime_matrice) {
  123. m.br_redova = 0; m.elementi = nullptr;
  124. }
  125.  
  126. template <typename TipEl>
  127. Matrica<TipEl> &Matrica<TipEl>::operator =(const Matrica<TipEl> &m) {
  128. if(br_redova < m.br_redova || br_kolona < m.br_kolona) {
  129. TipEl **novi_prostor(AlocirajMemoriju(m.br_redova, m.br_kolona));
  130. DealocirajMemoriju(elementi, br_redova);
  131. elementi = novi_prostor;
  132. }
  133. else if(br_redova > m.br_redova)
  134. for(int i = m.br_redova; i < br_redova; i++) delete elementi[i];
  135. br_redova = m.br_redova; br_kolona = m.br_kolona;
  136. ime_matrice = m.ime_matrice;
  137. KopirajElemente(m.elementi);
  138. return *this;
  139. }
  140. template <typename TipEl>
  141. Matrica<TipEl> &Matrica<TipEl>::operator =(Matrica<TipEl> &&m) {
  142. std::swap(br_redova, m.br_redova); std::swap(br_kolona, m.br_kolona);
  143. std::swap(ime_matrice, m.ime_matrice); std::swap(elementi, m.elementi);
  144. return *this;
  145. }
  146.  
  147. template <typename TipEl>
  148. Matrica<TipEl> operator +(const Matrica<TipEl> &m1,const Matrica<TipEl> &m2) {
  149. if(m1.br_redova != m2.br_redova || m1.br_kolona != m2.br_kolona)
  150. throw std::domain_error("Matrice nemaju jednake dimenzije!");
  151. Matrica<TipEl> m3(m1.br_redova, m1.br_kolona);
  152. for(int i = 0; i < m1.br_redova; i++)
  153. for(int j = 0; j < m1.br_kolona; j++)
  154. m3.elementi[i][j] = m1.elementi[i][j] + m2.elementi[i][j];
  155. return m3;
  156. }
  157.  
  158. template <typename TipEl>
  159. Matrica<TipEl> &operator +=(Matrica<TipEl> &m1,const Matrica<TipEl> &m2) {
  160. if(m1.br_redova != m2.br_redova || m1.br_kolona != m2.br_kolona)
  161. throw std::domain_error("Matrice nemaju jednake dimenzije!");
  162. return m1 = m1+m2;
  163. }
  164.  
  165. template <typename TipEl>
  166. Matrica<TipEl> operator -(const Matrica<TipEl> &m1,const Matrica<TipEl> &m2) {
  167. if(m1.br_redova != m2.br_redova || m1.br_kolona != m2.br_kolona)
  168. throw std::domain_error("Matrice nemaju jednake dimenzije!");
  169. Matrica<TipEl> m3(m1.br_redova, m1.br_kolona);
  170. for(int i = 0; i < m1.br_redova; i++)
  171. for(int j = 0; j < m1.br_kolona; j++)
  172. m3.elementi[i][j] = m1.elementi[i][j] - m2.elementi[i][j];
  173. return m3;
  174. }
  175.  
  176. template <typename TipEl>
  177. Matrica<TipEl> &operator -=(Matrica<TipEl> &m1,const Matrica<TipEl> &m2) {
  178. if(m1.br_redova != m2.br_redova || m1.br_kolona != m2.br_kolona)
  179. throw std::domain_error("Matrice nemaju jednake dimenzije!");
  180. return m1 = m1-m2;
  181. }
  182.  
  183. template <typename TipEl>
  184. Matrica<TipEl> operator *(const Matrica<TipEl> &m1,const Matrica<TipEl> &m2) {
  185. if(m1.br_kolona != m2.br_redova)
  186. throw std::domain_error("Matrice nisu saglasne za mnozenje");
  187. Matrica<TipEl> m3(m1.br_redova, m2.br_kolona);
  188. for(int i=0; i<m3.br_redova; i++){
  189. for(int j=0; j<m3.br_kolona; j++){
  190. m3[i][j] = 0;
  191. }
  192. }
  193. TipEl proizvod(1);
  194. int i(0), j(0), k(0);
  195. for(i = 0; i < m1.br_redova; i++){
  196. for( j=0; j < m2.br_kolona; j++){
  197. proizvod = 0;
  198. for( k=0; k<m1.br_kolona; k++){
  199. proizvod += (m1[i][k] * m2[k][j]);
  200. }
  201. m3[i][j] = proizvod;
  202. }
  203. }
  204. return m3;
  205. }
  206.  
  207. template <typename TipEl>
  208. Matrica<TipEl> &operator *=(Matrica<TipEl> &m1,const Matrica<TipEl> &m2) {
  209. if(m1.br_kolona != m2.br_redova)
  210. throw std::domain_error("Matrice nisu saglasne za mnozenje");
  211. return m1 = m1*m2;
  212. }
  213.  
  214.  
  215. int main ()
  216. { try {
  217. Matrica<int> a(2, 3, 'a');
  218. a(0,0) = 1; a(0,1) = 2; a(0,2) = 3;
  219. a(1,0) = 4; a(1,1) = 5; a(1,2) = 7;
  220. Matrica<int> b(2, 3, 'b');
  221. b(0,0) = 1; b(0,1) = 2; b(0,2) = 3;
  222. b(1,0) = 4; b(1,1) = 5; b(1,2) = 6;
  223. }
  224. catch(std::bad_alloc) {
  225. std::cout << "Nema dovoljno memorije!\n";
  226. }
  227. catch(std::domain_error e) {
  228. std::cout << e.what();
  229. }
  230. catch(std::range_error e) {
  231. std::cout << e.what();
  232. }
  233. /*
  234. Matrica<double> a(m, n, 'A'), b(m, n, 'B');
  235. std::cout << "Unesi matricu A:\n";
  236. std::cin >> a;
  237. std::cout << "Unesi matricu B:\n";
  238. std::cin >> b;
  239. std::cout << "Zbir ove dvije matrice je:\n";
  240. std::cout << std::setw(7) << a + b;
  241. */
  242. return 0;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement