Advertisement
vadim_sharaf

Untitled

Feb 13th, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <ctype.h >
  5. using namespace std;
  6. const unsigned N = 18;
  7. struct strM {
  8.     char S[N + 1];
  9.     char mark;
  10.     char sb;
  11.     unsigned count = 0;
  12. };
  13.  
  14. struct workWithFile {
  15.     bool fill;
  16.     char st;
  17.     int num;
  18.     char s1;
  19.     char s;
  20.     bool flag;
  21. };
  22. void inStrM(workWithFile& a1, strM& a, fstream& outFile) {
  23.     fstream inFile;
  24.     inFile.open("in.txt", ios::in);
  25.     if (inFile.is_open()) {
  26.         /*char c; char s1; char s;*/ unsigned counter = 0;
  27.         inFile >> a.sb;
  28.         if (inFile.eof()) {
  29.             a1.fill = false;
  30.             a1.st = 'e';
  31.         }
  32.         inFile >> a.mark;
  33.         if (inFile.eof()) {
  34.             a1.fill = false;
  35.             a1.st = 'e';
  36.         }
  37.         if (a.sb == a.mark) {
  38.             a1.fill = false;
  39.             a1.st = 'i';
  40.         }
  41.         inFile >> a1.num;
  42.         if (a1.num == 0 or a1.num < 0) {
  43.             a1.fill = false;
  44.             a1.st = 'i';
  45.         }
  46.         if (inFile.eof()) {
  47.             a1.fill = false;
  48.             a1.st = 'e';
  49.         }
  50.         inFile >> a1.s1;
  51.         //cout << a1.s1 << endl;
  52.         if (inFile.eof()) {
  53.             a1.fill = false;
  54.             a1.st = 'e';
  55.         }
  56.         if (a1.fill != false) {
  57.             inFile.seekg(-1, ios::cur);
  58.             inFile << resetiosflags(ios::skipws);
  59.             do {
  60.                 if (counter >= N) {
  61.                     break;
  62.                     cout << "Размер массива меньше количества символов";
  63.                 }
  64.                 inFile >> a1.s;
  65.                 if (a1.s == '\n') a1.s = ' ';
  66.                 a.S[counter] = a1.s;
  67.                 counter++;
  68.             } while (counter < a1.num);
  69.             a.S[counter] = a.mark;
  70.         }
  71.  
  72.     }
  73.     else {
  74.         cout << "Ошибка: файл не открылся.\n";
  75.         outFile << "Ошибка: файл не открылся.\n";
  76.     }
  77.     inFile.close();
  78. }
  79. void outError(workWithFile& a1, fstream& outFile) {
  80.     if (a1.st == 'e') {
  81.         cout << "Ошибка: файл пустой\n";
  82.         outFile << "Ошибка: файл пустой\n";
  83.     }
  84.     else if (a1.st == 'i') {
  85.         cout << "Ошибка: введенные данные некорректны\n";
  86.         outFile << "Ошибка: введенные данные некорректны\n";
  87.     }
  88. }
  89. void process(strM& a, workWithFile& a1, fstream& logFile, fstream& outFile) {
  90.     unsigned i = 0;
  91.     logFile << "Слова, начинающиеся с символа " << a.sb << ": ";
  92.     outFile << "Прочитанная строка: ";
  93.     a1.flag = false;
  94.     a.count = 0;
  95.     while (a.S[i] != a.mark) {
  96.         outFile << a.S[i];
  97.         if ((i == 0 or a.S[i - 1] == ' ') && (a.S[i] == a.sb)) {
  98.             a.count += 1;
  99.             a1.flag = true;
  100.             logFile << a.S[i];
  101.         }
  102.         else if (a1.flag == true) {
  103.             if (a.S[i] == ' ') a1.flag = false;
  104.             logFile << a.S[i];
  105.         }
  106.         i++;
  107.     }
  108.     outFile << '\n';
  109. }
  110. void outStrM(strM& a, fstream& outFile) {
  111.     outFile << "Количество слов, начинающихся с символа " << a.sb << " : " << a.count << "\n";
  112.  
  113. }
  114. int main() {
  115.     setlocale(LC_ALL, "RUS");
  116.     fstream outFile;
  117.     fstream logFile;
  118.     logFile.open("log.txt", ios::out);
  119.     outFile.open("out.txt", ios::out);
  120.     workWithFile a1;
  121.     strM a;
  122.     inStrM(a1, a, outFile);
  123.     //cout << a.S[0] << endl;
  124.     if (a1.fill) {
  125.         process(a, a1, logFile, outFile);
  126.         outStrM(a, outFile);
  127.     }
  128.     else {
  129.         outError(a1, outFile);
  130.     }
  131.     logFile.close();
  132.     outFile.close();
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement