Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdio>
  5. #define FNAME "text"
  6. using namespace std;
  7.  
  8. const int N = 1000; char Symbol, OldStr[N], NewStr[N], Context[N], SubString[N];
  9.  
  10. int MaxLength, StringsAmount, CurrentPosition, DisplayAmount, ChangeAmount, ChangedString, DeleteLength; short int DataSource, Command;
  11.  
  12. int Reading(short int);
  13. int Insert(string,  string, int);
  14. int Saving();
  15. int Show(int, int);
  16. int Replace(string, string, int);
  17. int Delete(string, int, int);
  18.  
  19. string Text[N];
  20.  
  21. int main()
  22. {
  23.     cout << "Set the maximum length of the strings in the text: ";
  24.     cin >> MaxLength;
  25.    
  26.     cout << "How will data be entered: from the file (0) or from the console (1)?  ";
  27.     cin >> DataSource; 
  28.     while (!((DataSource == 0) || (DataSource == 1)))
  29.     {
  30.         cout << "Incorrect format! Please try again: ";
  31.         cin >> DataSource;
  32.     }
  33.    
  34.     Reading(DataSource);
  35.    
  36.     while (1)
  37.     {
  38.         cout << endl << "Enter the command - insert substring (1), delete substring (2), replace substring (3), display strings (4), saving text in file (5), quit the program (0):  ";
  39.         cin >> Command;
  40.         cout << endl;
  41.         switch (Command)
  42.         {
  43.             case 1:
  44.                 cout << "Enter the substring you want to insert:  ";
  45.                 cin.clear();
  46.                 cin.sync();
  47.                 cin.getline(SubString, MaxLength, '\n');
  48.                
  49.                 cout << "Enter the context after which you wish to insert the substring:  ";
  50.                 cin.clear();
  51.                 cin.sync();
  52.                 cin.getline(Context, MaxLength);
  53.                
  54.                 cout << "Enter the number of strings you want to change:  ";
  55.                 cin >> ChangeAmount;
  56.                
  57.                 cout << "Enter the numbers of these strings:  ";
  58.                 for (int i = 0;  i < ChangeAmount; i++)
  59.                 {
  60.                     cin >> ChangedString;
  61.                     Insert(SubString, Context, ChangedString - 1);
  62.                 }
  63.                
  64.                 cout << "Done!" << endl;
  65.                
  66.                 break; 
  67.             case 2:
  68.                 cout << "Enter the context after which we will delete substring:  ";
  69.                 cin.clear();
  70.                 cin.sync();
  71.                 cin.getline(Context, MaxLength, '\n');
  72.                
  73.                 cout << "Enter the length of the substring we will delete:  ";
  74.                 cin >> DeleteLength;
  75.                
  76.                 cout << "Enter the number of strings you want to change:  ";
  77.                 cin >> ChangeAmount;
  78.                
  79.                 cout << "Enter the numbers of these strings:  ";
  80.                 for (int i = 0;  i < ChangeAmount; i++)
  81.                 {
  82.                     cin >> ChangedString;
  83.                     Delete(Context, DeleteLength, ChangedString - 1);
  84.                 }
  85.                
  86.                 cout << "Done!" << endl;
  87.  
  88.                 break; 
  89.             case 3:
  90.                 cout << "Enter the substring you want to replace:  ";
  91.                 cin.clear();
  92.                 cin.sync();
  93.                 cin.getline(OldStr, MaxLength, '\n');
  94.                
  95.                 cout << "Enter the new substring you want to insert instead:  ";
  96.                 cin.getline(NewStr, MaxLength, '\n');
  97.                
  98.                 cout << "Enter the number of strings you want to change:  ";
  99.                 cin >> ChangeAmount;
  100.                
  101.                 cout << "Enter the numbers of these strings:  ";
  102.                 for (int i = 0;  i < ChangeAmount; i++)
  103.                 {
  104.                     cin >> ChangedString;
  105.                     Replace(OldStr, NewStr, ChangedString - 1);
  106.                 }
  107.                
  108.                 cout << "Done!" << endl;
  109.                
  110.                 break; 
  111.             case 4:
  112.                 cout << "Enter the number of first string and amount of strings you want to display:  ";
  113.                 cin >> CurrentPosition >> DisplayAmount;
  114.                
  115.                 Show(CurrentPosition, DisplayAmount);
  116.                
  117.                 break; 
  118.             case 5:
  119.                 Saving();
  120.                
  121.                 cout << "Done!" << endl;
  122.                
  123.                 break;                         
  124.             case 0:
  125.                 return 0;  
  126.         }
  127.     }
  128.     return 0;
  129. }
  130.  
  131. //Reading text from the file or from the console. Here is only one argument - source of the data (0 or 1).
  132. int Reading(short int DataSource)
  133. {
  134.     int CurrentString = 0, StringsNumber;
  135.     char Str[MaxLength];
  136.    
  137.     if (DataSource == 0)
  138.     {
  139.         ifstream in_stream;
  140.         in_stream.open("file.in");
  141.         while (!in_stream.eof())
  142.         {
  143.             in_stream.getline(Str, sizeof(Str));
  144.             Text[CurrentString] = Str;
  145.             CurrentString += 1;
  146.         }
  147.     }
  148.         else
  149.     {
  150.         cout << "Enter the number of strings: ";
  151.         cin >> StringsNumber;
  152.        
  153.         cout << endl;
  154.        
  155.         for (int i = 0; CurrentString < StringsNumber; i++)
  156.         {
  157.             cout << i + 1 << ") ";
  158.             cin.clear();
  159.             cin.sync();
  160.             cin.getline(Str, sizeof(Str));
  161.             Text[CurrentString] = Str;
  162.             CurrentString += 1;
  163.         }
  164.     }
  165.    
  166.     StringsAmount = CurrentString;
  167.    
  168.     return 0;
  169. }
  170.  
  171. //Saving text in the file after changing. The user should think up the filename.
  172. int Saving()
  173. {
  174.     string FileName;
  175.    
  176.     cout << "Please, enter the filename:  ";
  177.     cin >> FileName;
  178.    
  179.     ofstream out_stream;
  180.     out_stream.open(FileName.c_str());
  181.    
  182.     for (int i = DataSource; i < StringsAmount; i++)
  183.         out_stream << Text[i] << endl;
  184.        
  185.     return 0;
  186. }
  187.  
  188. //That function dispays the secuence of strings.
  189. int Show(int StartPosition, int AmountToShow)
  190. {
  191.     for (int i = StartPosition - 1; i < StartPosition + AmountToShow - 1; i++)
  192.         cout << i + 1 << ") " << Text[i] << endl;
  193.        
  194.     return 0;
  195. }
  196.  
  197. //Replacing the substring in the secuence of strings to another substring.
  198. int Replace(string OldSubstr, string NewSubstr, int StringNumber)
  199. {
  200.     int StrPos;
  201.     while ((StrPos = Text[StringNumber].find(OldSubstr,0)) != string::npos)
  202.     Text[StringNumber].replace(StrPos, OldSubstr.length(), NewSubstr);
  203.    
  204.     return 0;
  205. }
  206.  
  207. //Inserting the substring after the context.
  208. int Insert(string SubStr, string ContextStr, int StringNumber)
  209. {
  210.    
  211.     int StrPos, PostfixBegin = 0;
  212.     while ((StrPos = Text[StringNumber].find(ContextStr, PostfixBegin)) != string::npos)
  213.     {
  214.         Text[StringNumber].insert(StrPos + ContextStr.length(), SubStr);
  215.         PostfixBegin = StrPos + sizeof(ContextStr);
  216.     }
  217.  
  218.     return 0;
  219. }
  220.  
  221. //Delete substring after the context. The user should think up the sequence of the strings, there that operation will done. Also he should specify, how long is the substring.
  222. int Delete(string ContextStr, int Length, int StringNumber)
  223. {
  224.     int StrPos, PostfixBegin = 0;
  225.     while ((StrPos = Text[StringNumber].find(ContextStr, PostfixBegin)) != string::npos)
  226.     {
  227.         Text[StringNumber].erase(StrPos + ContextStr.length(), Length);
  228.         PostfixBegin = StrPos + sizeof(ContextStr);
  229.     }  
  230.  
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement