Trawka011

Untitled

Mar 1st, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. class Fraction {
  6. private:
  7. long int x, y;
  8. public:
  9. Fraction(long int x = 0, long int y = 1) {
  10. this->x = x;
  11. this->y = y;
  12. }
  13. Fraction operator+ (Fraction other) {
  14. return Fraction(x * other.y + y * other.x, y * other.y);
  15. }
  16. friend istream& operator>>(istream& in, Fraction& a);
  17. friend ostream& operator<<(ostream& on, Fraction& a);
  18. };
  19.  
  20. template <class T>
  21. class Array {
  22. private:
  23. int len;
  24. T* data;
  25. public:
  26.  
  27. Array(int len = 0) {
  28. this->len = len;
  29.  
  30. data = new T[len];
  31. }
  32. Array(const Array& other) {
  33. len = other.len;
  34. data = new T[len];
  35. for (int i = 0; i < len; i++) {
  36. data[i] = other.data[i];
  37. }
  38. }
  39. ~Array() { delete[] data; };
  40. T& operator[](int index) {
  41. index %= len;
  42. if (index < 0)
  43. {
  44. index = len + index;
  45. }
  46. return data[index];
  47. }
  48. T operator[](int index) const {
  49. index %= len;
  50. if (index < 0)
  51. {
  52. index = len + index;
  53. }
  54. return data[index];
  55. }
  56. Array& operator=(const Array& other) {
  57. delete[] data;
  58. len = other.len;
  59. data = new T[len];
  60. for (int i = 0; i < len; i++) {
  61. data[i] = other.data[i];
  62. }
  63. return *this;
  64. }
  65. Array operator() (int l, int r) {
  66. Array b(r - l - 1);
  67. cout << r - l - 1 << endl;
  68. for (int i = 0; i < r - l - 1; i++)
  69. {
  70. b[i] = data[i + l + 1];
  71. }
  72. return b;
  73. }
  74. int size() {
  75. return len;
  76. }
  77. };
  78.  
  79. istream& operator>>(istream& in, Fraction& a) {
  80. char tmp;
  81. in >> a.x >> tmp >> a.y;
  82. return in;
  83. }
  84.  
  85. ostream& operator<<(ostream& on, Fraction& a) {
  86. on << a.x << "/" << a.y;
  87. return on;
  88. }
  89.  
  90. template <class T>
  91. istream& operator>>(istream& in, Array <T>& a) {
  92. for (int i = 0; i < a.size(); i++) {
  93. in >> a[i];
  94. }
  95. return in;
  96. }
  97. template <class T>
  98. ostream& operator<<(ostream& on, Array<T>& a) {
  99. for (int i = a.size() - 1; i >= 0; i--) {
  100. on << a[i] << ' ';
  101. }
  102. on << endl;
  103. return on;
  104. }
  105.  
  106. int main() {
  107. int cntint, cntchar, cntFraction;
  108. cin >> cntint;
  109. Array<int> a(cntint);
  110. cin >> a;
  111. cin >> cntchar;
  112. Array <char> z(cntchar);
  113. cin >> z;
  114. cin >> cntFraction;
  115. Array<Fraction> c(cntFraction);
  116. cin >> c;
  117. cout << cntint << endl << a;
  118. cout << cntchar << endl << z;
  119. cout << cntFraction << endl << c;
  120. return 0;
  121. }
Add Comment
Please, Sign In to add comment