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