Advertisement
Guest User

Untitled

a guest
May 27th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1.  
  2. #pragma hdrstop
  3. #pragma argsused
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include <vector>
  9. using namespace std;
  10.  
  11. class CText {
  12.     int strs;
  13.     vector<string> s;
  14.     int seps_col;
  15.     char seps[5];
  16. public:
  17.     void get_seps();
  18.     void get_first();
  19.     void read();
  20.     void write();
  21.     bool is_sep(char ch);
  22.     void processing();
  23.     void replace();
  24. };
  25.  
  26. void CText::get_seps() {
  27.     cout << "Enter number of separators" << endl;
  28.     cin >> seps_col;
  29.     cout << "Enter separators" << endl;
  30.     for (int i = 0; i < seps_col; i++)
  31.         cin >> seps[i];
  32. }
  33.  
  34. void CText::get_first() {
  35.     string str;
  36.     cout << "Enter string to insert as first or enter \"-_-\" to skip" << endl;
  37.     cin >> str;
  38.     cout << str << endl;
  39.     if (str != "-_-") {
  40.         strs++;
  41.         s.insert(s.begin(), str);
  42.     }
  43. }
  44.  
  45. void CText::read() {
  46.     ifstream in("input.txt");
  47.     strs = 0;
  48.     char ch;
  49.     int prev = 0;
  50.     int k = 0;
  51.     string str;
  52.     in.get(ch);
  53.     while (in) {
  54.         if (ch != '\n')
  55.             str += ch;
  56.         else {
  57.             s.push_back(str);
  58.             strs++;
  59.             if (prev == str.size()) k++;
  60.             else k = 0;
  61.             if (k == 4) {
  62.                 str.clear();
  63.                 get_first();
  64.                 break;
  65.             }
  66.             prev = str.size();
  67.             str.clear();
  68.         }
  69.         in.get(ch);
  70.     }
  71.     if (str.size()) {
  72.         s.push_back(str);
  73.         strs++;
  74.     }
  75.     in.close();
  76. }
  77.  
  78. void CText::write() {
  79.     ofstream out("output.txt");
  80.     if (!out)
  81.         cout << "Not enought file" << endl;
  82.  
  83.     for (unsigned int i = 0; i < s.size(); i++) {
  84.         for (unsigned int j = 0; j < s[i].size(); j++)
  85.             out << (s.at(i)).at(j);
  86.         out << endl;
  87.     }
  88.     out.close();
  89. }
  90.  
  91. bool CText::is_sep(char ch) {
  92.     for (int i = 0; i < seps_col; i++)
  93.         if (ch == seps[i])
  94.             return true;
  95.     return false;
  96. }
  97.  
  98. void CText::processing() {
  99.     int chet = 0, nechet = 0;
  100.     for (int i = 0; i < strs; i++)
  101.         for (int j = 0; j < s[i].length(); j++)
  102.             if (s[i][j] >= '0' && s[i][j] <= '9')
  103.                 if ((i + 1) % 2 == 0)
  104.                     chet++;
  105.                 else
  106.                     nechet++;
  107.     if (chet < nechet)
  108.         replace();
  109. }
  110.  
  111. void CText::replace() {
  112.     for (int i = 0; i < strs; i++) {
  113.         bool flag = false;
  114.         for (int j = 0; j < s[i].length(); j++)
  115.             if (s[i][j] >= '1' && s[i][j] <= '9')
  116.                 if (!is_sep(s[i][j])) {
  117.                     s[i][j] = s[i][j] - '1' + 'a';
  118.                     flag = true;
  119.                 }
  120.         if (!flag) {
  121.             s[i].insert(s[i].begin(), seps[0]);
  122.             s[i].insert(s[i].begin(), seps[0]);
  123.             s[i].insert(s[i].begin(), seps[0]);
  124.         }
  125.     }
  126. }
  127.  
  128. int main(int argc, char* argv[]) {
  129.     system("chcp 1251");
  130.     char rdels[5];
  131.     int n, i;
  132.     CText text;
  133.  
  134.     text.get_seps();
  135.     text.read();
  136.     text.processing();
  137.     text.write();
  138.  
  139.     system("pause");
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement