Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <stack>
- using namespace std;
- void makeSelection(int& startIndex, int& endIndex, const string &text)
- {
- int fromIndex = startIndex;
- int toIndex = endIndex;
- for (int i = fromIndex; i > 0; --i)
- {
- if (text[i - 1] == ' ')
- {
- break;
- }
- startIndex--;
- }
- for (int i = toIndex; i < text.size(); ++i)
- {
- if (text[i] == ' ')
- {
- break;
- }
- endIndex++;
- }
- }
- void copy(vector<string>& copyCommand, const string& copyText, stack<string>& clip)
- {
- string& fromIndexInString = copyCommand[1];
- int from = stoi(fromIndexInString);
- string& toIndexInString = copyCommand[2];
- int to = stoi(toIndexInString);
- makeSelection(from, to, copyText);
- clip.push(copyText.substr(from, to - from));
- }
- void paste(vector<string>& pasteCommand, string& pasteText, stack<string>& clip)
- {
- string& indexInString = pasteCommand[1];
- int index = stoi(indexInString);
- if (!clip.empty())
- {
- if (pasteText[index] != ' ')
- {
- pasteText.insert(index, clip.top());
- }
- else
- {
- string current = clip.top();
- current.insert(0, " ");
- pasteText.insert(index, current);
- }
- clip.pop();
- }
- }
- void makeOperation(string& text, string& command)
- {
- stack<string> clipboard;
- getline(cin, command);
- while (true)
- {
- if (command == "end")
- {
- break;
- }
- vector<string> vec;
- string word;
- istringstream istr(command);
- while (istr >> word)
- {
- vec.push_back(word);
- }
- const int size = vec.size();
- if (size == 3)
- {
- copy(vec, text, clipboard);
- }
- else
- {
- paste(vec, text, clipboard);
- }
- getline(cin, command);
- }
- }
- int main()
- {
- string inputText;
- getline(cin, inputText);
- string operation;
- makeOperation(inputText, operation);
- cout << inputText << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment