Advertisement
Guest User

pk3

a guest
Nov 16th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. ###################Array2D.h#####################3
  2.  
  3.  
  4. --------------------------
  5. #pragma once
  6. #include <stdexcept>
  7. namespace collections {
  8. template <typename T>
  9. class Array2D {
  10. protected:
  11. T* array;
  12. int w, k; //1.
  13. public:
  14. Array2D(int w, int k) : array(new T[w*k]), w(w), k(k) {} //2.a
  15. Array2D(int w, int k, const T& value) : w(w), k(k) {
  16. array = new T[w*k];
  17. for (int i = 0; i < w*k; i++) { array[i] = value; }
  18. } //2.b <-- koniec
  19. //konstruktor kopiuj¹cy:
  20. Array2D& operator=(const Array2D& copy) {
  21. if (this != &copy) {
  22. w = copy.w; //przypisanie wartosci do kopii
  23. k = copy.k;
  24. delete[] array; //usuwanie starej
  25. array = new T[w*k]; //tworzenie nowej tablicy
  26. for (int i = 0; i < w*k; ++i) { array[i] = copy.array[i]; } //wypelnienie nowej wartosciami kopii - array1 -> kopia, this -> nowa
  27. return *this; //zwraca now¹ tablice
  28. }
  29. else
  30. return *this;
  31. }
  32. Array2D<T>(const Array2D& copy) : w(w), k(k) { //g³êboka kopia
  33. this->array = new T[w*k];
  34. for (int i = 0; i < copy.w*copy.k; ++i) {
  35. this->array[i] = copy.array[i];
  36. }
  37. }
  38. T* operator[](const int copy) const {
  39. return &array[w*copy];
  40. }
  41. T& at(int rn, int cn) {
  42. if (rn >= w || rn < 0 || cn >= k || cn < 0) {
  43. throw std::runtime_error("poza zakresem");
  44. }
  45. return array[rn*w + cn];
  46. }
  47. ~Array2D() {
  48. delete[] array;
  49. }
  50. };
  51. }
  52. --------------------
  53.  
  54. ####################Matrix.h#####################
  55. #pragma once
  56. #include "Array2D.h"
  57. #include"ITextRepresentable.h"
  58. #include <sstream>
  59. namespace collections {
  60. class Matrix : public Array2D<double>, public ITextRepresentable {
  61. public:
  62. Matrix(int w, int k) : Array2D(w,k) {} //2.a
  63. Matrix(int w, int k, const double value) :Array2D(w,k,value) {}
  64. std::string asText() const override {
  65. std::ostringstream display;
  66. for (int i = 0; i < k; ++i) {
  67. for (int j = 0; j < w; ++j) {
  68. display << array[i*w + j] << " ";
  69. }
  70. display << std::endl;
  71. }
  72. return display.str();
  73. }
  74. };
  75. }
  76.  
  77. ---------------------------------
  78.  
  79. #####################ITextRepresentable#################
  80.  
  81. -------------------------
  82.  
  83. #pragma once
  84. #include <string>
  85. class ITextRepresentable {
  86. public:
  87. virtual std::string asText() const = 0;
  88. virtual ~ITextRepresentable() {
  89.  
  90. }
  91. };
  92. inline std::ostream& operator<<(std::ostream& os, const ITextRepresentable& rep) {
  93. os << rep.asText();
  94. return os;
  95. }
  96.  
  97. --------------------------
  98.  
  99.  
  100. ####################source.cpp#########################
  101.  
  102. --------------------
  103. #include "Array2D.h"
  104. #include "Matrix.h"
  105. #include "ITextRepresentable.h"
  106. #include <iostream>
  107. #include <cstdlib>
  108. int main() {
  109.  
  110. collections::Array2D<int> test(2,4);
  111. collections::Array2D<int> test2(3, 2, 6);
  112. collections::Array2D<int> test3(test);
  113. test3 = test;
  114.  
  115. collections::Matrix test5(2, 3);
  116. collections::Matrix test6(3, 2, 4.5);
  117. test6.at(1,2) = 33;
  118. std::cout << test6;
  119. std::cout << std::endl;
  120.  
  121. //std::cout << test;
  122. //std::cout << std::endl;
  123.  
  124. /*test[1][1]= 1;
  125. test[0][0] = 0;
  126. test[10][30] = 69;
  127. std::cout << test;
  128. std::cout << std::endl;*/
  129.  
  130. system("pause");
  131. return 0;
  132. }-------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement