Advertisement
Guest User

asd

a guest
Feb 14th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <cmath>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. double H1(int *A, int b ,const string& s) {
  10.     double a = 0 ;
  11.     for (int i = 0; i < b; ++i) {
  12.         a -= (A[i] != 0 ? double(A[i])/ double(s.size()) * (log2(A[i]/double(s.size())))  : double(0));
  13.     }
  14.     return a;
  15. }
  16.  
  17. string remove_spaces(const string &s) {
  18.     string res;
  19.     for (int i = 0; i < s.size(); ++i) {
  20.         if (isalpha(s[i])) {
  21.             res += tolower(s[i]);
  22.         }
  23.     }
  24.     return res;
  25. }
  26.  
  27. int encode(char a, char b) {
  28.     return (a - 'а') + (b - 'а') * 32;
  29. }
  30.  
  31. int getbigram(const string &s, int p) {
  32.     try {
  33.         return encode(s[p], s[p + 1]);
  34.     } catch (...) {
  35.         return 0;
  36.     }
  37. }
  38.  
  39.  
  40.  
  41. int* getfreq(const string &_str, int step) {
  42.     string str = remove_spaces(_str);
  43.     int *A = new int[32 * 32];
  44.     for (int i = 0; i < 32 * 32; ++i) {
  45.         A[i] = 0;
  46.     }
  47.     for (int i = 0; i < str.size() - 1; i += step) {
  48.         ++A[getbigram(str, i)];
  49.     }
  50.     return A;
  51. }
  52.  
  53. void printcell(double n) {
  54.     printf("%1.3f|", n);
  55. }
  56. void printcell(char c) {
  57.     cout << "  " << c << "  |";
  58. }
  59.  
  60. void printstick(int n = 199) {
  61.     cout << string(n, '-') << "\n";
  62. }
  63. void printrow(int n, const int *A, int s) {
  64.     cout << "|";
  65.     printcell(char('а' + n));
  66.     for (int i = 0; i < 32; ++i) {
  67.         printcell(A[n + i * 32] / double(s));
  68.     }
  69.     cout << "\n";
  70.     printstick();
  71. }
  72. void printhead() {
  73.     printstick();
  74.     cout << "|     |";
  75.     for (int i = 0; i < 32; ++i) {
  76.         printcell(char('а' + i));
  77.     }
  78.     cout << "\n";
  79.     printstick();
  80. }
  81.  
  82. int sum(int *A, int n) {
  83.     int r = 0;
  84.     for (int i = 0; i < n; ++i) {
  85.         r += A[i];
  86.     }
  87.     return r;
  88. }
  89.  
  90. void print_table(const string &str, int step) {
  91.     int *A = getfreq(str, step),
  92.         s = sum(A, 1024);
  93.     printhead();
  94.     for (int i = 0; i < 32; ++i) {
  95.         printrow(i, A, s);
  96.     }
  97.     cout << "\n";
  98. }
  99.  
  100. int main() {
  101.  
  102.     setlocale(LC_CTYPE, "rus");
  103.  
  104.     string s = "апоаыеждрш иьвэадкщлиот эдлкыотрэкдвщлорнлаиотывждларот";
  105.  
  106.     print_table(s, 1);
  107.     print_table(s, 2);
  108.  
  109.     int delta = 'я' - 'а' + 2;
  110.     int *A = new int[delta],
  111.         *B = new int[delta];
  112.     for (int i = 0; i < delta; ++i) {
  113.         A[i] = 0;
  114.         B[i] = i;
  115.     }
  116.     for (int i = 0; i < s.size(); ++i) {
  117.         ++A[s[i] == ' ' ? delta - 1   //  индекс последнего элемента массива
  118.                         : s[i] - 'а'];
  119.     }
  120.  
  121.     for (int i = 0; i < delta; ++i) {
  122.         for (int j = i + 1; j < delta; ++j) {
  123.             if (A[i] < A[j]) {
  124.                 swap(A[i], A[j]);
  125.                 swap(B[i], B[j]);
  126.             }
  127.         }
  128.     }
  129.  
  130.     int total = sum(A, delta);
  131.  
  132.     printstick();
  133.     cout << "|";
  134.     for (int i = 0; i < delta; ++i) {
  135.         printcell(B[i] == delta - 1 ? ' ' : char('а' + B[i]));
  136.     }
  137.     cout << "\n";
  138.     printstick();
  139.     cout << "|";
  140.     for (int i = 0; i < delta; ++i) {
  141.         printcell(A[i] / double(total));
  142.     }
  143.     cout << "\n";
  144.     printstick();
  145.  
  146.     cout << "\n" << H1(A, delta,s) << "\n";
  147.  
  148.     delete[] A;
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement