Advertisement
Guest User

Untitled

a guest
Nov 4th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 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.  
  19.             for (int k = copyTo; k <= (text.size() - 1); ++k)
  20.             {
  21.                 if (text [k] == ' ')
  22.                 {
  23.                     copyEndIndex = k;
  24.                     break;
  25.                 }
  26.  
  27.                 if (k + 1 > (text.size() - 1))
  28.                 {
  29.                     copyEndIndex = text.size();
  30.                     break;
  31.                 }
  32.             }
  33.  
  34.             if (copyTo > (text.size() - 1))
  35.             {
  36.                 copyEndIndex = text.size();
  37.                 break;
  38.             }
  39.  
  40.         }
  41.  
  42.         if (i == 0)
  43.         {
  44.             copyBeginIndex = 0;
  45.  
  46.              for (int k = copyTo; k <= (text.size() - 1); ++k)
  47.             {
  48.                 if (text [k] == ' ')
  49.                 {
  50.                     copyEndIndex = k;
  51.                     break;
  52.                 }
  53.  
  54.                 if (k + 1 == (text.size() - 1))
  55.                 {
  56.                     copyEndIndex = text.size();
  57.                     break;
  58.                 }
  59.             }
  60.  
  61.             if (copyTo > (text.size() - 1))
  62.             {
  63.                 copyEndIndex = text.size();
  64.                 break;
  65.             }
  66.         }
  67.  
  68.     }
  69.  
  70.     result = text.substr (copyBeginIndex, copyEndIndex);
  71.     return result;
  72. }
  73.  
  74. string pastedText (string text, string copiedText, int pasteIn)
  75. {
  76.  
  77.     string result;
  78.  
  79.     if (text [pasteIn] == ' ')
  80.     {
  81.       result = text.substr (0, pasteIn);
  82.       result.append (" ");
  83.       result.append (copiedText);
  84.       result += text.substr (pasteIn, text.size());
  85.     } else
  86.     {
  87.         result = text.substr (0, pasteIn);
  88.         result.append (copiedText);
  89.         result.append (text.substr(pasteIn, text.size()));
  90.     }
  91.  
  92.     return result;
  93. }
  94.  
  95. int main()
  96. {
  97.  
  98. string text;
  99. getline (cin, text);
  100.  
  101. string command;
  102. int copyFrom, copyTo, pasteIn;
  103. stack <string> copiedText;
  104.  
  105. cycle:while (command != "end")
  106. {
  107.     cin >> command;
  108.  
  109.     if (command == "copy")
  110.     {
  111.         cin >> copyFrom;
  112.         cin >> copyTo;
  113.  
  114.         string copyText = copyingText(text, copyFrom, copyTo);
  115.         copiedText.push (copyText);
  116.  
  117.     } else if (command == "paste")
  118.     {
  119.         if (copiedText.empty())
  120.         {
  121.             goto cycle;
  122.         } else
  123.         {
  124.         cin >> pasteIn;
  125.         string copyText = copiedText.top();
  126.         copiedText.pop();
  127.         text = pastedText (text, copyText, pasteIn);
  128.         }
  129.     }
  130. }
  131.  
  132. cout << text;
  133.  
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement