Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <algorithm>
- #include <windows.h>
- using namespace std;
- int takeChoise() {
- const string ERROR_CHOISE = "Проверьте корректность введнных данных!\n";
- bool isIncorrect;
- int num;
- do {
- isIncorrect = false;
- cin >> num;
- if (cin.fail()) {
- isIncorrect = true;
- cout << ERROR_CHOISE;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && cin.get() != '\n') {
- cin.clear();
- while (cin.get() != '\n');
- cout << ERROR_CHOISE;
- isIncorrect = true;
- }
- if (!isIncorrect && (num < 0 || num > 1)) {
- isIncorrect = true;
- cout << ERROR_CHOISE;
- }
- } while (isIncorrect);
- return num;
- }
- string wayToFile() {
- fstream checkFile;
- string way;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Введите путь к файлу\n";
- cin >> way;
- checkFile.open(way);
- if (!checkFile.is_open()) {
- cout << "Проверьте правильность и нахождение вашего файла по заданному пути\n";
- isIncorrect = true;
- }
- else {
- isIncorrect = false;
- }
- checkFile.close();
- } while (isIncorrect);
- return way;
- }
- int sortSpace(string text, string*& arrayOfSymbols) {
- int i = 0;
- int checkForLoop = 0;
- int k = 0;
- while (i < text.length()) {
- checkForLoop = 0;
- while (isalpha((unsigned char)text[i]) && i < text.length()) {
- arrayOfSymbols[k] += text[i];
- i++;
- checkForLoop++;
- }
- if (checkForLoop != 0) {
- k++;
- }
- checkForLoop = 0;
- while (!isalpha((unsigned char)text[i]) && i < text.length()) {
- arrayOfSymbols[k] += text[i];
- i++;
- checkForLoop++;
- }
- if (checkForLoop != 0) {
- k++;
- }
- }
- return k;
- }
- int takeFromConsole(string*& arrayOfSymbols) {
- string text;
- getline(cin, text);
- int k = sortSpace(text, arrayOfSymbols);
- return k;
- }
- int takeFromFile(string*& arrayOfSymbols) {
- fstream file;
- string fileName = wayToFile();
- file.open(fileName);
- string text;
- if (!file.bad()) {
- getline(file, text, '\t');
- }
- file.close();
- int k = sortSpace(text, arrayOfSymbols);
- return k;
- }
- void conventText(string*& arrayOfSymbols, int& k) {
- int i = 0;
- while (arrayOfSymbols[i] == " ") {
- i++;
- }
- for (; i < k; i += 2) {
- arrayOfSymbols[i] = "(" + arrayOfSymbols[i] + ")";
- i += 2;
- if (i < k) {
- transform(arrayOfSymbols[i].begin(), arrayOfSymbols[i].end(), arrayOfSymbols[i].begin(), ::toupper);
- }
- }
- }
- void resultInConsole(string* arrayOfSymbols, int k) {
- for (int i = 0; i < k; i++) {
- cout << arrayOfSymbols[i];
- }
- }
- void resultInFile(string* arrayOfSymbols, int k) {
- fstream file;
- string fileName = wayToFile();
- file.open(fileName);
- if (!file.bad()) {
- for (int i = 0; i < k; i++) {
- file << arrayOfSymbols[i];
- }
- }
- file.close();
- }
- int choiseEnter(string*& arrayOfSymbols) {
- cout << "Выберете способ ввода. \n 1. Введите 1 для использования консоли. \n 2. Введите 0 для использования файла.\n";
- int choise = takeChoise();
- int k;
- if (choise == 1) {
- k = takeFromConsole(arrayOfSymbols);
- conventText(arrayOfSymbols, k);
- }
- else {
- k = takeFromFile(arrayOfSymbols);
- conventText(arrayOfSymbols, k);
- }
- return k;
- }
- void choiseResult(string* arrayOfSymbols, int k) {
- cout << "Выберете способ вывода результата. \n 1. Введите 1 для использования консоли. \n 2. Введите 0 для использования файла.\n";
- int choise = takeChoise();
- if (choise) {
- resultInConsole(arrayOfSymbols, k);
- }
- else {
- resultInFile(arrayOfSymbols, k);
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Rus");
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- string* arrayOfSymbols = new string[1000000];
- int k = choiseEnter(arrayOfSymbols);
- choiseResult(arrayOfSymbols, k);
- }
Advertisement
Add Comment
Please, Sign In to add comment