Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstdio>
- #define FNAME "text"
- using namespace std;
- const int N = 1000; char Symbol, OldStr[N], NewStr[N], Context[N], SubString[N];
- int MaxLength, StringsAmount, CurrentPosition, DisplayAmount, ChangeAmount, ChangedString, DeleteLength; short int DataSource, Command;
- int Reading(short int);
- int Insert(string, string, int);
- int Saving();
- int Show(int, int);
- int Replace(string, string, int);
- int Delete(string, int, int);
- string Text[N];
- int main()
- {
- cout << "Set the maximum length of the strings in the text: ";
- cin >> MaxLength;
- cout << "How will data be entered: from the file (0) or from the console (1)? ";
- cin >> DataSource;
- while (!((DataSource == 0) || (DataSource == 1)))
- {
- cout << "Incorrect format! Please try again: ";
- cin >> DataSource;
- }
- Reading(DataSource);
- while (1)
- {
- 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): ";
- cin >> Command;
- cout << endl;
- switch (Command)
- {
- case 1:
- cout << "Enter the substring you want to insert: ";
- cin.clear();
- cin.sync();
- cin.getline(SubString, MaxLength, '\n');
- cout << "Enter the context after which you wish to insert the substring: ";
- cin.clear();
- cin.sync();
- cin.getline(Context, MaxLength);
- cout << "Enter the number of strings you want to change: ";
- cin >> ChangeAmount;
- cout << "Enter the numbers of these strings: ";
- for (int i = 0; i < ChangeAmount; i++)
- {
- cin >> ChangedString;
- Insert(SubString, Context, ChangedString - 1);
- }
- cout << "Done!" << endl;
- break;
- case 2:
- cout << "Enter the context after which we will delete substring: ";
- cin.clear();
- cin.sync();
- cin.getline(Context, MaxLength, '\n');
- cout << "Enter the length of the substring we will delete: ";
- cin >> DeleteLength;
- cout << "Enter the number of strings you want to change: ";
- cin >> ChangeAmount;
- cout << "Enter the numbers of these strings: ";
- for (int i = 0; i < ChangeAmount; i++)
- {
- cin >> ChangedString;
- Delete(Context, DeleteLength, ChangedString - 1);
- }
- cout << "Done!" << endl;
- break;
- case 3:
- cout << "Enter the substring you want to replace: ";
- cin.clear();
- cin.sync();
- cin.getline(OldStr, MaxLength, '\n');
- cout << "Enter the new substring you want to insert instead: ";
- cin.getline(NewStr, MaxLength, '\n');
- cout << "Enter the number of strings you want to change: ";
- cin >> ChangeAmount;
- cout << "Enter the numbers of these strings: ";
- for (int i = 0; i < ChangeAmount; i++)
- {
- cin >> ChangedString;
- Replace(OldStr, NewStr, ChangedString - 1);
- }
- cout << "Done!" << endl;
- break;
- case 4:
- cout << "Enter the number of first string and amount of strings you want to display: ";
- cin >> CurrentPosition >> DisplayAmount;
- Show(CurrentPosition, DisplayAmount);
- break;
- case 5:
- Saving();
- cout << "Done!" << endl;
- break;
- case 0:
- return 0;
- }
- }
- return 0;
- }
- //Reading text from the file or from the console. Here is only one argument - source of the data (0 or 1).
- int Reading(short int DataSource)
- {
- int CurrentString = 0, StringsNumber;
- char Str[MaxLength];
- if (DataSource == 0)
- {
- ifstream in_stream;
- in_stream.open("file.in");
- while (!in_stream.eof())
- {
- in_stream.getline(Str, sizeof(Str));
- Text[CurrentString] = Str;
- CurrentString += 1;
- }
- }
- else
- {
- cout << "Enter the number of strings: ";
- cin >> StringsNumber;
- cout << endl;
- for (int i = 0; CurrentString < StringsNumber; i++)
- {
- cout << i + 1 << ") ";
- cin.clear();
- cin.sync();
- cin.getline(Str, sizeof(Str));
- Text[CurrentString] = Str;
- CurrentString += 1;
- }
- }
- StringsAmount = CurrentString;
- return 0;
- }
- //Saving text in the file after changing. The user should think up the filename.
- int Saving()
- {
- string FileName;
- cout << "Please, enter the filename: ";
- cin >> FileName;
- ofstream out_stream;
- out_stream.open(FileName.c_str());
- for (int i = DataSource; i < StringsAmount; i++)
- out_stream << Text[i] << endl;
- return 0;
- }
- //That function dispays the secuence of strings.
- int Show(int StartPosition, int AmountToShow)
- {
- for (int i = StartPosition - 1; i < StartPosition + AmountToShow - 1; i++)
- cout << i + 1 << ") " << Text[i] << endl;
- return 0;
- }
- //Replacing the substring in the secuence of strings to another substring.
- int Replace(string OldSubstr, string NewSubstr, int StringNumber)
- {
- int StrPos;
- while ((StrPos = Text[StringNumber].find(OldSubstr,0)) != string::npos)
- Text[StringNumber].replace(StrPos, OldSubstr.length(), NewSubstr);
- return 0;
- }
- //Inserting the substring after the context.
- int Insert(string SubStr, string ContextStr, int StringNumber)
- {
- int StrPos, PostfixBegin = 0;
- while ((StrPos = Text[StringNumber].find(ContextStr, PostfixBegin)) != string::npos)
- {
- Text[StringNumber].insert(StrPos + ContextStr.length(), SubStr);
- PostfixBegin = StrPos + sizeof(ContextStr);
- }
- return 0;
- }
- //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.
- int Delete(string ContextStr, int Length, int StringNumber)
- {
- int StrPos, PostfixBegin = 0;
- while ((StrPos = Text[StringNumber].find(ContextStr, PostfixBegin)) != string::npos)
- {
- Text[StringNumber].erase(StrPos + ContextStr.length(), Length);
- PostfixBegin = StrPos + sizeof(ContextStr);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement