Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- ifstream fin;
- ofstream fout;
- const int SIZE = 25;
- const string arrOfPairs[SIZE] = { "ab", "bc", "cd", "de", "ef", "fg", "gh", "hi", "ig", "gk", "kl", "lm", "mn", "no", "op", "pq", "qr", "rs", "st", "tu", "uv", "vw", "wz", "xy", "yz" };
- bool choose() {
- int inputNumber;
- bool isIncorrect;
- const int MIN_NUM = 0;
- const int MAX_NUM = 1;
- do {
- cin >> inputNumber;
- isIncorrect = false;
- if (cin.fail()) {
- isIncorrect = true;
- cout << "Please, enter a number." << endl;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- cout << "You are out of input range!" << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- // консольный ввод и вывод
- string consoleInputString() {
- string strInput;
- bool isIncorrect;
- do {
- cin >> strInput;
- isIncorrect = false;
- if (cin.fail()) {
- cout << "Please, enter a string." << endl;
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return strInput;
- }
- string* findPairs(string strInput) {
- string* combinations = new string[SIZE];
- for (int j = 0; j < SIZE; j++)
- combinations[j] = "";
- int i = 0;
- int counter = 0;
- string strForTest;
- int whereIsPair = 0;
- for (int j = 0; j < SIZE; j++) {
- strForTest = strInput;
- do {
- if (strForTest.find(arrOfPairs[j]) != -1) {
- counter++;
- whereIsPair = strForTest.find(arrOfPairs[j]);
- strForTest = strForTest.substr(whereIsPair + 1);
- }
- } while (whereIsPair == -1);
- if (counter != 0) {
- combinations[i] = arrOfPairs[j];
- i++;
- }
- counter = 0;
- }
- return combinations;
- }
- int* findRepetition(string strInput) {
- int* repeat = new int[SIZE];
- for (int j = 0; j < SIZE; j++)
- repeat[j] = 0;
- int i = 0;
- int counter = 0;
- string strForTest;
- int whereIsPair = 0;
- for (int j = 0; j < SIZE; j++) {
- strForTest = strInput;
- do {
- if ((strForTest.find(arrOfPairs[j]) != -1)) {
- counter++;
- whereIsPair = strForTest.find(arrOfPairs[j]);
- strForTest = strForTest.substr(whereIsPair + 1);
- }
- } while (whereIsPair == -1);
- if (counter != 0) {
- repeat[i] = counter;
- i++;
- }
- counter = 0;
- }
- return repeat;
- }
- void check(int* repeat) {
- int counter = 0;
- const int SIZE = 25;
- for (int i = 0; i < sizeof(repeat); i++) {
- if (repeat[i] == 0)
- counter++;
- }
- if (counter == SIZE)
- cout << "There are no pairs:(" << endl;
- }
- void consoleOutput(int* repeat, string* combinations) {
- for (int j = 0; j < sizeof(repeat); j++)
- if (repeat[j] != 0 && combinations[j] != "")
- cout << combinations[j] << " - " << repeat[j] << " times" << endl;
- }
- string inputFilePath() {
- const int EXTENSION_SIZE = 4;
- string path;
- string ext;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Input path to file: " << endl;
- cin >> path;
- fin.open(path);
- if (path.size() > EXTENSION_SIZE) {
- ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
- }
- else {
- cout << "Incorrect file name." << endl;
- isIncorrect = true;
- } if (!isIncorrect && ext != ".txt") {
- cout << "Must have .txt!" << endl;
- isIncorrect = true;
- }
- else if (!isIncorrect && !fin.is_open()) {
- cout << "Wrong way to file." << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- fin.close();
- return path;
- }
- string FileInputString(string path) {
- string strInput;
- bool isIncorrect;
- do {
- isIncorrect = false;
- fin.open(path);
- fin >> strInput;
- if (cin.fail()) {
- isIncorrect = true;
- cout << "Check file" << endl;
- path = inputFilePath();
- }
- } while (isIncorrect);
- return strInput;
- }
- void fileOutput(string path, int* repeat, string* combinations) {
- bool isIncorrect;
- fout.open(path);
- do {
- isIncorrect = false;
- try {
- for (int i = 0; i < sizeof(repeat); i++) {
- if (repeat[i] != 0 && combinations[i] != "")
- fout << combinations[i] << " - " << repeat[i] << " times" << endl;
- fout << '\n';
- }
- }
- catch (exception e) {
- isIncorrect = true;
- fout << "Mistake of output in file. Input path." << endl;
- path = inputFilePath();
- }
- } while (isIncorrect);
- cout << "Successful output in file." << endl;
- fout.close();
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- cout << "For pairs of adjacent letters (Latin) occurring in a given text, indicate how many times each of these two-letter combinations occurs." << endl;
- cout << "Enter type of input." << endl << "1 is console input, 0 is file input." << endl;
- bool chose = choose();
- string strInput;
- string path;
- if (chose) {
- cout << "Input string without spaces: " << endl;;
- strInput = consoleInputString();
- }
- else {
- path = inputFilePath();
- strInput = FileInputString(path);
- }
- int* repeat = findRepetition(strInput);
- string* combination = findPairs(strInput);
- cout << "Enter type of input." << endl << "1 is console output, 0 is file output." << endl;
- chose = choose();
- if (chose) {
- consoleOutput(repeat, combination);
- }
- else {
- path = inputFilePath();
- fileOutput(path, repeat, combination);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement