Advertisement
Trawka011

Untitled

Mar 1st, 2023
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 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.     Fraction operator- (Fraction other) {
  17.         return Fraction(x * other.y - y * other.x, y * other.y);
  18.     }
  19.     Fraction operator* (Fraction other) {
  20.         return Fraction(x * other.x, y * other.y);
  21.     }
  22.     Fraction operator/ (Fraction other) {
  23.         return Fraction(x * other.y, y * other.x);
  24.     }
  25.     Fraction operator+ (long int z) {
  26.         return Fraction(x + y * z, y);
  27.     }
  28.     Fraction operator- () {
  29.         return Fraction(x = -x, y);
  30.     }
  31.     bool operator== (Fraction other) {
  32.         if (x * other.y == other.x * y)
  33.         {
  34.             return true;
  35.         }
  36.         else
  37.         {
  38.             return false;
  39.         }
  40.     }
  41.     bool operator< (Fraction other) {
  42.         if (x * other.y < other.x * y)
  43.         {
  44.             return true;
  45.         }
  46.         else
  47.         {
  48.             return false;
  49.         }
  50.     }
  51.     bool operator> (Fraction other) {
  52.         if (x * other.y > other.x * y)
  53.         {
  54.             return true;
  55.         }
  56.         else
  57.         {
  58.             return false;
  59.         }
  60.     }
  61.     friend istream& operator>>(istream& in, Fraction& a);
  62.     friend ostream& operator<<(ostream& on, Fraction& a);
  63. };
  64.  
  65. template <class T>
  66. class Array {
  67. private:
  68.     int len;
  69.     T* data;
  70. public:
  71.  
  72.     Array(int len = 0) {
  73.         this->len = len;
  74.  
  75.         data = new T[len];
  76.     }
  77.     Array(const Array& other) {
  78.         len = other.len;
  79.         data = new T[len];
  80.         for (int i = 0; i < len; i++) {
  81.             data[i] = other.data[i];
  82.         }
  83.     }
  84.     ~Array() { delete[] data; };
  85.     T& operator[](int index) {
  86.         index %= len;
  87.         if (index < 0)
  88.         {
  89.             index = len + index;
  90.         }
  91.         return data[index];
  92.     }
  93.     T operator[](int index) const {
  94.         index %= len;
  95.         if (index < 0)
  96.         {
  97.             index = len + index;
  98.         }
  99.         return data[index];
  100.     }
  101.     Array& operator=(const Array& other) {
  102.         delete[] data;
  103.         len = other.len;
  104.         data = new T[len];
  105.         for (int i = 0; i < len; i++) {
  106.             data[i] = other.data[i];
  107.         }
  108.         return *this;
  109.     }
  110.     Array  operator() (int  l, int r) {
  111.         Array b(r - l - 1);
  112.         cout << r - l - 1 << endl;
  113.         for (int i = 0; i < r - l - 1; i++)
  114.         {
  115.             b[i] = data[i + l + 1];
  116.         }
  117.         return b;
  118.     }
  119.     int size() {
  120.         return len;
  121.     }
  122.     T& array_max() {
  123.         int max = 0;
  124.         for (int i = 1; i < len; i++)
  125.         {
  126.             if (data[max] < data[i] )
  127.             {
  128.                 max = i;
  129.             }
  130.         }
  131.         return data[max];
  132.     }
  133. };
  134.  
  135. istream& operator>>(istream& in, Fraction& a) {
  136.     char tmp;
  137.     in >> a.x >> tmp >> a.y;
  138.     return in;
  139. }
  140.  
  141. ostream& operator<<(ostream& on, Fraction& a) {
  142.     on << a.x << "/" << a.y;
  143.     return on;
  144. }
  145.  
  146. template <class T>
  147. istream& operator>>(istream& in, Array <T>& a) {
  148.     for (int i = 0; i < a.size(); i++) {
  149.         in >> a[i];
  150.     }
  151.     return in;
  152. }
  153.  
  154. template <class T>
  155. ostream& operator<<(ostream& on, Array<T>& a) {
  156.     for (int i = 0; i < a.size(); i++) {
  157.         on << a[i] << ' ';
  158.     }
  159.     on << endl;
  160.     return on;
  161. }
  162.  
  163. int main() {
  164.     int cntint, cntchar, cntFraction;
  165.     cin >> cntint;
  166.     Array<int> a(cntint);
  167.     cin >> a;
  168.     cin >> cntchar;
  169.     Array <char> b(cntchar);
  170.     cin >> b;
  171.     cin >> cntFraction;
  172.     Array<Fraction> c(cntFraction);
  173.     cin >> c;
  174.     cout << a.array_max() << endl;
  175.     cout << b.array_max() << endl;
  176.     cout << c.array_max() << endl;
  177.     return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement