Advertisement
Vladislav8653

laba_3_1_c++

Nov 28th, 2022 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. ifstream fin;
  6. ofstream fout;
  7. const int SIZE = 25;
  8. 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" };
  9.  
  10. bool choose() {
  11.     int inputNumber;
  12.     bool isIncorrect;
  13.     const int MIN_NUM = 0;
  14.     const int MAX_NUM = 1;
  15.     do {
  16.         cin >> inputNumber;
  17.         isIncorrect = false;
  18.         if (cin.fail()) {
  19.             isIncorrect = true;
  20.             cout << "Please, enter a number." << endl;
  21.             cin.clear();
  22.             while (cin.get() != '\n');
  23.         }
  24.         if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
  25.             cout << "You are out of input range!" << endl;
  26.             isIncorrect = true;
  27.         }
  28.     } while (isIncorrect);
  29.     if (inputNumber == 1)
  30.         return true;
  31.     else
  32.         return false;
  33. }
  34.  
  35. // консольный ввод и вывод
  36.  
  37. string consoleInputString() {
  38.     string strInput;
  39.     bool isIncorrect;
  40.     do {
  41.         cin >> strInput;
  42.         isIncorrect = false;
  43.         if (cin.fail()) {
  44.             cout << "Please, enter a string." << endl;
  45.             isIncorrect = true;
  46.             cin.clear();
  47.             while (cin.get() != '\n');
  48.         }
  49.     } while (isIncorrect);
  50.     return strInput;
  51. }
  52. string* findPairs(string strInput) {
  53.     string* combinations = new string[SIZE];
  54.     for (int j = 0; j < SIZE; j++)
  55.         combinations[j] = "";
  56.     int i = 0;
  57.     int counter = 0;
  58.     string strForTest;
  59.     int whereIsPair = 0;
  60.     for (int j = 0; j < SIZE; j++) {
  61.         strForTest = strInput;
  62.         do {
  63.             if (strForTest.find(arrOfPairs[j]) != -1) {
  64.                 counter++;
  65.                 whereIsPair = strForTest.find(arrOfPairs[j]);
  66.                 strForTest = strForTest.substr(whereIsPair + 1);
  67.             }
  68.         } while (whereIsPair == -1);
  69.         if (counter != 0) {
  70.             combinations[i] = arrOfPairs[j];
  71.             i++;
  72.         }
  73.         counter = 0;
  74.     }
  75.     return combinations;
  76. }
  77.  
  78.  
  79. int* findRepetition(string strInput) {
  80.     int* repeat = new int[SIZE];
  81.     for (int j = 0; j < SIZE; j++)
  82.         repeat[j] = 0;
  83.     int i = 0;
  84.     int counter = 0;
  85.     string strForTest;
  86.     int whereIsPair = 0;
  87.     for (int j = 0; j < SIZE; j++) {
  88.         strForTest = strInput;
  89.         do {
  90.             if ((strForTest.find(arrOfPairs[j]) != -1)) {
  91.                 counter++;
  92.                 whereIsPair = strForTest.find(arrOfPairs[j]);
  93.                 strForTest = strForTest.substr(whereIsPair + 1);
  94.             }
  95.         } while (whereIsPair == -1);
  96.         if (counter != 0) {
  97.             repeat[i] = counter;
  98.             i++;
  99.         }
  100.         counter = 0;
  101.     }
  102.     return repeat;
  103. }
  104.  
  105. void check(int* repeat) {
  106.     int counter = 0;
  107.     const int SIZE = 25;
  108.     for (int i = 0; i < sizeof(repeat); i++) {
  109.         if (repeat[i] == 0)
  110.             counter++;
  111.     }
  112.     if (counter == SIZE)
  113.         cout << "There are no pairs:(" << endl;
  114. }
  115.  
  116. void consoleOutput(int* repeat, string* combinations) {
  117.     for (int j = 0; j < sizeof(repeat); j++)
  118.         if (repeat[j] != 0 && combinations[j] != "")
  119.             cout << combinations[j] << " - " << repeat[j] << " times" << endl;
  120. }
  121. string inputFilePath() {
  122.     const int EXTENSION_SIZE = 4;
  123.     string path;
  124.     string ext;
  125.     bool isIncorrect;
  126.     do {
  127.         isIncorrect = false;
  128.         cout << "Input path to file: " << endl;
  129.         cin >> path;
  130.         fin.open(path);
  131.         if (path.size() > EXTENSION_SIZE) {
  132.             ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
  133.         }
  134.         else {
  135.             cout << "Incorrect file name." << endl;
  136.             isIncorrect = true;
  137.         } if (!isIncorrect && ext != ".txt") {
  138.             cout << "Must have .txt!" << endl;
  139.             isIncorrect = true;
  140.         }
  141.         else if (!isIncorrect && !fin.is_open()) {
  142.             cout << "Wrong way to file." << endl;
  143.             isIncorrect = true;
  144.         }
  145.     } while (isIncorrect);
  146.     fin.close();
  147.     return path;
  148. }
  149.  
  150. string FileInputString(string path) {
  151.     string strInput;
  152.     bool isIncorrect;
  153.     do {
  154.         isIncorrect = false;
  155.         fin.open(path);
  156.         fin >> strInput;
  157.         if (cin.fail()) {
  158.             isIncorrect = true;
  159.             cout << "Check file" << endl;
  160.             path = inputFilePath();
  161.         }
  162.     } while (isIncorrect);
  163.     return strInput;
  164. }
  165.  
  166. void fileOutput(string path, int* repeat, string* combinations) {
  167.     bool isIncorrect;
  168.     fout.open(path);
  169.     do {
  170.         isIncorrect = false;
  171.         try {
  172.             for (int i = 0; i < sizeof(repeat); i++) {
  173.                 if (repeat[i] != 0 && combinations[i] != "")
  174.                     fout << combinations[i] << " - " << repeat[i] << " times" << endl;
  175.                 fout << '\n';
  176.             }
  177.         }
  178.         catch (exception e) {
  179.             isIncorrect = true;
  180.             fout << "Mistake of output in file. Input path." << endl;
  181.             path = inputFilePath();
  182.         }
  183.     } while (isIncorrect);
  184.     cout << "Successful output in file." << endl;
  185.     fout.close();
  186. }
  187.  
  188. int main() {
  189.     setlocale(LC_ALL, "Russian");
  190.     cout << "For pairs of adjacent letters (Latin) occurring in a given text, indicate how many times each of these two-letter combinations occurs." << endl;
  191.     cout << "Enter type of input." << endl << "1 is console input, 0 is file input." << endl;
  192.     bool chose = choose();
  193.     string strInput;
  194.     string path;
  195.     if (chose) {
  196.         cout << "Input string without spaces: " << endl;;
  197.         strInput = consoleInputString();
  198.     }
  199.     else {
  200.         path = inputFilePath();
  201.         strInput = FileInputString(path);
  202.     }
  203.     int* repeat = findRepetition(strInput);
  204.     string* combination = findPairs(strInput);
  205.     cout << "Enter type of input." << endl << "1 is console output, 0 is file output." << endl;
  206.     chose = choose();
  207.     if (chose) {
  208.         consoleOutput(repeat, combination);
  209.     }
  210.     else {
  211.         path = inputFilePath();
  212.         fileOutput(path, repeat, combination);
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement