Advertisement
Guest User

Untitled

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