Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <ctime>
  5. #include <cstring>
  6. using namespace std;
  7.  
  8.  
  9. class MyString
  10. {
  11. private:
  12.     char *str;
  13.     int length;
  14.  
  15.     char *GenString(int len) //генерация случайных строк
  16.     {
  17.         str = new char[len + 1];
  18.         for (int i = 0; i < len; i++)
  19.             str[i] = 'a' + rand() % 26;
  20.         str[len] = '\0';
  21.         return str;
  22.     }
  23.  
  24. public:
  25.     MyString() //конструктор без параметров
  26.     {
  27.         length = 1 + rand() % 6;
  28.         GenString(length);
  29.     }
  30.  
  31.     MyString(char *s) //конструктор с параметром ANSI-строка
  32.     {
  33.         length = strlen(s);
  34.         str = new char[length + 1];
  35.         strcpy(str, s);
  36.     }
  37.  
  38.     MyString(int len) //конструктор с параметром длина строки
  39.     {
  40.         length = len;
  41.         if (length <= 20)
  42.             GenString(length);
  43.         else
  44.             cout << "The number of symbols more than 20" << endl;
  45.     }
  46.  
  47.     MyString(MyString &obj) // конструктор копий. В параметре объект класса MyString
  48.     {
  49.         length = obj.length;
  50.         str = new char[obj.length + 1];
  51.         strcpy(str, obj.str);
  52.     }
  53.  
  54.     ~MyString() //деструктор
  55.     {
  56.         if (str)
  57.             delete[] str;
  58.     }
  59.  
  60.     MyString& operator =(const char *s) //оператор присваивания ANSI-строки
  61.     {
  62.         if (str)
  63.             delete[] str;
  64.         length = strlen(s);
  65.         str = new char[length + 1];
  66.         strcpy(str, s);
  67.         return *this; //this это указатель на адрес объекта класса
  68.     }
  69.  
  70.     MyString& operator =(MyString &obj) //оператор присваивания объекта MyString
  71.     {
  72.         if (str)
  73.             delete[] str;
  74.         length = strlen(obj.str);
  75.         str = new char[length + 1];
  76.         strcpy(str, obj.str);
  77.         return *this;
  78.     }
  79.  
  80.     MyString& operator +(MyString &obj) //оператор сложения
  81.     {
  82.         length += obj.length;
  83.         char *s = new char[length+1];
  84.         strcpy(s, str);
  85.         strcat(s, obj.str);
  86.         str = s;
  87.         /*length += obj.length + 1;
  88.         strcat(str, obj.str);*/
  89.         return *this;
  90.     }
  91.  
  92.     int GetLength() //getter для длины строки
  93.     {
  94.         return length;
  95.     }
  96.  
  97.     bool operator >(MyString &obj) //оператор >
  98.     {
  99.         if (strcmp(str, obj.str) > 0)
  100.             return true;
  101.         return false;
  102.     }
  103.  
  104.     bool operator <(MyString &obj) //оператор <
  105.     {
  106.         if (strcmp(str, obj.str) < 0)
  107.             return true;
  108.         return false;
  109.     }
  110.  
  111.     friend ostream& operator <<(ostream& ost, MyString &s); //friend-оператор вывода в поток
  112. };
  113.  
  114.  
  115. ostream& operator <<(ostream& ost, MyString &s)
  116. {
  117.     ost << s.str;
  118.     return ost;
  119. }
  120.  
  121.  
  122. //=======================================================================================================================
  123.  
  124.  
  125. template <class T> class MyVector
  126. {
  127. private:
  128.     T *arr;
  129.     int length;
  130.  
  131. public:
  132.     MyVector()
  133.     {
  134.         arr = NULL;
  135.     }
  136.  
  137.     MyVector(int len)
  138.     {
  139.         length = len;
  140.         arr = new T[length];
  141.     }
  142.  
  143.     T& operator[](int i)
  144.     {
  145.         return arr[i];
  146.     }
  147.  
  148.     friend ostream& operator<<(ostream& stream, MyVector& obj)
  149.     {
  150.         for (int i = 0; i < obj.length; i++)
  151.             stream << obj.arr[i] << "  ";
  152.         return stream;
  153.     }
  154. };
  155.  
  156.  
  157. template <class T> class MyMatrix
  158. {
  159. private:
  160.     MyVector<T> *matrix;
  161.     int rows, columns;
  162.  
  163. public:
  164.     MyMatrix()
  165.     {
  166.         matrix = NULL;
  167.     }
  168.  
  169.     MyMatrix(int r, int c)
  170.     {
  171.         rows = r;
  172.         columns = c;
  173.         matrix = new MyVector<T>[rows];
  174.         for (int i = 0; i < rows; i++)
  175.             matrix[i] = MyVector<T>(columns);
  176.     }
  177.  
  178.     MyVector<T>* operator = (MyMatrix &Matr)
  179.     {
  180.         if (rows != Matr.rows || columns != Matr.columns)
  181.             cout << "Error. Different dimensions." << endl;
  182.         else
  183.         {
  184.             for (int i = 0; i < rows; i++)
  185.                 for (int j = 0; j < columns; j++)
  186.                     matrix[i][j] = Matr.matrix[i][j];
  187.         }
  188.         return matrix;
  189.     }
  190.  
  191.     MyMatrix <T> operator+ (MyMatrix <T> &right)
  192.     {
  193.         MyMatrix <T> M(rows, columns);
  194.         if (columns == right.columns && rows == right.rows)
  195.         {          
  196.             for (int i = 0; i < rows; i++)
  197.                 for (int j = 0; j < columns; j++)
  198.                     M[i][j] = matrix[i][j] + right[i][j];
  199.         }
  200.         else cout << "Error. Different dimensions." << endl;
  201.         return M;
  202.     }
  203.  
  204.     MyVector<T>& operator [](int index)
  205.     {
  206.         while (index >= rows || index < 0)
  207.         {
  208.             cout << "Wrong index. " << "The index must be between 0 and " << rows - 1 << endl << "Please retry: ";
  209.             cin >> index; cout << endl;
  210.         }
  211.         return matrix[index];
  212.     }
  213.  
  214.     friend ostream& operator<<(ostream& str, const MyMatrix& matr)
  215.     {
  216.         for (int i = 0; i < matr.rows; i++)
  217.         {
  218.             for (int j = 0; j < matr.columns; j++)
  219.                 str << matr.matrix[i][j] << "  ";
  220.             str << endl << endl;
  221.         }
  222.         return str;
  223.     }
  224. };
  225.  
  226.  
  227. int main()
  228. {
  229.     srand(time(NULL));
  230.  
  231.     // MyVector TEST
  232.  
  233.     /*MyVector<MyString> a(5), b(5);
  234.     cout << a << endl << b << endl;
  235.     a[4] = "HELLO!";
  236.     cout << endl << a << endl;
  237.  
  238.     MyString a1; MyString a2; MyString a3;
  239.     cout << a1 << endl;
  240.     cout << a2 << endl;
  241.     a3 = a1 + a2; cout << a3 << endl;*/
  242.  
  243.  
  244.     // MyMatrix TEST
  245.  
  246.     MyMatrix<MyString> m1(4,3), m2(4,3), m3(2,2);
  247.     MyVector<MyString> a(3);
  248.  
  249.     //cout << m2 << endl;
  250.     //cout << a << endl << endl << endl;
  251.     //m2[4] = a;
  252.     //cout << m2 << endl;
  253.     //a[2] = "HELLO!";
  254.     //cout << m2 << endl;
  255.  
  256.     cout << m1 << endl;
  257.     cout << m2 << endl;
  258.     cout << m1 + m2 << endl;
  259.     m1 = m2;
  260.     cout << m1 << endl;
  261.  
  262.     system("Pause");
  263.     return 0;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement