Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include<string>
- using namespace std;
- class MyString
- {
- private:
- char* mem;
- int count;
- int size;
- public:
- MyString(string str = "") {
- if (str.length() > 128)
- {
- size = str.length();
- }
- else {
- size = 128;
- }
- size = str.length();
- count = str.length();
- mem = new char[size];
- for (int i = 0; i < count; i++) {
- mem[i] = str[i];
- }
- };
- MyString(char* src, int k) {
- if (k > 128)
- {
- size = k;
- }
- else {
- size = 128;
- }
- mem = new char[size];
- count = k;
- for (int i = 0; i < count; i++) {
- mem[i] = src[i];
- }
- };
- ~MyString() { delete[]mem; };
- MyString(const MyString& tmp) {
- count = tmp.count;
- size = tmp.size;
- mem = new char[size];
- for (int i = 0; i < count; i++) {
- mem[i] = tmp.mem[i];
- }
- };
- MyString& operator=(MyString tmp) {
- if (size != tmp.size) {
- if (size != 0) {
- delete[]mem;
- }
- size = tmp.size;
- mem = new char[size];
- }
- count = tmp.count;
- for (int i = 0; i < count; i++) {
- mem[i] = tmp.mem[i];
- }
- return *this;
- };
- int GetCount() {
- return this->count;
- }
- char& operator[](int k) {
- if (k >= 0 && k < count) {
- return mem[k];
- }
- char a = NULL;
- return a;
- }
- friend ostream& operator<<(ostream& out, MyString tmp)
- {
- int count;
- for (int i = 0; i < count; i++)
- {
- out << tmp.mem[i];
- return out;
- }
- }
- friend istream& operator>>(istream& in, MyString& tmp)
- {
- char* r;
- r = new char[512];
- tmp.count = 0;
- do
- {
- in >> r[tmp.count];
- if (tmp.count != 13)
- {
- tmp.count++;
- }
- } while (tmp.count != 13);
- tmp = MyString(r, tmp.count);
- return in;
- }
- int operator == (MyString tmp)
- {
- int result = 0; //сравнимые строки одинаковы
- if (count != tmp.count)
- {
- int result = -1;
- }
- else
- {
- for(int i = 0; i < count && result == 0; i++)
- {
- if (mem[i] != tmp.mem[i])
- {
- result = -1;
- }
- }
- }
- return result;
- }
- int operator > (MyString tmp)
- {
- int result = 0;
- for (int i = 0; i < count && i < tmp.count; i++)
- {
- if (mem[i] > tmp.mem[i])
- {
- result = -1;
- }
- if (count > tmp.count)
- {
- result = -1;
- }
- }
- return result;
- }
- int operator < (MyString tmp)
- {
- int result = 0;
- for (int i = 0; i < count && i < tmp.count; i++)
- {
- if (mem[i] < tmp.mem[i])
- {
- result = -1;
- }
- if (count < tmp.count)
- {
- result = -1;
- }
- }
- return result;
- }
- MyString(int num)
- {
- size = 128;
- count = 0;
- char tmp;
- mem = new char[size];
- /*char nums[] = {'0','1','2','3','4','5','6','7','8','9'};*/
- while (num > 0)
- {
- mem[count++] = num / 10 + 48;
- num /= 10;
- }
- for (int i = 0; i < count || 2; i++)
- {
- tmp = mem[i];
- mem[i] = mem[count - 1 - i];
- mem[count - 1 - i] = tmp;
- }
- }
- };
Add Comment
Please, Sign In to add comment