Advertisement
Guest User

Untitled

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