Xom9ik

Lab_11/3 var (IIl semester)

Dec 6th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. //Lab_11.cpp
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7. template <typename T>
  8. class Vector
  9. {
  10. public:
  11.     Vector(int s, T k = 0)
  12.     {
  13.         size = s;
  14.         data = new T[size];
  15.         for (int i = 0; i<size; i++)
  16.             data[i] = k;
  17.     }
  18.     //конструктор копирования
  19.     Vector(const Vector<T>&a)
  20.     {
  21.         size = a.size;
  22.         data = new int[size];
  23.         for (int i = 0; i<size; i++)
  24.             data[i] = a.data[i];
  25.     }
  26.     ~Vector()
  27.     {
  28.         delete[]data;
  29.         data = 0;
  30.     }
  31.     //оператор присваивания
  32.     Vector<T> &operator=(const Vector<T> &a)
  33.     {
  34.         if (this == &a)
  35.             return *this;
  36.         size = a.size;
  37.         if (data != 0)
  38.             delete[]data;
  39.         data = new int[size];
  40.         for (int i = 0; i<size; i++)
  41.             data[i] = a.data[i];
  42.         return *this;
  43.     }
  44.     //операция доступа по индексу
  45.     T &operator[](int index)
  46.     {
  47.         if (index < size)
  48.             return data[index];
  49.         else
  50.             cout << "Error" << endl;
  51.     }
  52.     //операция для сложения векторов
  53.     Vector<T> operator+(const Vector<T> &k)
  54.     {
  55.         Vector<T> temp(size);
  56.         for (int i = 0; i < size; i++)
  57.             temp.data[i] = data[i] + k.data[i];
  58.         return temp;
  59.     }
  60.     //операция для добавления константы
  61.     Vector<T> operator+(int value)
  62.     {
  63.         Vector<T> temp(size);
  64.         for (int i = 0; i < size; i++)
  65.             temp.data[i] = data[i] + value;
  66.         return temp;
  67.     }
  68.     //перегруженные операции ввода-вывода
  69.     template <typename J>
  70.     friend ostream& operator<<(ostream& out, Vector<J> &a)
  71.     {
  72.         for (int i = 0; i<a.size; ++i)
  73.             out << a.data[i] << " ";
  74.         return out;
  75.     }
  76.     template <typename J>
  77.     friend istream& operator >> (istream& in, Vector<J> &a)
  78.     {
  79.         for (int i = 0; i<a.size; ++i)
  80.             in >> a.data[i];
  81.         return in;
  82.     }
  83.  
  84. private:
  85.     int size;
  86.     T *data;
  87. };
  88. template <class T1, class T2>
  89. class Time
  90. {
  91. public:
  92.     Time() {};
  93.     Time(T1 f, T2 s)
  94.     {
  95.         Minutes = f;
  96.         Seconds = s;
  97.     }
  98.     T1 Minutes;
  99.     T2 Seconds;
  100.     ~Time() { Minutes = 0; Seconds = 0; };
  101.     //перегруженные операции ввода-вывода
  102.     template <class J1, class J2>
  103.     friend ostream& operator << (ostream& out, Time<J1, J2> &a)
  104.     {
  105.         out << a.Minutes << ":" << a.Seconds;
  106.         return out;
  107.     }
  108.     template <class J1, class J2>
  109.     friend istream& operator >> (istream& in, Time<J1, J2> &a)
  110.     {
  111.         cout << "Минуты:";
  112.         in >> a.Minutes;
  113.         cout << "Секунды:";
  114.         in >> a.Seconds;
  115.         return in;
  116.     }
  117. };
  118. int main()
  119. {
  120.     setlocale(LC_ALL, "rus");
  121.     srand(time(NULL));
  122.     int size;
  123.     cout << "Введите размерность вектора A и B: ";
  124.     cin >> size;
  125.  
  126.     Vector<int> A(size);
  127.     cout << "Введите " << size << " чисел вектора A\n";
  128.     cin >> A;
  129.     cout << "Введенный вектор A: " << A << endl;
  130.     int value;
  131.     cout << "---Добавление к вектору А константы---" << endl;
  132.     cout << "Введите число:";
  133.     cin >> value;
  134.     A = A+value;
  135.     cout << "Вектор A после добавления константы: " << A << endl;
  136.  
  137.     Vector<int> B(size);
  138.     cout << "Введите " << size << " чисел вектора B\n";
  139.     cin >> B;
  140.     cout << "---Вектор B после добавления к нему вектора А---" << endl;
  141.     B = B + A;
  142.     cout << B << endl;
  143.  
  144.     cout << "---Ввод чисел для Time(int, int)---" << endl;
  145.     Time<int, int> MyTime;
  146.     cin >> MyTime;
  147.     cout << MyTime;
  148.     cout << endl;
  149.     system("pause");
  150. }
Advertisement
Add Comment
Please, Sign In to add comment