Guest User

Untitled

a guest
Dec 13th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. //
  2. //  MyString.cpp
  3. //  lab1
  4. //
  5. //  Created by Alex on 10/2/12.
  6. //  Copyright (c) 2012 Alexey Oshevnev. All rights reserved.
  7. //
  8.  
  9. #include "MyString.h"
  10. #include <string.h>
  11. #include <iostream>
  12.  
  13. using namespace std;
  14.  
  15. MyString::MyString() {
  16.     str = NULL;
  17. }
  18.  
  19.  
  20. MyString::~MyString() {
  21.     delete[] str;
  22. }
  23.  
  24. MyString::MyString (const MyString &obj) {
  25.     str = new char[strlen(obj.str) + 1];
  26.     strcpy(str, obj.str);
  27. }
  28.  
  29. void MyString :: input() {
  30.     char *tmp = new char[1];
  31.     cout << endl << "input: ";
  32.     fflush(stdin);
  33.     gets(tmp);
  34.     str = new char[strlen(tmp) + 1];
  35.     strcpy(str, tmp);
  36. }
  37.  
  38. void MyString :: output() {
  39.     cout << endl << "output: " << str <<endl;
  40. }
  41.  
  42. MyString MyString::operator+(MyString obj) {
  43.     MyString tmp;
  44.     tmp.str= new char[strlen(this->str)+strlen(obj.str) + 1];
  45.     strcpy(tmp.str,this->str);
  46.     strcat(tmp.str,obj.str);
  47.    
  48.     return tmp;
  49. }
  50.  
  51. void MyString::operator+=(MyString obj) {
  52.     strcat(this->str, obj.str);
  53. }
  54.  
  55. MyString MyString::operator=(MyString obj) {
  56.     str = new char[strlen(obj.str) + 1];
  57.     strcpy(str, obj.str);
  58.    
  59.     return *this;
  60. }
  61.  
  62. bool MyString::operator==(MyString obj) {
  63.     return !(strcmp(this->str, obj.str));
  64. }
  65.  
  66. bool MyString::operator!=(MyString obj) {
  67.     return (strcmp(this->str, obj.str));
  68. }
  69.  
  70. bool MyString::operator<(MyString obj) {
  71.     if (strcmp(this->str, obj.str) >= 0)
  72.         return false;
  73.     else
  74.         return true;
  75. }
  76.  
  77. bool MyString::operator>(MyString obj) {
  78.     if (strcmp(this->str, obj.str) >= 0)
  79.         return true;
  80.     else
  81.         return false;
  82. }
  83.  
  84. char MyString::operator[](int counter)
  85. {
  86.     if (strlen(this->str) < counter || counter < 0)
  87.         return '?';
  88.     else
  89.         return this->str[counter];
  90. }
  91.  
  92. char* MyString::operator()(int begin, int end) {
  93.     if (begin > end || strlen(this->str) < end) {
  94.         char *error = new char[1]; error = "?";
  95.         return error;
  96.     }
  97.     else {
  98.         int count = 0;
  99.         char *tmp = new char[end - begin];
  100.         for (int i = begin; i < end; i++) {
  101.             tmp[count] = this->str[i];
  102.             count++;
  103.         }
  104.         return tmp;
  105.     }
  106. }
Add Comment
Please, Sign In to add comment