ftpud12

Untitled

Nov 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #define _CTR_SECURE_NO_WARNINGS
  5.  
  6. const int PHRASE_LEN = 9;
  7.  
  8. using namespace std;
  9.  
  10. string table[2] = { "the quick brown fox jumps over the lazy dog", "" };
  11. char trans(char ch)
  12. {
  13.     for (int i = 0; table[0][i]; i++)
  14.     {
  15.         if (ch == table[1][i]) return table[0][i];
  16.     }
  17.  
  18.     return ch;
  19. }
  20.  
  21. int calcChars(string phrase, char ch) {
  22.     int c=0;
  23.     for(int i=0; phrase[i]; i++) {
  24.         if(phrase[i] == ch) {
  25.             c++;
  26.         }
  27.     }
  28.     return c;
  29. }
  30.  
  31. void ToDo()
  32. {
  33.     const char phrase[] = "\2\2\3 \1\2\1\1\1 \1\2\4\1\1 \1\4\1 \1\2\1\1\1 \4\1\3\2 \2\2\3 \1\1\1\1 \1\4\1";
  34.  
  35.     ifstream in("input.txt");
  36.     if (!in.is_open())
  37.     {
  38.         cout << "file not open";
  39.         return;
  40.     }
  41.     bool isExists = false;
  42.  
  43.     int n;
  44.     in >> n;
  45.  
  46.     char ** text = new char* [n];
  47.     in.ignore(255, '\n'); //. пропустили первую строку
  48.     for (int i = 0; i < n; i++)
  49.     {
  50.         text[i] = new char[80];
  51.         in.getline(text[i], 80);
  52.         if (text[i][strlen(text[i]) - 1] == '\r') text[i][strlen(text[i]) - 1] = 0;
  53.  
  54.         cout<<"\n";
  55.         //if (strlen(text[i]) == strlen(phrase))
  56.         //{
  57.         if (isExists == false)
  58.         {
  59.             for (int j = 0; j < strlen(text[i]); j++)
  60.             {
  61.  
  62.                 if(text[i][j] == ' ' && phrase[j] == ' ') {
  63.                     cout<<" ";
  64.                 }
  65.                 if(text[i][j] != ' ' && phrase[j] != ' ') {
  66.                     cout<<calcChars(text[i], text[i][j])<<"|"<<(int)phrase[j]<<" ";
  67.                 }
  68.  
  69.                 if (text[i][j] == ' ' && phrase[j] == ' ' ||
  70.                         (text[i][j] != ' ' && phrase[j] != ' '
  71.                           && phrase[j] == calcChars(text[i], text[i][j]))) {
  72.                         isExists = true;
  73.                 }
  74.                 else
  75.                 {
  76.                     isExists = false;
  77.                     break;
  78.                 }
  79.             }
  80.             if (isExists) table[1] = text[i];
  81.         }
  82.     }
  83.  
  84.     ofstream out("output.txt");
  85.  
  86.  
  87.     if (isExists)
  88.     {
  89.         for (int i = 0; i < n; i++)
  90.         {
  91.             for (int j = 0; j < strlen(text[i]); j++)
  92.             {
  93.                 out << trans(text[i][j]);
  94.             }
  95.             out << "\n";
  96.         }
  97.     }
  98.     else out << "No solution";
  99.  
  100.     out.close();
  101.     in.close();
  102. }
  103.  
  104. int main()
  105. {
  106.     ToDo();
  107.  
  108.     //system("pause");
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment