Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Line {
- char *line;
- int length = 0;
- void checkIndex(int index) {
- if (index < 0 || index > length - 1) {
- throw "Incorrect index: your index: " + to_string(index) + ", line length: " + to_string(length - 1);
- }
- }
- public:
- Line() {
- string defaultLineText = "Hello, world!";
- this->line = new char[defaultLineText.length() + 1];
- length = (int) defaultLineText.length() + 1;
- strcpy(this->line, defaultLineText.c_str());
- cout << "constructor L";
- }
- explicit Line(const string &line) { //avoids implicit calls to constructor
- this->line = new char[line.length() + 1];
- length = (int) line.length() + 1;
- strcpy(this->line, line.c_str());
- cout << "constructor L";
- }
- explicit Line(const Line &userLine) { // copy constructor
- length = userLine.length;
- delete[]line;
- line = new char[length];
- for (int i = 0; i < length; i++) {
- line[i] = userLine.line[i];
- }
- //userLine->line = line;
- cout << "constructor L";
- }
- ~Line() {
- delete[] line;
- cout << "destructor L" << endl;
- }
- void printArr(char *arr, int arrLength) {
- for (int i = 0; i < arrLength; i++) {
- cout << arr[i];
- }
- cout << endl;
- }
- void clear() {
- length = 1;
- char *newLine = new char[1];
- newLine[0] = '\0';
- delete[]line;
- line = newLine;
- };
- void deleteSymbolFromTheEnd() {
- deleteSymbolByIndex(length - 2);
- }
- void deleteFromTheBeginning() {
- deleteSymbolByIndex(0);
- }
- void deleteSymbolByIndex(int index) {
- checkIndex(index);
- char *newLine = new char[length - 1];
- for (int i = 0; i < index; i++) {
- newLine[i] = line[i];
- }
- for (int i = index + 1; i < length; i++) {
- newLine[i - 1] = line[i];
- }
- length--;
- delete[]line;
- line = newLine;
- }
- void insertTextFromIndex(int index, const string &userLine) {
- checkIndex(index);
- char *newLine = new char[length + userLine.length()];
- for (int i = 0; i < index; i++) {
- newLine[i] = line[i];
- }
- for (int i = 0; i < userLine.length(); i++) {
- newLine[i + index] = userLine[i];
- }
- for (int i = index; i < length; i++) {
- newLine[i + userLine.length()] = line[i];
- }
- delete[] line;
- line = newLine;
- length += userLine.length();
- }
- void print() {
- printArr(line, length);
- }
- string getLine() {
- return line;
- }
- int getSize() {
- return length;
- }
- Line operator=(Line &other) {
- char *newLine = new char[other.getLine().size() + 1];
- string str = other.getLine();
- std::copy(str.begin(), str.end(), newLine);
- newLine[other.getLine().size()] = '\0';
- line = newLine;
- newLine = nullptr;
- length = (int) (other.getLine().size()) + 1;
- delete[] newLine;
- }
- };
- class Text {
- Line *text;
- int length;
- int numberOfSentences = 0;
- public:
- Text(Line *line) {
- text = line;
- length = line->getSize();
- numberOfSentences++;
- cout << "constructor T";
- }
- ~Text() {
- delete text;
- cout << "destructor T";
- //text->~Line();
- }
- void add(Line *line) {
- text->insertTextFromIndex(length - 1, " " + line->getLine());
- length += line->getSize();
- numberOfSentences++;
- }
- void print() {
- text->print();
- }
- int getLength() {
- return length;
- }
- string getText() {
- return text->getLine();
- }
- int getNumberOfSentence() {
- return numberOfSentences;
- }
- Text operator=(Text otherText) {
- text = new Line(otherText.getText());
- length = otherText.getLength();
- numberOfSentences = otherText.getNumberOfSentence();
- }
- Text operator+(Text &otherText) {
- int otherTextLength = otherText.getLength();
- text->insertTextFromIndex(length - 1, otherText.getText());
- length += otherText.getLength();
- //cout << "other length: " + to_string(otherText.getLength()) + " " + to_string(length);
- }
- };
- int main() {
- try {
- Line converter("Hello, dudes!");
- converter.print();
- converter.insertTextFromIndex(1, "[new_word]");
- converter.print();
- converter.deleteSymbolByIndex(5);
- converter.print();
- converter.deleteFromTheBeginning();
- converter.print();
- converter.deleteSymbolFromTheEnd();
- converter.print();
- Line newLine("new_line");
- converter = newLine;
- converter.print();
- Text text(&converter);
- text.print();
- cout << " error " << endl;
- Line *anotherLine = new Line("RANDOM_WORD");
- text.add(anotherLine);
- text.print();
- text = text + text;
- Text newText(new Line("virus_WORD_virus"));
- text = newText;
- text.print();
- } catch (const char *return_exception) {
- cout << return_exception;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement