Advertisement
Guest User

JA3-Task-2-Copy-Paste

a guest
Nov 12th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4. using namespace std;
  5.  
  6. string copyingText(string text, int copyFrom, int copyTo)
  7. {
  8.     string result;
  9.     int copyBeginIndex = 0;
  10.     int copyEndIndex = (int)text.size();
  11.     int size = (int)text.size();
  12.     for(int i = copyFrom; i >= 0; --i)
  13.     {
  14.         if(text [i] == ' ')
  15.         {
  16.             copyBeginIndex = i + 1;
  17.             break;
  18.             for(int k = copyTo; k <= size; ++k)
  19.             {
  20.                 if (text [k] == ' ')
  21.                 {
  22.                     copyEndIndex = k;
  23.                     break;
  24.                 }
  25.                 if (k + 1 > size)
  26.                 {
  27.                     copyEndIndex = size;
  28.                     break;
  29.                 }
  30.             }
  31.             if(copyTo > size)
  32.             {
  33.                 copyEndIndex = size;
  34.                 break;
  35.             }
  36.         }
  37.         if(i == 0)
  38.         {
  39.              copyBeginIndex = 0;
  40.              for(int k = copyTo; k <= size; ++k)
  41.              {
  42.                 if(text [k] == ' ')
  43.                 {
  44.                     copyEndIndex = k;
  45.                     break;
  46.                 }
  47.                 if (k + 1 == size)
  48.                 {
  49.                     copyEndIndex = size;
  50.                     break;
  51.                 }
  52.             }
  53.             if(copyTo > size)
  54.             {
  55.                 copyEndIndex = size;
  56.                 break;
  57.             }
  58.         }
  59.     }
  60.     result = text.substr(copyBeginIndex, copyEndIndex - copyBeginIndex);
  61.     return result;
  62. }
  63.  
  64. string pastedText(string text, string copiedText, int pasteIn)
  65. {
  66.     string result;
  67.     if(text[pasteIn] == ' ')
  68.     {
  69.       result = text.substr (0, pasteIn);
  70.       result.append (" ");
  71.       result.append (copiedText);
  72.       result += text.substr(pasteIn, text.size());
  73.     }
  74.     else
  75.     {
  76.         result = text.substr(0, pasteIn);
  77.         result.append(copiedText);
  78.         result.append(text.substr(pasteIn, text.size()));
  79.     }
  80.     return result;
  81. }
  82.  
  83. int main()
  84. {
  85.  
  86. string text;
  87. getline (cin, text);
  88. string command;
  89. int copyFrom, copyTo, pasteIn;
  90. stack<string> copiedText;
  91. while(command != "end")
  92. {
  93.     cin >> command;
  94.     if(command == "copy")
  95.     {
  96.         cin >> copyFrom;
  97.         cin >> copyTo;
  98.         string copyText = copyingText(text, copyFrom, copyTo);
  99.         copiedText.push(copyText);
  100.     }
  101.     else if(command == "paste")
  102.     {
  103.         if(copiedText.empty())
  104.             continue;
  105.         else
  106.         {
  107.         cin >> pasteIn;
  108.         string copyText = copiedText.top();
  109.         copiedText.pop();
  110.         text = pastedText(text, copyText, pasteIn);
  111.         }
  112.     }
  113. }
  114. cout << text;
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement