Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <cstdio>
- #include <ctime>
- #include <cstring>
- using namespace std;
- class MyString
- {
- private:
- char *str;
- int length;
- char *GenString(int len) //генерация случайных строк
- {
- str = new char[len + 1];
- for (int i = 0; i < len; i++)
- str[i] = 'a' + rand() % 26;
- str[len] = '\0';
- return str;
- }
- public:
- MyString() //конструктор без параметров
- {
- length = 1 + rand() % 6;
- GenString(length);
- }
- MyString(char *s) //конструктор с параметром ANSI-строка
- {
- length = strlen(s);
- str = new char[length + 1];
- strcpy(str, s);
- }
- MyString(int len) //конструктор с параметром длина строки
- {
- length = len;
- if (length <= 20)
- GenString(length);
- else
- cout << "The number of symbols more than 20" << endl;
- }
- MyString(MyString &obj) // конструктор копий. В параметре объект класса MyString
- {
- length = obj.length;
- str = new char[obj.length + 1];
- strcpy(str, obj.str);
- }
- ~MyString() //деструктор
- {
- if (str)
- delete[] str;
- }
- MyString& operator =(const char *s) //оператор присваивания ANSI-строки
- {
- if (str)
- delete[] str;
- length = strlen(s);
- str = new char[length + 1];
- strcpy(str, s);
- return *this; //this это указатель на адрес объекта класса
- }
- MyString& operator =(MyString &obj) //оператор присваивания объекта MyString
- {
- if (str)
- delete[] str;
- length = strlen(obj.str);
- str = new char[length + 1];
- strcpy(str, obj.str);
- return *this;
- }
- MyString& operator +(MyString &obj) //оператор сложения
- {
- length += obj.length;
- char *s = new char[length+1];
- strcpy(s, str);
- strcat(s, obj.str);
- str = s;
- /*length += obj.length + 1;
- strcat(str, obj.str);*/
- return *this;
- }
- int GetLength() //getter для длины строки
- {
- return length;
- }
- bool operator >(MyString &obj) //оператор >
- {
- if (strcmp(str, obj.str) > 0)
- return true;
- return false;
- }
- bool operator <(MyString &obj) //оператор <
- {
- if (strcmp(str, obj.str) < 0)
- return true;
- return false;
- }
- friend ostream& operator <<(ostream& ost, MyString &s); //friend-оператор вывода в поток
- };
- ostream& operator <<(ostream& ost, MyString &s)
- {
- ost << s.str;
- return ost;
- }
- //=======================================================================================================================
- template <class T> class MyVector
- {
- private:
- T *arr;
- int length;
- public:
- MyVector()
- {
- arr = NULL;
- }
- MyVector(int len)
- {
- length = len;
- arr = new T[length];
- }
- T& operator[](int i)
- {
- return arr[i];
- }
- friend ostream& operator<<(ostream& stream, MyVector& obj)
- {
- for (int i = 0; i < obj.length; i++)
- stream << obj.arr[i] << " ";
- return stream;
- }
- };
- template <class T> class MyMatrix
- {
- private:
- MyVector<T> *matrix;
- int rows, columns;
- public:
- MyMatrix()
- {
- matrix = NULL;
- }
- MyMatrix(int r, int c)
- {
- rows = r;
- columns = c;
- matrix = new MyVector<T>[rows];
- for (int i = 0; i < rows; i++)
- matrix[i] = MyVector<T>(columns);
- }
- MyVector<T>* operator = (MyMatrix &Matr)
- {
- if (rows != Matr.rows || columns != Matr.columns)
- cout << "Error. Different dimensions." << endl;
- else
- {
- for (int i = 0; i < rows; i++)
- for (int j = 0; j < columns; j++)
- matrix[i][j] = Matr.matrix[i][j];
- }
- return matrix;
- }
- MyMatrix <T> operator+ (MyMatrix <T> &right)
- {
- MyMatrix <T> M(rows, columns);
- if (columns == right.columns && rows == right.rows)
- {
- for (int i = 0; i < rows; i++)
- for (int j = 0; j < columns; j++)
- M[i][j] = matrix[i][j] + right[i][j];
- }
- else cout << "Error. Different dimensions." << endl;
- return M;
- }
- MyVector<T>& operator [](int index)
- {
- while (index >= rows || index < 0)
- {
- cout << "Wrong index. " << "The index must be between 0 and " << rows - 1 << endl << "Please retry: ";
- cin >> index; cout << endl;
- }
- return matrix[index];
- }
- friend ostream& operator<<(ostream& str, const MyMatrix& matr)
- {
- for (int i = 0; i < matr.rows; i++)
- {
- for (int j = 0; j < matr.columns; j++)
- str << matr.matrix[i][j] << " ";
- str << endl << endl;
- }
- return str;
- }
- };
- int main()
- {
- srand(time(NULL));
- // MyVector TEST
- /*MyVector<MyString> a(5), b(5);
- cout << a << endl << b << endl;
- a[4] = "HELLO!";
- cout << endl << a << endl;
- MyString a1; MyString a2; MyString a3;
- cout << a1 << endl;
- cout << a2 << endl;
- a3 = a1 + a2; cout << a3 << endl;*/
- // MyMatrix TEST
- MyMatrix<MyString> m1(4,3), m2(4,3), m3(2,2);
- MyVector<MyString> a(3);
- //cout << m2 << endl;
- //cout << a << endl << endl << endl;
- //m2[4] = a;
- //cout << m2 << endl;
- //a[2] = "HELLO!";
- //cout << m2 << endl;
- cout << m1 << endl;
- cout << m2 << endl;
- cout << m1 + m2 << endl;
- m1 = m2;
- cout << m1 << endl;
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement