Advertisement
Mary_99

recursion proba

Jan 30th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <ctime>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int columnsQuantity = 0;
  9. int rowsQuantity = 0;
  10. char countedCharsTable[100][100];
  11. char charsTable[100][100];
  12. string text = string("");
  13. int charTypesCount = 1;
  14. char * charTypes = new char[text.size()];
  15.  
  16. int count(int c, int r, char t) {
  17.  
  18.       int n = 0;
  19.  
  20.       if(charsTable[r][c] != t || countedCharsTable[r][c] == t) {
  21.           return n;
  22.       } else {
  23.  
  24.           n++;
  25.           countedCharsTable[r][c] = t;
  26.  
  27.           if(c < columnsQuantity - 1) {
  28.               n += count(c + 1, r, t);
  29.           }
  30.  
  31.           if(c > 0) {
  32.               n += count(c - 1, r, t);
  33.           }
  34.  
  35.           if(r < rowsQuantity - 1) {
  36.               n += count(c, r + 1, t);
  37.           }
  38.  
  39.           if(r > 0) {
  40.               n += count(c, r - 1, t);
  41.           }
  42.  
  43.           return n;
  44.       }
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50.  
  51.   srand(time( NULL ));
  52.  
  53.   int * table = new int[10];
  54.  
  55.   for(int i = 0; i < 10; i++) {
  56.      table[i] = rand() % 10;
  57.   }
  58.  
  59.   for(int i = 0; i < 10; i++) {
  60.      cout << table[i] << "; ";
  61.   }
  62.  
  63.   text += "#################aa##a###c######\n";
  64.   text += "####bbbbaaaabbbbbaaaaa###ccc##cc\n";
  65.   text += "#o##bbbbaaaabbbbbaaaaa###c#c##cc\n";
  66.   text += "#oo#bbbbaeeabbbbbbbbaa##cc#ccccc\n";
  67.   text += "#o##bbbbaeeabbbbbaaaaaa#cc#####c\n";
  68.   text += "#o##bbbbaaaabbbbbaaaaaa#cc#####c\n";
  69.  
  70.   for(int i = 0; i < text.size(); i++) {
  71.     if(text[i] == '\n') {
  72.         rowsQuantity++;
  73.     } else if (rowsQuantity < 1) {
  74.         columnsQuantity++;
  75.     }
  76.   }
  77.  
  78.   cout << "2D string dimensions: " << rowsQuantity << " rows; " << columnsQuantity << " columns.\n\n";
  79.  
  80.   int columnIndex = 0;
  81.   int rowIndex = 0;
  82.   for(int i = 0; i < text.size(); i++) {
  83.       if(text[i] != '\n') {
  84.           charsTable[rowIndex][columnIndex] = text[i];
  85.           countedCharsTable[rowIndex][columnIndex] = '-';
  86.           columnIndex++;
  87.       } else {
  88.           rowIndex++;
  89.           columnIndex = 0;
  90.       }
  91.   }
  92.  
  93.   for(int i = 0; i < rowsQuantity; i++) {
  94.       for(int j = 0; j < columnsQuantity; j++) {
  95.         cout << charsTable[i][j];
  96.       }
  97.       cout << "\n";
  98.   }
  99.   cout << "\n";
  100.  
  101.   charTypes[0] = text[0];
  102.  
  103.   for(int i = 0; i < text.size(); i++) {
  104.       if(text[i] != '\n') {
  105.         for(int j = 0; j < charTypesCount; j++) {
  106.           if(charTypes[j] == text[i]) {
  107.             break;
  108.           } else if(j == charTypesCount - 1) {
  109.             charTypes[charTypesCount] = text[i];
  110.             charTypesCount++;
  111.           }
  112.         }
  113.       }
  114.   }
  115.  
  116.   cout << "Char types: ";
  117.   for(int i = 0; i < charTypesCount; i++) {
  118.       cout << charTypes[i];
  119.   }
  120.   cout << "\n\n";
  121.  
  122.   int n = 0, r = 0, c = 0;
  123.   char t = charsTable[r][c];
  124.  
  125.   n = count(r, c, t);
  126.  
  127.     cout << "\n\nRegion of symbols " << t << ", area: " << n << "\n\n";
  128.  
  129.     for(int i = 0; i < rowsQuantity; i++) {
  130.       for(int j = 0; j < columnsQuantity; j++) {
  131.         cout << countedCharsTable[i][j];
  132.       }
  133.       cout << "\n";
  134.     }
  135.     cout << "\n";
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement