Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <ctime>
  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() % 19;
  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 =(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.     int GetLength() //getter для длины строки
  81.     {
  82.         return length;
  83.     }
  84.  
  85.     bool operator >(MyString &obj) //оператор >
  86.     {
  87.         if (strcmp(str, obj.str) > 0)
  88.             return true;
  89.         return false;
  90.     }
  91.  
  92.     bool operator <(MyString &obj) //оператор <
  93.     {
  94.         if (strcmp(str, obj.str) < 0)
  95.             return true;
  96.         return false;
  97.     }
  98.  
  99.     friend ostream& operator <<(ostream& ost, MyString &s); //friend-оператор вывода в поток
  100. };
  101.  
  102.  
  103. ostream& operator <<(ostream& ost, MyString &s)
  104. {
  105.     ost << s.str;
  106.     return ost;
  107. }
  108.  
  109.  
  110. class MyStringArray
  111. {
  112. private:
  113.     MyString *strArr;
  114.     int Length;
  115.  
  116. public:
  117.     MyStringArray(int len) //конструктор с параметром длина строки
  118.     {
  119.         Length = len;
  120.         strArr = new MyString[Length];
  121.     }
  122.  
  123.     ~MyStringArray() //деструктор
  124.     {
  125.         if (strArr)
  126.             delete[] strArr;
  127.     }
  128.  
  129.     MyString& operator [](int i) //оператор индексации []
  130.     {
  131.         return strArr[i];
  132.     }
  133.  
  134.     void SortContent() //сортировка пузырьком по содержимому строк
  135.     {
  136.         MyString tmp;
  137.         for (int i = 0; i < Length - 1; i++)
  138.         {
  139.             for (int j = 0; j < Length - i - 1; j++)
  140.             {
  141.                 if (strArr[j] > strArr[j + 1])
  142.                 {
  143.                     tmp = strArr[j];
  144.                     strArr[j] = strArr[j + 1];
  145.                     strArr[j + 1] = tmp;
  146.                 }
  147.             }
  148.         }
  149.     }
  150.  
  151.  
  152.     void SortLength() //сортировка пузырьком по длине строк
  153.     {
  154.         MyString tmp;
  155.         for (int i = 0; i < Length - 1; i++)
  156.         {
  157.             for (int j = 0; j < Length - i - 1; j++)
  158.             {
  159.                 if (strArr[j].GetLength() > strArr[j + 1].GetLength())
  160.                 {
  161.                     tmp = strArr[j];
  162.                     strArr[j] = strArr[j + 1];
  163.                     strArr[j + 1] = tmp;
  164.                 }
  165.             }
  166.         }
  167.     }
  168.  
  169.     void CheckSortContent() // проверка упорядоченности массива по содержимому строк
  170.     {
  171.         cout << endl << "Check Sort Content: ";
  172.         int error = 0;
  173.         for (int i = 0; i < Length - 1; i++)
  174.             if (strArr[i] > strArr[i + 1]) error++;
  175.         if (error == 0) cout << "The sorting is correct!" << endl;
  176.         else cout << "The sorting is NOT correct." << endl;
  177.     }
  178.  
  179.     void CheckSortLength() //проверка упорядоченности массива по длине
  180.     {
  181.         cout << endl << "Check Sort Length: ";
  182.         int error = 0;
  183.         for (int i = 0; i < Length - 1; i++)
  184.             if (strArr[i].GetLength() > strArr[i + 1].GetLength()) error++;
  185.  
  186.         if (error == 0) cout << "The sorting is correct!" << endl;
  187.         else cout << "The sorting is NOT correct." << endl;
  188.     }
  189.  
  190.     friend ostream& operator << (ostream& ost, MyStringArray &arr);
  191. };
  192.  
  193.  
  194. ostream& operator << (ostream& ost, MyStringArray &arr)
  195. {
  196.     if (arr.Length <= 50)
  197.     {
  198.         for (int i = 0; i < arr.Length; i++)
  199.         {
  200.             ost << arr.strArr[i] << endl;
  201.         }
  202.         return ost;
  203.     }
  204.     else
  205.         cout << endl << "The number of strings more than 50" << endl;
  206.  
  207. }
  208.  
  209.  
  210.  
  211. int main()
  212. {
  213.     srand(time(NULL));
  214.  
  215.     MyStringArray arr(15);
  216.  
  217.     arr.SortContent();
  218.     arr.CheckSortContent();
  219.     cout << arr << endl;
  220.  
  221.     arr.SortLength();
  222.     arr.CheckSortLength();
  223.     cout << arr << endl;
  224.  
  225.     system("Pause");
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement