Advertisement
Leedwon

Untitled

Mar 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.75 KB | None | 0 0
  1. #include "l_subset_finder.h"
  2.  
  3. void findAndSaveIntSubset(int numberOfSetElements) {
  4.     int *arr = new int[numberOfSetElements]; // we will work on this array to generate next possible subsets
  5.     int rows = pow(2, numberOfSetElements);
  6.     int **twoDimArray = new int*[rows]; // all possible subsets are being saved in this 2darray
  7.     for (int i = 0; i < rows; i++) {
  8.         twoDimArray[i] = new int[numberOfSetElements];
  9.     }
  10.     int currentRowCount = 0;
  11.     for (int i = 0; i < numberOfSetElements; i++) {
  12.         arr[i] = 0;
  13.         twoDimArray[currentRowCount][i] = arr[i];
  14.     }
  15.     int currentNumberOfPermutations = 0;
  16.     int currentIndexInPermutation = 0;
  17.     do {
  18.         currentRowCount++;
  19.         currentNumberOfPermutations++;
  20.         currentIndexInPermutation = 0;
  21.         int numberOfPermutationsToModify = currentNumberOfPermutations;
  22.         while (numberOfPermutationsToModify % 2 == 0) {
  23.             numberOfPermutationsToModify /= 2;
  24.             currentIndexInPermutation++;
  25.         }
  26.         if (currentIndexInPermutation < numberOfSetElements) {
  27.             arr[currentIndexInPermutation] = 1 - arr[currentIndexInPermutation];
  28.             for (int i = 0; i < numberOfSetElements; i++) {
  29.                 twoDimArray[currentRowCount][i] = arr[i];
  30.             }
  31.         }
  32.     } while (currentIndexInPermutation < numberOfSetElements);
  33.     sort(twoDimArray, rows, numberOfSetElements);
  34.     saveSubsetsToFile(twoDimArray, rows, numberOfSetElements);
  35. }
  36.  
  37.  
  38. void findAndSaveCharSubset(int numberOfSetElements) {
  39.    
  40.     int *arr = new int[numberOfSetElements];
  41.     int rows = pow(2, numberOfSetElements); //all posible combinations = 2^numberOfElements
  42.     int **twoDimArray = new int*[rows];
  43.     for (int i = 0; i < rows; i++) {
  44.         twoDimArray[i] = new int[numberOfSetElements];
  45.     }
  46.     int currentRowCount = 0;
  47.     for (int i = 0; i < numberOfSetElements; i++) {
  48.         arr[i] = 0;
  49.         twoDimArray[currentRowCount][i] = arr[i];
  50.     }
  51.     int currentNumberOfPermutations = 0;
  52.     int currentIndexInPermutation = 0;
  53.     do {
  54.         currentRowCount++;
  55.         currentNumberOfPermutations++;
  56.         currentIndexInPermutation = 0;
  57.         int numberOfPermutationsToModify = currentNumberOfPermutations;
  58.         while (numberOfPermutationsToModify % 2 == 0) {
  59.             numberOfPermutationsToModify /= 2;
  60.             currentIndexInPermutation++;
  61.         }
  62.         if (currentIndexInPermutation < numberOfSetElements) {
  63.             arr[currentIndexInPermutation] = 1 - arr[currentIndexInPermutation];
  64.             for (int i = 0; i < numberOfSetElements; i++) {
  65.                 twoDimArray[currentRowCount][i] = arr[i];
  66.             }
  67.         }
  68.     } while (currentIndexInPermutation < numberOfSetElements);
  69.     sort(twoDimArray, rows, numberOfSetElements);
  70.     saveCharSubsetsToFile(twoDimArray, rows, numberOfSetElements);
  71. }
  72.  
  73. void findAllSubsetsOfGivenSizeOfASet(int numberOfElementsInSet, int numberOfElementsInSubSet) {
  74.  
  75.     if (numberOfElementsInSet < numberOfElementsInSubSet) {
  76.         return;
  77.     }
  78.     std::ofstream writingStreamInt("intSubsetOut2.txt");
  79.     std::ofstream writingStreamChar("charSubsetOut2.txt");
  80.  
  81.     char ch = 'a';
  82.     int *arr = new int[numberOfElementsInSubSet];
  83.     int i;
  84.     for (i = 0; i < numberOfElementsInSubSet; i++) {
  85.         arr[i] = i;
  86.     }
  87.     int p = numberOfElementsInSubSet - 1;
  88.     while (p >= 0) {
  89.         for (int j = 0; j < numberOfElementsInSubSet; j++) {
  90.             writingStreamInt << arr[j] << " ";
  91.             writingStreamChar << static_cast<char>(ch + arr[j]);
  92.             ch = 'a';
  93.         }
  94.         writingStreamInt << std::endl;
  95.         writingStreamChar << std::endl;
  96.         if (arr[numberOfElementsInSubSet - 1] == numberOfElementsInSet)
  97.             p--;
  98.         else
  99.             p = numberOfElementsInSubSet - 1;
  100.         if (p >= 0) {
  101.             for (i = numberOfElementsInSubSet - 1; i >= p; i--)
  102.                 arr[i] = arr[p] + i - p + 1;
  103.         }
  104.     };
  105.     writingStreamInt.close();
  106.     writingStreamChar.close();
  107. }
  108.  
  109. void sort(int **&arr, int rows, int numberOfSetElements) {
  110.     int *eachRowBinaryRepresentation = new int[rows] {0};
  111.     for (int i = 0; i < rows; i++) {
  112.         int k = numberOfSetElements - 1;
  113.         for (int j = 0; j < numberOfSetElements; j++) {
  114.             eachRowBinaryRepresentation[i] += arr[i][j] * pow(2, k);
  115.             k--;
  116.         }
  117.     }
  118.     for (int i = 0; i < rows; i++) {
  119.         for (int j = 0; j < rows - 1; j++) {
  120.             if (eachRowBinaryRepresentation[j] > eachRowBinaryRepresentation[j + 1]) {
  121.                 int temp1D = eachRowBinaryRepresentation[j];
  122.                 int *temp2D = new int[numberOfSetElements];
  123.                 eachRowBinaryRepresentation[j] = eachRowBinaryRepresentation[j + 1];
  124.                 eachRowBinaryRepresentation[j + 1] = temp1D;
  125.                 for (int k = 0; k < numberOfSetElements; k++) {
  126.                     temp2D[k] = arr[j][k];
  127.                 }
  128.                 for (int k = 0; k < numberOfSetElements; k++) {
  129.                     arr[j][k] = arr[j + 1][k];
  130.                 }
  131.                 for (int k = 0; k < numberOfSetElements; k++) {
  132.                     arr[j + 1][k] = temp2D[k];
  133.                 }
  134.             }
  135.         }
  136.     }
  137. }
  138.  
  139. void saveSubsetsToFile(int **subsetsArray, int rows, int columns) {
  140.     std::ofstream writingStream("intSubsetOut.txt");
  141.     for (int i = 0; i < rows; i++) {
  142.         for (int j = 0; j < columns; j++)
  143.             writingStream << subsetsArray[i][j];
  144.         writingStream << std::endl;
  145.     }
  146.     writingStream.close();
  147. }
  148.  
  149. void saveCharSubsetsToFile(int **subsetsArray, int rows, int columns) {
  150.     char *letters = new char[columns];
  151.     char k = 'a';
  152.     for (int i = 0; i < columns; i++) {
  153.         letters[i] = k++;
  154.     }
  155.     std::ofstream writingStream("charSubsetOut.txt");
  156.     for (int i = 0; i < rows; i++) {
  157.         for (int j = 0; j < columns; j++) {
  158.             if (subsetsArray[i][j] == 1) // could be true as well but type conflict warning
  159.                 writingStream << letters[j];
  160.             else
  161.                 writingStream << "-";
  162.         }
  163.         writingStream << std::endl;
  164.     }
  165.     writingStream.close();
  166. }
  167.  
  168. void findAllPossibleSumsToCreateNumber(int number) {
  169.     std::ofstream writingStream("Out3.txt");
  170.     int *partsOfNumber = new int[number];
  171.     int *howManyTimesIsPartAppearing = new int[number];
  172.     for (int i = 0; i < number; i++) {
  173.         partsOfNumber[i] = -1;
  174.         howManyTimesIsPartAppearing[i] = -1;
  175.     }
  176.     partsOfNumber[0] = number;
  177.     howManyTimesIsPartAppearing[0] = 1;
  178.     int d = 0;
  179.     writingStream << partsOfNumber[0];
  180.     writingStream << std::endl;
  181.     while (partsOfNumber[0] > 1) {
  182.         int sum = 0;
  183.         if (partsOfNumber[d] == 1) {
  184.             sum += howManyTimesIsPartAppearing[d];
  185.             partsOfNumber[d] = -1; // set values to -1 so it won't be printed to file
  186.             howManyTimesIsPartAppearing[d] = -1;
  187.             d--;
  188.         }
  189.         sum += partsOfNumber[d];
  190.         howManyTimesIsPartAppearing[d]--;
  191.         int l = partsOfNumber[d] - 1;
  192.         if (howManyTimesIsPartAppearing[d] > 0) {
  193.             d++;
  194.         }
  195.         partsOfNumber[d] = l;
  196.         howManyTimesIsPartAppearing[d] = sum / l;
  197.         l = sum % l;
  198.         if (l != 0) {
  199.             d++;
  200.             partsOfNumber[d] = l;
  201.             howManyTimesIsPartAppearing[d] = 1;
  202.         }
  203.         for (int i = 0; i < number; i++) {
  204.             if (howManyTimesIsPartAppearing[i] != -1) {
  205.                 for (int j = 0; j < howManyTimesIsPartAppearing[i]; j++) {
  206.                     writingStream << partsOfNumber[i];
  207.                     if (j < howManyTimesIsPartAppearing[i] -1)
  208.                         writingStream << "+";
  209.                 }
  210.                 if (howManyTimesIsPartAppearing[i + 1] != -1)
  211.                     writingStream << "+";
  212.             }
  213.         }
  214.         writingStream << std::endl;
  215.     }
  216.     writingStream.close();
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement