Advertisement
Nevarkir

6B

Dec 7th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. #define _CRT_SECURE_NO_WARNINGS
  6.  
  7. using namespace std;
  8.  
  9. void fillingAnArrayOfWords(ifstream*, string*, int*);
  10. void outputToTheConsoleAnArrayOfWords(string*, int*);
  11. void arrayReset(int*, int*);
  12. void numberOfSubwords(string*, int*, int*);
  13. void arrayOutput(ofstream*, int*, int*);
  14.  
  15. int main()
  16. {
  17.     setlocale(LC_ALL, "Russian");
  18.     int n;
  19.     ifstream in("D:\\Users\\Nevarkir\\source\\repos\\Project1\\Project1\\6b\\input.txt");
  20.     in >> n;
  21.     string* c;
  22.     c = new string[n];
  23.  
  24.     fillingAnArrayOfWords(&in, c, &n);
  25.     //cout << "\n" << endl;
  26.     //outputToTheConsoleAnArrayOfWords(c, &n);
  27.     in.close();
  28.  
  29.     int* a;
  30.     a = new int[n];
  31.  
  32.     arrayReset(a, &n);
  33.     numberOfSubwords(c, a, &n);
  34.  
  35.     ofstream out;
  36.     out.open("D:\\Users\\Nevarkir\\source\\repos\\Project1\\Project1\\6b\\output.txt");
  37.     arrayOutput(&out, a, &n);
  38.  
  39.     out.close();
  40.     delete[] c;
  41.     delete[] a;
  42.     system("PAUSE");
  43.     return 0;
  44. }
  45.  
  46. void fillingAnArrayOfWords(ifstream* in, string* c, int* n)
  47. {
  48.     for (int i = 0; i < (*n); i++)
  49.     {
  50.         (*in) >> c[i];
  51.     }
  52. }
  53.  
  54.  
  55. void outputToTheConsoleAnArrayOfWords(string* c, int* n)
  56. {
  57.     for (int i = 0; i < (*n); i++)
  58.     {
  59.         cout << c[i] << endl;
  60.     }
  61. }
  62.  
  63. void arrayReset(int* a, int* n)
  64. {
  65.     for (int i = 0; i < (*n); i++)
  66.     {
  67.         a[i] = 0;
  68.     }
  69. }
  70.  
  71. void numberOfSubwords(string* c, int* a, int* n)
  72. {
  73.     for (int i = 0; i < (*n); i++)
  74.     {
  75.         for (int j = 0; j < (*n); j++)
  76.         {
  77.             if (i != j)
  78.             {
  79.                 //cout << "=============================================================================================" << endl;
  80.                 //cout << "START: В слове - " << c[i] << " под номером " << i << " при сравнении со словом - " << c[j] << " под номером " << j << ". Значение a[" << i << "] = " << a[i] << endl;
  81.                 int m1 = c[i].length();
  82.                 char* s1;
  83.                 s1 = new char[m1];
  84.                 strcpy(s1, c[i].c_str());
  85.                 //cout << s1 << " с длиной: " << m1 << endl;
  86.                 int m2 = c[j].length();
  87.                 char* s2;
  88.                 s2 = new char[m2];
  89.                 strcpy(s2, c[j].c_str());
  90.                 //cout << s2 << " с длиной: " << m2 << endl;
  91.                 int count = 0;
  92.                 for (int k = 0; k < m2; k++)
  93.                 {
  94.                     if (k + m1 - 1 < m2)
  95.                     {
  96.                         if (s2[k] == s1[0])
  97.                         {
  98.                             count = 0;
  99.                             //cout << "---------------------------------------------------------------------------------------------" << endl;
  100.                             //cout << "Начат процесс сравнения: " << endl;
  101.                             for (int h = 0; h < m1; h++)
  102.                             {
  103.                                 if (s2[k + h] == s1[h]) count++;
  104.                                 //cout << k + h << ": " << s2[k + h] << " и " << h << ": " << s1[h] << endl;
  105.                             }
  106.                             //cout << "Сравнение завершено" << endl;
  107.                             //cout << "---------------------------------------------------------------------------------------------" << endl;
  108.                             if (count == m1)
  109.                             {
  110.                                 //cout << "---------------------------------------------------------------------------------------------" << endl;
  111.                                 //cout << "Слова совпали" << endl;
  112.                                 a[i]++;
  113.                                 //cout << a[i] << endl;
  114.                                 //cout << "---------------------------------------------------------------------------------------------" << endl;
  115.                             }
  116.                             else
  117.                             {
  118.                                 //cout << "---------------------------------------------------------------------------------------------" << endl;
  119.                                 //cout << "Слова не совпали" << endl;
  120.                                 //cout << "---------------------------------------------------------------------------------------------" << endl;
  121.                             }
  122.                         }
  123.                     }
  124.                     else
  125.                     {
  126.                         //cout << "---------------------------------------------------------------------------------------------" << endl;
  127.                         //cout << "Длины не совпали" << endl;
  128.                         //cout << "---------------------------------------------------------------------------------------------" << endl;
  129.                     }
  130.                 }
  131.                 //cout << "END: В слове - " << c[i] << " под номером " << i << " при сравнении со словом - " << c[j] << " под номером " << j << ". Значение a[" << i << "] = " << a[i] << endl;
  132.             }
  133.         }
  134.     }
  135. }
  136.  
  137. void arrayOutput (ofstream* out, int* a, int* n)
  138. {
  139.     for (int i = 0; i < (*n); i++)
  140.     {
  141.         (*out) << a[i] << endl;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement