mitakvd

02. Copy - Paste

Nov 6th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. void makeSelection(int& startIndex, int& endIndex, const string &text)
  10. {
  11.     int fromIndex = startIndex;
  12.     int toIndex = endIndex;
  13.  
  14.     for (int i = fromIndex; i > 0; --i)
  15.     {
  16.         if (text[i - 1] == ' ')
  17.         {
  18.             break;
  19.         }
  20.         startIndex--;
  21.     }
  22.     for (int i = toIndex; i < text.size(); ++i)
  23.     {
  24.         if (text[i] == ' ')
  25.         {
  26.             break;
  27.         }
  28.         endIndex++;
  29.     }
  30. }
  31.  
  32. void copy(vector<string>& copyCommand, const string& copyText, stack<string>& clip)
  33. {
  34.     string& fromIndexInString = copyCommand[1];
  35.     int from = stoi(fromIndexInString);
  36.     string& toIndexInString = copyCommand[2];
  37.     int to = stoi(toIndexInString);
  38.  
  39.     makeSelection(from, to, copyText);
  40.  
  41.     clip.push(copyText.substr(from, to - from));
  42. }
  43.  
  44. void paste(vector<string>& pasteCommand, string& pasteText, stack<string>& clip)
  45. {
  46.     string& indexInString = pasteCommand[1];
  47.     int index = stoi(indexInString);
  48.  
  49.     if (!clip.empty())
  50.     {
  51.         if (pasteText[index] != ' ')
  52.         {
  53.             pasteText.insert(index, clip.top());
  54.         }
  55.         else
  56.         {
  57.             string current = clip.top();
  58.             current.insert(0, " ");
  59.             pasteText.insert(index, current);
  60.         }
  61.         clip.pop();
  62.     }
  63. }
  64.  
  65. void makeOperation(string& text, string& command)
  66. {
  67.     stack<string> clipboard;
  68.     getline(cin, command);
  69.     while (true)
  70.     {
  71.         if (command == "end")
  72.         {
  73.             break;
  74.         }
  75.  
  76.         vector<string> vec;
  77.         string word;
  78.         istringstream istr(command);
  79.  
  80.         while (istr >> word)
  81.         {
  82.             vec.push_back(word);
  83.         }
  84.         const int size = vec.size();
  85.         if (size == 3)
  86.         {
  87.             copy(vec, text, clipboard);
  88.         }
  89.         else
  90.         {
  91.             paste(vec, text, clipboard);
  92.         }
  93.  
  94.         getline(cin, command);
  95.     }
  96. }
  97.  
  98. int main()
  99. {
  100.     string inputText;
  101.     getline(cin, inputText);
  102.  
  103.     string operation;
  104.  
  105.     makeOperation(inputText, operation);
  106.  
  107.     cout << inputText << endl;
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment