Advertisement
Guest User

Untitled

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