Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- #include <iostream>
- #include <set>
- using namespace std;
- bool checkPermissionForWriting(const string& PathToFile) {
- bool Flag;
- ofstream file_out(PathToFile);
- if (file_out.is_open()) {
- file_out.close();
- Flag = true;
- }
- else {
- std::cout << "File isn't available for writing.\n";
- Flag = false;
- }
- return Flag;
- }
- int inputNumber(const int MIN_NUMBER, const int MAX_NUMBER) {
- bool isIncorrect;
- int number;
- string input = "";
- do {
- getline(cin, input);
- isIncorrect = false;
- try {
- number = stoi(input);
- }
- catch (invalid_argument ex) {
- cout << "You need to write a number.\n";
- isIncorrect = true;
- }
- catch (out_of_range ex) {
- cout << "Number mustn't be less than " << MIN_NUMBER << " and more than"
- << MAX_NUMBER << "\n";
- isIncorrect = true;
- }
- if (!isIncorrect && (number < MIN_NUMBER || number > MAX_NUMBER)) {
- cout << "Number mustn't be less than " << MIN_NUMBER << " and more than "
- << MAX_NUMBER << "\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return number;
- }
- int chooseWayOfInputOrOuput() {
- const int CONSOLE_INPUT = 1;
- const int FILE_INPUT = 2;
- int UserWay;
- do {
- UserWay = inputNumber(1, 2);
- } while (UserWay != CONSOLE_INPUT && UserWay != FILE_INPUT);
- return UserWay;
- }
- const string receiveTextFromFile(const string Path)
- {
- string Text;
- ifstream InputFile(Path);
- getline(InputFile, Text);
- InputFile.close();
- return Text;
- }
- string receiveTextFromConsole() {
- cout << "Input string:\n";
- string Text;
- getline(cin, Text);
- return Text;
- }
- void printResultInConsole(const set<char>& ResultSet) {
- cout << "Result set of chars:\n";
- for (set<char> ::iterator it = ResultSet.begin(); it != ResultSet.end(); it++)
- cout << *it << " ";
- cout << "\n";
- }
- bool checkPermissionForReading(const string& PathToFile) {
- bool Flag;
- ifstream file_out(PathToFile);
- if (file_out.is_open()) {
- file_out.close();
- Flag = true;
- }
- else {
- std::cout << "File isn't available for reading.\n";
- Flag = false;
- }
- return Flag;
- }
- bool checkExtension(const string& PathToFile) {
- const string extension = "txt";
- bool Flag;
- if (PathToFile.substr(PathToFile.length() - 3) == extension) {
- Flag = true;
- }
- else {
- cout << "Wrong extension.\n";
- Flag = false;
- }
- return Flag;
- }
- const string inputPathToFileForReading() {
- string PathToFile;
- do {
- cout << "Input path to file for reading: \n";
- getline(cin, PathToFile);
- } while (!checkExtension(PathToFile) || !checkPermissionForReading(PathToFile));
- return PathToFile;
- }
- bool checkReceivedText(string Text, const set<char>& SetOfRequiredChars) {
- bool IsStringCorrect = false;
- int EndNumber = Text.length();
- for (int i = 0; (i < EndNumber && !IsStringCorrect); i++)
- if (SetOfRequiredChars.count(Text[i]) == 1)
- IsStringCorrect = true;
- return IsStringCorrect;
- }
- string receiveText(const set<char> &SetOfRequiredChars) {
- string Path;
- string Text;
- int UserWay;
- bool IsIncorrect;
- IsIncorrect = false;
- do {
- cout << "Choose way of input: \nType '1' if you want to receive string from console.\nType '2' if you want to receieve string from file.\n";
- UserWay = chooseWayOfInputOrOuput();
- switch (UserWay) {
- case 1:
- {
- Text = receiveTextFromConsole();
- break;
- }
- case 2:
- {
- do {
- Path = inputPathToFileForReading();
- IsIncorrect = checkPermissionForReading(Path);
- } while (!IsIncorrect);
- Text = receiveTextFromFile(Path);
- break;
- }
- }
- IsIncorrect = checkReceivedText(Text, SetOfRequiredChars);
- if (!IsIncorrect)
- cout << "There are not any required chars in text.\n";
- } while (!IsIncorrect);
- return Text;
- }
- set<char> receiveResultSet(const string& Text, const set<char>& SetOfRequiredChars) {
- set<char> ResultSet;
- int EndNumber = Text.length();
- for (int i = 0; i < EndNumber; i++)
- if (SetOfRequiredChars.count(Text[i]) == 1)
- ResultSet.insert(Text[i]);
- return ResultSet;
- }
- void printResultInFile(const string &PathToFile, set<char> ResultSet)
- {
- ofstream fout(PathToFile, ios::trunc);
- fout << "Result set of chars:\n";
- for (set<char> ::iterator it = ResultSet.begin(); it != ResultSet.end(); it++)
- fout << *it << " ";
- fout.close();
- }
- const string inputPathToFileForWriting() {
- string PathToFile;
- do {
- cout << "Input path to file for writing: \n";
- getline(cin, PathToFile);
- } while (!checkExtension(PathToFile) || !checkPermissionForWriting(PathToFile));
- return PathToFile;
- }
- void printResult(const set<char> ResultSet) {
- cout << "Choose way of output: \nType '1' if you want to print result in console.\nType '2' if you want to print result in file.\n";
- string PathToFile;
- int UserWay;
- UserWay = chooseWayOfInputOrOuput();
- bool IsIncorrect;
- IsIncorrect = false;
- switch (UserWay) {
- case 1:
- {
- printResultInConsole(ResultSet);
- break;
- }
- case 2:
- {
- do {
- PathToFile = inputPathToFileForWriting();
- IsIncorrect = checkPermissionForWriting(PathToFile);
- } while (!IsIncorrect);
- printResultInFile(PathToFile, ResultSet);
- break;
- }
- }
- }
- int main() {
- const set<char> SetOfRequiredChars{ '+' ,'-' ,'/', '*', '{' ,'}' ,'(' ,')', '[', ']', '}', '0', '2', '4', '6', '8'};
- cout << "Program receives and prints even number, brackets and characters of arithmetic operations from the string you inserted\n";
- string Text;
- Text = receiveText(SetOfRequiredChars);
- set<char> ResultSet = receiveResultSet(Text, SetOfRequiredChars);
- printResult(ResultSet);
- cout << "Program is completed.";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement