Advertisement
Guest User

Untitled

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