Advertisement
vadim_sharaf

Untitled

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