Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <string>
- using namespace std;
- string copyingText(string text, int copyFrom, int copyTo)
- {
- string result;
- int copyBeginIndex = 0;
- int copyEndIndex = (int)text.size();
- int size = (int)text.size();
- for(int i = copyFrom; i >= 0; --i)
- {
- if(text [i] == ' ')
- {
- copyBeginIndex = i + 1;
- break;
- for(int k = copyTo; k <= size; ++k)
- {
- if (text [k] == ' ')
- {
- copyEndIndex = k;
- break;
- }
- if (k + 1 > size)
- {
- copyEndIndex = size;
- break;
- }
- }
- if(copyTo > size)
- {
- copyEndIndex = size;
- break;
- }
- }
- if(i == 0)
- {
- copyBeginIndex = 0;
- for(int k = copyTo; k <= size; ++k)
- {
- if(text [k] == ' ')
- {
- copyEndIndex = k;
- break;
- }
- if (k + 1 == size)
- {
- copyEndIndex = size;
- break;
- }
- }
- if(copyTo > size)
- {
- copyEndIndex = size;
- break;
- }
- }
- }
- result = text.substr(copyBeginIndex, copyEndIndex - copyBeginIndex);
- return result;
- }
- string pastedText(string text, string copiedText, int pasteIn)
- {
- string result;
- if(text[pasteIn] == ' ')
- {
- result = text.substr (0, pasteIn);
- result.append (" ");
- result.append (copiedText);
- result += text.substr(pasteIn, text.size());
- }
- else
- {
- result = text.substr(0, pasteIn);
- result.append(copiedText);
- result.append(text.substr(pasteIn, text.size()));
- }
- return result;
- }
- int main()
- {
- string text;
- getline (cin, text);
- string command;
- int copyFrom, copyTo, pasteIn;
- stack<string> copiedText;
- while(command != "end")
- {
- cin >> command;
- if(command == "copy")
- {
- cin >> copyFrom;
- cin >> copyTo;
- string copyText = copyingText(text, copyFrom, copyTo);
- copiedText.push(copyText);
- }
- else if(command == "paste")
- {
- if(copiedText.empty())
- continue;
- else
- {
- cin >> pasteIn;
- string copyText = copiedText.top();
- copiedText.pop();
- text = pastedText(text, copyText, pasteIn);
- }
- }
- }
- cout << text;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement