fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <curses.h>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int search(int); // FUNCTION (CONSEC_NUM) ---------> INDEX
  10. long sum(int[], int[], int);
  11. void display(); // DISPLAY STUFF AS PROGRAM RUNS
  12. void reorder();
  13.  
  14.  
  15. long bits = 0; // KEEP TRACK OF TOTAL BITS READ
  16. bool match;
  17. int consec = 0, alt = 0;
  18. char bit, bit_prev;
  19. int r[2][200], n[2][200], MAX[2];
  20.  
  21. int main()
  22. {
  23. ifstream in("/home/cups/randombin.txt");
  24.  
  25.  
  26.  
  27.  
  28.  
  29. bit_prev = in.get(); // GRAB INITIAL BIT
  30. consec = 1; // SET CONSEC NUMBER (for bit type of initial bit)
  31.  
  32. while (in.good()){
  33. bit = in.get(); // GRAB NEXT BIT
  34. match = (bit == bit_prev); // CURRENT BIT == PREVIOUS BIT ?
  35. if (match){ // IF: YES
  36. consec++; //-----> INCREMENT CONSEC NUMBER
  37. }
  38. else{ // IF: NO
  39. int bit_type = bit_prev - '0'; //
  40. int index = search(bit_type); //-----> SEE IF THERE ALREADY IS A RECORDED INSTANCE OF CONSEC NUMBER OF BIT_TYPE
  41.  
  42. n[bit_type][index]++; //-----> INCREMENT THE NUMBER OF INSTANCES WE ENCOUNTER CONSEC NUMBER (identified by index for BIT_TYPE)
  43. alt++; //-----> INCREMENT NUMBER OF BIT FLIP ALTERNATIONS (program only enters else block when there's a bit flip)
  44. consec = 1; //-----> ADJUST CONSEC NUMBER FOR THE FIRST FLIPPED BIT
  45. }
  46. bit_prev = bit;
  47.  
  48.  
  49. bits++; // KEEP TRACK OF TOTAL NUMBER OF BITS
  50. }
  51.  
  52.  
  53. in.close(); // FINISHED READING FILE
  54.  
  55.  
  56.  
  57.  
  58. long tot_0 = sum(r[0], n[0], MAX[0]); // GET SUM OF 0_bits
  59. long tot_1 = sum(r[1], n[1], MAX[1]); // GET SUM OF 1_bits
  60.  
  61. system("clear");
  62.  
  63.  
  64. int *tmp = new int[MAX[0]];
  65. for (int y = 0; y <= MAX[0]; y++){
  66. tmp[y] = 1;
  67. }
  68. int spots_0 = sum(tmp, n[0], MAX[0]);
  69.  
  70. cout << spots_0 << endl;
  71.  
  72. delete[] tmp;
  73.  
  74. tmp = new int[MAX[1]];
  75. for (int y = 0; y <= MAX[1]; y++){
  76. tmp[y] = 1;
  77. }
  78. int spots_1 = sum(tmp, n[1], MAX[1]);
  79.  
  80.  
  81. cout << tot_0 << endl << tot_1 << endl << spots_1;
  82. cin.get();
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. return 0;
  91. }
  92.  
  93.  
  94. int search(int bit_type){ // SEARCH FOR A CONSEC NUMBER (of type BIT_TYPE) TO SEE IF ALREADY ENCOUNTERED
  95.  
  96. for (int i = 1; i <= MAX[bit_type]; i++){ //GO THRU ALL ENCOUNTERED CONSEC NUMBERS SO FAR (for type BIT_TYPE)
  97. if (consec == r[bit_type][i]) // IF: FOUND
  98. return i; // -----> RETURN INDEX OF RECORDED CONSEC_NUM
  99. }
  100. // IF: NOT FOUND
  101. r[bit_type][++MAX[bit_type]] = consec; // -----> INCREMENT MAX[bit_type] & RECORD NEW CONSEC_NUM -------> ARRAY[MAX]
  102. n[bit_type][MAX[bit_type]] = 1;
  103. return(MAX[bit_prev]); // -----> RETURN THE NEWLY FILLED INDEX
  104. }
  105.  
  106.  
  107. long sum(int arr_r[], int arr_n[], int limit){
  108.  
  109. long tot = 0;
  110. for (int i = 1; i <= limit; i++){
  111. tot += (arr_r[i])*(arr_n[i]);
  112. }
  113. return tot;
  114. }
  115.  
  116.  
  117.  
  118.  
  119. void display(){
  120. system("cls");
  121. cout << "bit: " << bit << "\tbool: " << match << "\tconsec: " << consec;
  122. cout << "\n\nMAX[0]: " << MAX[0] << "\t MAX[1]: " << MAX[1];
  123. cout << "\n\n 0\t\t 1\t\t\t\t\t\t\tbits: " << bits << "\n";
  124.  
  125. int j = 1;
  126. int bigger;
  127. if (MAX[0] < MAX[1])
  128. bigger = MAX[1];
  129. else
  130. bigger = MAX[0];
  131.  
  132. //reorder();
  133.  
  134. do{
  135. if (j <= MAX[0])
  136. cout << "\nr[" << j << "]: " << r[0][j] << " (" << n[0][j] << ")";
  137.  
  138. if (j <= MAX[1])
  139. cout << "\tr[" << j << "]: " << r[1][j] << " (" << n[1][j] << ")";
  140. cout << endl;
  141.  
  142. j++;
  143. } while (j <= bigger);
  144.  
  145. }
  146.  
Success #stdin #stdout #stderr 0s 3468KB
stdin
Standard input is empty
stdout
0
0
0
0
stderr
TERM environment variable not set.