Advertisement
_Kripaka001_

Labolatory work 3(1)

Mar 18th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. class Line {
  8.     char *line;
  9.     int length = 0;
  10.  
  11.     void checkIndex(int index) {
  12.         if (index < 0 || index > length - 1) {
  13.             throw "Incorrect index: your index: " + to_string(index) + ", line length: " + to_string(length - 1);
  14.         }
  15.     }
  16.  
  17. public:
  18.  
  19.  
  20.     Line() {
  21.         string defaultLineText = "Hello, world!";
  22.         this->line = new char[defaultLineText.length() + 1];
  23.  
  24.         length = (int) defaultLineText.length() + 1;
  25.         strcpy(this->line, defaultLineText.c_str());
  26.         cout << "constructor L";
  27.     }
  28.  
  29.     explicit Line(const string &line) { //avoids implicit calls to constructor
  30.         this->line = new char[line.length() + 1];
  31.  
  32.         length = (int) line.length() + 1;
  33.         strcpy(this->line, line.c_str());
  34.         cout << "constructor L";
  35.     }
  36.  
  37.     explicit Line(const Line &userLine) { // copy constructor
  38.         length = userLine.length;
  39.         delete[]line;
  40.         line = new char[length];
  41.         for (int i = 0; i < length; i++) {
  42.             line[i] = userLine.line[i];
  43.         }
  44.         //userLine->line = line;
  45.         cout << "constructor L";
  46.     }
  47.  
  48.     ~Line() {
  49.         delete[] line;
  50.         cout << "destructor L" << endl;
  51.     }
  52.  
  53.     void printArr(char *arr, int arrLength) {
  54.         for (int i = 0; i < arrLength; i++) {
  55.             cout << arr[i];
  56.         }
  57.         cout << endl;
  58.     }
  59.  
  60.     void clear() {
  61.         length = 1;
  62.         char *newLine = new char[1];
  63.         newLine[0] = '\0';
  64.         delete[]line;
  65.         line = newLine;
  66.     };
  67.  
  68.     void deleteSymbolFromTheEnd() {
  69.         deleteSymbolByIndex(length - 2);
  70.     }
  71.  
  72.     void deleteFromTheBeginning() {
  73.         deleteSymbolByIndex(0);
  74.     }
  75.  
  76.     void deleteSymbolByIndex(int index) {
  77.         checkIndex(index);
  78.         char *newLine = new char[length - 1];
  79.         for (int i = 0; i < index; i++) {
  80.             newLine[i] = line[i];
  81.         }
  82.         for (int i = index + 1; i < length; i++) {
  83.             newLine[i - 1] = line[i];
  84.         }
  85.         length--;
  86.  
  87.         delete[]line;
  88.         line = newLine;
  89.     }
  90.  
  91.     void insertTextFromIndex(int index, const string &userLine) {
  92.         checkIndex(index);
  93.         char *newLine = new char[length + userLine.length()];
  94.  
  95.         for (int i = 0; i < index; i++) {
  96.             newLine[i] = line[i];
  97.         }
  98.  
  99.         for (int i = 0; i < userLine.length(); i++) {
  100.             newLine[i + index] = userLine[i];
  101.         }
  102.  
  103.         for (int i = index; i < length; i++) {
  104.             newLine[i + userLine.length()] = line[i];
  105.         }
  106.  
  107.         delete[] line;
  108.         line = newLine;
  109.         length += userLine.length();
  110.     }
  111.  
  112.     void print() {
  113.         printArr(line, length);
  114.     }
  115.  
  116.     string getLine() {
  117.         return line;
  118.     }
  119.  
  120.     int getSize() {
  121.         return length;
  122.     }
  123.  
  124.     Line operator=(Line &other) {
  125.         char *newLine = new char[other.getLine().size() + 1];
  126.         string str = other.getLine();
  127.         std::copy(str.begin(), str.end(), newLine);
  128.         newLine[other.getLine().size()] = '\0';
  129.         line = newLine;
  130.         newLine = nullptr;
  131.         length = (int) (other.getLine().size()) + 1;
  132.         delete[] newLine;
  133.     }
  134.  
  135. };
  136.  
  137. class Text {
  138.     Line *text;
  139.     int length;
  140.  
  141.     int numberOfSentences = 0;
  142.  
  143. public:
  144.     Text(Line *line) {
  145.         text = line;
  146.         length = line->getSize();
  147.         numberOfSentences++;
  148.         cout << "constructor T";
  149.     }
  150.  
  151.     ~Text() {
  152.         delete text;
  153.         cout << "destructor T";
  154.         //text->~Line();
  155.     }
  156.  
  157.     void add(Line *line) {
  158.         text->insertTextFromIndex(length - 1, " " + line->getLine());
  159.         length += line->getSize();
  160.         numberOfSentences++;
  161.     }
  162.  
  163.     void print() {
  164.         text->print();
  165.     }
  166.  
  167.     int getLength() {
  168.         return length;
  169.     }
  170.  
  171.     string getText() {
  172.         return text->getLine();
  173.     }
  174.  
  175.     int getNumberOfSentence() {
  176.         return numberOfSentences;
  177.     }
  178.  
  179.     Text operator=(Text otherText) {
  180.         text = new Line(otherText.getText());
  181.         length = otherText.getLength();
  182.         numberOfSentences = otherText.getNumberOfSentence();
  183.     }
  184.  
  185.     Text operator+(Text &otherText) {
  186.         int otherTextLength = otherText.getLength();
  187.         text->insertTextFromIndex(length - 1, otherText.getText());
  188.         length += otherText.getLength();
  189.         //cout << "other length: " + to_string(otherText.getLength()) + "  " + to_string(length);
  190.     }
  191.  
  192. };
  193.  
  194. int main() {
  195.     try {
  196.     Line converter("Hello, dudes!");
  197.     converter.print();
  198.     converter.insertTextFromIndex(1, "[new_word]");
  199.     converter.print();
  200.     converter.deleteSymbolByIndex(5);
  201.     converter.print();
  202.     converter.deleteFromTheBeginning();
  203.     converter.print();
  204.     converter.deleteSymbolFromTheEnd();
  205.     converter.print();
  206.  
  207.     Line newLine("new_line");
  208.     converter = newLine;
  209.     converter.print();
  210.  
  211.     Text text(&converter);
  212.     text.print();
  213.     cout << " error " << endl;
  214.     Line *anotherLine = new Line("RANDOM_WORD");
  215.     text.add(anotherLine);
  216.     text.print();
  217.     text = text + text;
  218.     Text newText(new Line("virus_WORD_virus"));
  219.     text = newText;
  220.     text.print();
  221.  
  222.     } catch (const char *return_exception) {
  223.         cout << return_exception;
  224.     }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement