Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_set>
- #include <fstream>
- using namespace std;
- constexpr string DEFAULT_INPUT_FILE = "./input.txt";
- constexpr string DEFAULT_OUTPUT_FILE = "./output.txt";
- constexpr int PRINT_TYPE_MIN = 0;
- constexpr int PRINT_TYPE_MAX = 2;
- constexpr int SCAN_TYPE_MIN = 0;
- constexpr int SCAN_TYPE_MAX = 1;
- string calculate(const string &value)
- {
- string result;
- const unordered_set set{'+', '-', '*', '/', '.', ',', '=', '^', '%', '0', '2', '4', '6', '8'};
- for(int i = 0; i < value.length(); i++)
- if (set.contains(value[i]))
- result += value[i];
- return result;
- }
- bool isTextFile(const string &filePath)
- {
- bool isTxt;
- isTxt = (filePath.length() > 4 &&
- filePath[filePath.length() - 4] == '.' &&
- filePath[filePath.length() - 3] == 't' &&
- filePath[filePath.length() - 2] == 'x' &&
- filePath[filePath.length() - 1] == 't');
- return isTxt;
- }
- bool checkFileAvailability(const string &filePath, const bool read)
- {
- ios::openmode mode;
- if (read)
- mode = ifstream::in;
- else
- mode = ifstream::app;
- fstream file(filePath, mode);
- bool available;
- available = file.good();
- file.close();
- if (available && !isTextFile(filePath))
- available = false;
- return available;
- }
- int takeIntValueFromConsole(const string& text)
- {
- bool isInCorrect;
- int value;
- isInCorrect = true;
- while (isInCorrect)
- {
- cout << text;
- cin >> value;
- isInCorrect = false;
- if (cin.fail())
- {
- cout << "Введите число, а не строку или что-то иное!\n";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- isInCorrect = true;
- }
- }
- return value;
- }
- int takeIntValueInRangeFromConsole(const string& text, const int min, const int max)
- {
- bool isInCorrect;
- int value;
- isInCorrect = true;
- value = 0;
- while (isInCorrect)
- {
- value = takeIntValueFromConsole(text);
- isInCorrect = false;
- if (value < min || value > max)
- {
- cout << "Число должно находится в границах от " << min << " до " << max << "\n";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- isInCorrect = true;
- }
- }
- return value;
- }
- string takeCorrectFile(const bool input)
- {
- bool isInCorrect;
- string value;
- isInCorrect = true;
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- while(isInCorrect) {
- if (input)
- cout << "Введите путь до входного файла (пустая строка – " << DEFAULT_INPUT_FILE << "): ";
- else
- cout << "Введите путь до выходного файла (пустая строка – " << DEFAULT_OUTPUT_FILE << "): ";
- getline(cin, value);
- if (value.empty())
- {
- if (input)
- value = DEFAULT_INPUT_FILE;
- else
- value = DEFAULT_OUTPUT_FILE;
- }
- isInCorrect = false;
- if (!checkFileAvailability(value, input)) {
- isInCorrect = true;
- cout << "Путь ведёт до файла, который недоступен или который не является текстовым файлом!\n";
- }
- }
- return value;
- }
- string takeValueFromFile(ifstream &reader)
- {
- string value;
- getline(reader, value);
- reader.clear();
- return value;
- }
- string readStringFromFile()
- {
- bool isInCorrect = true;
- string value;
- while (isInCorrect)
- {
- string filePath = takeCorrectFile(true);
- isInCorrect = false;
- ifstream file(filePath);
- value = takeValueFromFile(file);
- if (value.empty())
- {
- isInCorrect = true;
- cout << "Строка не может быть пустой!\n";
- }
- }
- return value;
- }
- string readStringFromConsole()
- {
- bool isInCorrect = true;
- string value;
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- while (isInCorrect)
- {
- cout << "Строка для решения: ";
- getline(cin, value);
- isInCorrect = false;
- if (value.empty())
- {
- isInCorrect = true;
- cout << "Строка не может быть пустой!\n";
- }
- }
- return value;
- }
- string readString()
- {
- int type;
- string value;
- cout << "\nКак считать значения для поиска решения?\n";
- cout << "0 - Из ввода с клавиатуры (консоль)\n";
- cout << "1 - Из файла\n";
- type = takeIntValueInRangeFromConsole("Выбранный вариант ввода: ", SCAN_TYPE_MIN, SCAN_TYPE_MAX);
- if (type == 1)
- value = readStringFromFile();
- else
- value = readStringFromConsole();
- return value;
- }
- bool saveResultIntoFile(const string &filePath, const string &res)
- {
- bool saved;
- saved = true;
- ofstream file(filePath);
- if (file.good())
- {
- file << res;
- }
- else
- saved = false;
- file.close();
- return saved;
- }
- void printResult(const string &res)
- {
- bool saved;
- int type;
- cout << "\nКуда вывести результат решения?\n";
- cout << "0 - Только консоль\n";
- cout << "1 - Только в файл\n";
- cout << "2 - И в файл, и в консоль\n";
- type = takeIntValueInRangeFromConsole("Выбранный вариант вывода: ", PRINT_TYPE_MIN, PRINT_TYPE_MAX);
- saved = false;
- string output;
- switch (type)
- {
- case 0:
- cout << "\nМножество арифметических знаков и чётных чисел: " << res << "\n";
- break;
- case 1:
- output = takeCorrectFile(false);
- saved = saveResultIntoFile(output, res);
- break;
- case 2:
- output = takeCorrectFile(false);
- saved = saveResultIntoFile(output, res);
- cout << "\nМножество арифметических знаков и чётных чисел: " << res << "\n";
- break;
- }
- if (saved)
- cout << "Результат записан в выходной файл!" << "\n";
- }
- int main()
- {
- string value, res;
- cout << "2. Дана непустая последовательность символов, требуется построить и напечатать множество, элементами которого являются встречающиеся в последовательности знаки арифметических операций и чётные цифры.\n";
- value = readString();
- res = calculate(value);
- printResult(res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment