vadim_sharaf

Untitled

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