Hanaigi

Untitled

Nov 28th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <Windows.h>
  8.  
  9. using namespace std;
  10. int s_count = 0;
  11. const int M = 256;
  12. const int page_size = 20;
  13. int C[M][M];
  14. int codblock = 0;
  15. int simvB = 0;
  16. float leng = 0;
  17. int sumA = 0;
  18. int sumL = 0;
  19.  
  20. struct code
  21. {
  22.     char a;
  23.     float p;
  24.     float q;
  25.     int l;
  26. }
  27. A[M], B[166];
  28.  
  29. struct record1 {
  30.     char a[30];      
  31.     short int b;      
  32.     char c[22];      
  33.     char d[10];      
  34. };
  35.  
  36. struct vertex {
  37.     record1* data;
  38.     vertex* left;
  39.     vertex* right;
  40.     vertex* next;
  41.     int Bal;
  42. };
  43.  
  44. struct tLE {
  45.     record1 data;
  46.     int digit[4];    
  47.     tLE* next;
  48. };
  49.  
  50. struct queue {
  51.     tLE* head = nullptr;
  52.     tLE* tail = nullptr;
  53.  
  54.     void enqueue(tLE* r) {
  55.         tLE* newNode = new tLE();
  56.         *newNode = *r;
  57.         newNode->next = nullptr;
  58.         if (tail) {
  59.             tail->next = newNode;
  60.         }
  61.         tail = newNode;
  62.         if (!head) {
  63.             head = newNode;
  64.         }
  65.     }
  66.  
  67.     tLE* dequeue() {
  68.         if (!head) return nullptr;
  69.         tLE* temp = head;
  70.         head = head->next;
  71.         if (!head) {
  72.             tail = nullptr;
  73.         }
  74.         return temp;
  75.     }
  76.  
  77.     bool isEmpty() {
  78.         return head == nullptr;
  79.     }
  80. };
  81.  
  82. vertex* LL(vertex*& p) {
  83.     vertex* q = p->left;
  84.     p->Bal = 0;
  85.     q->Bal = 0;
  86.     p->left = q->right;
  87.     q->right = p;
  88.     return q;
  89. }
  90.  
  91. vertex* RR(vertex*& p) {
  92.     vertex* q = p->right;
  93.     p->Bal = 0;
  94.     q->Bal = 0;
  95.     p->right = q->left;
  96.     q->left = p;
  97.     return q;
  98. }
  99.  
  100. vertex* LR(vertex*& p) {
  101.     vertex* q = p->left;
  102.     vertex* r = q->right;
  103.  
  104.     if (r->Bal < 0) p->Bal = 1;
  105.     else p->Bal = 0;
  106.  
  107.     if (r->Bal > 0) q->Bal = -1;
  108.     else q->Bal = 0;
  109.  
  110.     r->Bal = 0;
  111.  
  112.     q->right = r->left;
  113.     p->left = r->right;
  114.     r->left = q;
  115.     r->right = p;
  116.  
  117.     return r;
  118. }
  119.  
  120. vertex* RL(vertex*& p) {
  121.     vertex* q = p->right;
  122.     vertex* r = q->left;
  123.  
  124.     if (r->Bal > 0) p->Bal = -1;
  125.     else p->Bal = 0;
  126.  
  127.     if (r->Bal < 0) q->Bal = 1;
  128.     else q->Bal = 0;
  129.  
  130.     r->Bal = 0;
  131.  
  132.     q->left = r->right;
  133.     p->right = r->left;
  134.     r->right = q;
  135.     r->left = p;
  136.  
  137.     return r;
  138. }
  139.  
  140. bool AVL(record1* D, vertex*& p, bool& Rost) {
  141.     if (p == NULL) {
  142.         p = new vertex;
  143.         p->data = D;
  144.         p->left = NULL;
  145.         p->right = NULL;
  146.         p->next = NULL;
  147.         p->Bal = 0;
  148.         Rost = true;
  149.     } else {
  150.         int cmp = strcmp(D->a, p->data->a);
  151.         if (cmp == 0) {
  152.             AVL(D, p->next, Rost);
  153.             Rost = false;  
  154.         }
  155.         else if (cmp < 0) {
  156.             AVL(D, p->left, Rost);
  157.             if (Rost) {
  158.                 if (p->Bal > 0) {
  159.                     p->Bal = 0;
  160.                     Rost = false;
  161.                 } else if (p->Bal == 0) {
  162.                     p->Bal = -1;
  163.                     Rost = true;
  164.                 } else {
  165.                     if (p->left->Bal < 0) {
  166.                         p = LL(p);
  167.                     } else {
  168.                         p = LR(p);
  169.                     }
  170.                     Rost = false;
  171.                 }
  172.             }
  173.         }
  174.         else if (cmp > 0) {
  175.             AVL(D, p->right, Rost);
  176.             if (Rost) {
  177.                 if (p->Bal < 0) {
  178.                     p->Bal = 0;
  179.                     Rost = false;
  180.                 } else if (p->Bal == 0) {
  181.                     p->Bal = 1;
  182.                     Rost = true;
  183.                 } else {
  184.                     if (p->right->Bal > 0) {
  185.                         p = RR(p);
  186.                     } else {
  187.                         p = RL(p);
  188.                     }
  189.                     Rost = false;
  190.                 }
  191.             }
  192.         }
  193.     }
  194.     return true;
  195. }
  196.  
  197.  
  198. vertex* tree_search(vertex *p, char x[], int size) {
  199.     while (p != nullptr) {
  200.         if (strncmp(x, p->data->a, size) < 0) {
  201.             p = p->left;
  202.         } else if (strncmp(x, p->data->a, size) > 0) {
  203.             p = p->right;
  204.         } else {
  205.             break;
  206.         }
  207.     }
  208.     return p;
  209. }
  210.  
  211. int size_tree = 0;
  212. void obhod_left(vertex *p) {
  213.     if (p != NULL) {
  214.         obhod_left(p->left);
  215.        
  216.         vertex *o = p;
  217.         while (o != NULL) {
  218.             cout << p->data->a << "\t"
  219.                  << p->data->c << "\t"
  220.                  << p->data->b << "\t"
  221.                  << p->data->d << "\t" << endl;
  222.             size_tree++;
  223.             o = o->next;
  224.         }
  225.        
  226.         obhod_left(p->right);
  227.     }
  228. }
  229.  
  230.  
  231. void convertDateToDigits(tLE* node) {
  232.     int year = (node->data.d[6] - '0') * 10 + (node->data.d[7] - '0');
  233.     int month = (node->data.d[3] - '0') * 10 + (node->data.d[4] - '0');
  234.     int day = (node->data.d[0] - '0') * 10 + (node->data.d[1] - '0');
  235.    
  236.     year += 1900;
  237.     int dateNum = year * 10000 + month * 100 + day;
  238.  
  239.     node->digit[0] = (dateNum >> 24) & 0xFF;
  240.     node->digit[1] = (dateNum >> 16) & 0xFF;
  241.     node->digit[2] = (dateNum >> 8) & 0xFF;
  242.     node->digit[3] = dateNum & 0xFF;
  243. }
  244.  
  245. void DigitalSort(tLE** head) {
  246.     int d, i, j;
  247.     tLE* p;
  248.     queue q[256];
  249.  
  250.     for (j = 3; j >= 0; j--) {
  251.         for (i = 0; i < 256; i++) {
  252.             q[i].head = nullptr;
  253.             q[i].tail = nullptr;
  254.         }
  255.  
  256.         p = *head;
  257.         while (p != nullptr) {
  258.             d = p->digit[j];
  259.             tLE* nextNode = p->next;
  260.             p->next = nullptr;
  261.             if (q[d].tail == nullptr) {
  262.                 q[d].head = p;
  263.             } else {
  264.                 q[d].tail->next = p;
  265.             }
  266.             q[d].tail = p;
  267.             p = nextNode;
  268.         }
  269.  
  270.         p = nullptr;
  271.  
  272.         for (i = 0; i < 256; i++) {
  273.             if (q[i].head != nullptr) {
  274.                 if (p == nullptr) {
  275.                     *head = q[i].head;
  276.                 } else {
  277.                     p->next = q[i].head;
  278.                 }
  279.                 p = q[i].tail;
  280.             }
  281.         }
  282.  
  283.         if (p != nullptr) {
  284.             p->next = nullptr;
  285.         }
  286.     }
  287. }
  288.  
  289. void displayRecords(tLE* head, int start, int count) {
  290.     tLE* current = head;
  291.     int i = 0;
  292.     while (current != nullptr && i < start) {
  293.         current = current->next;
  294.         i++;
  295.     }
  296.     i = 0;
  297.     while (current != nullptr && i < count) {
  298.         cout << current->data.a << "\t" << current->data.b << "\t"
  299.              << current->data.c << "\t" << current->data.d << endl;
  300.         current = current->next;
  301.         i++;
  302.     }
  303. }
  304.  
  305. int getTotalRecords(tLE* head) {
  306.     tLE* temp = head;
  307.     int totalRecords = 0;
  308.     while (temp != nullptr) {
  309.         totalRecords++;
  310.         temp = temp->next;
  311.     }
  312.     return totalRecords;
  313. }
  314.  
  315. queue BinarySearchByYear(tLE* head, const char* targetYear) {
  316.     int totalRecords = getTotalRecords(head);
  317.     tLE* nodes[totalRecords];
  318.     tLE* temp = head;
  319.  
  320.     for (int i = 0; i < totalRecords; i++) {
  321.         nodes[i] = temp;
  322.         temp = temp->next;
  323.     }
  324.  
  325.     int L = 0, R = totalRecords - 1, flag = -1, m;
  326.  
  327.     while (L < R) {
  328.         m = (L + R) / 2;
  329.         if (strncmp(nodes[m]->data.d + 6, targetYear, 2) < 0) {
  330.             L = m + 1;
  331.         } else {
  332.             R = m;
  333.         }
  334.     }
  335.  
  336.     if (strncmp(nodes[R]->data.d + 6, targetYear, 2) == 0) {
  337.         flag = R;
  338.     }
  339.  
  340.     queue recordQueue;
  341.  
  342.     if (flag == -1) {
  343.         cout << "No records found for year: " << (1900 + atoi(targetYear)) << endl;
  344.         return recordQueue;
  345.     }
  346.  
  347.     int i = flag;
  348.     cout << "Records found for year " << (1900 + atoi(targetYear)) << ":\n";
  349.    
  350.     int foundCount = 0;
  351.  
  352.     while (i < totalRecords && strncmp(nodes[i]->data.d + 6, targetYear, 2) == 0) {
  353.         cout << nodes[i]->data.a << "\t" << nodes[i]->data.b << "\t"
  354.              << nodes[i]->data.c << "\t" << nodes[i]->data.d << endl;
  355.  
  356.         recordQueue.enqueue(nodes[i]);
  357.         foundCount++;
  358.         i++;
  359.     }
  360.  
  361.     cout << "Total records found: " << foundCount << endl;
  362.  
  363.     return recordQueue;
  364. }
  365.  
  366.  
  367. vertex* buildAVLFromQueue(queue& q) {
  368.     vertex* root = nullptr;
  369.     bool Rost;
  370.  
  371.     while (!q.isEmpty()) {
  372.         tLE* node = q.dequeue();
  373.         AVL(&(node->data), root, Rost);
  374.     }
  375.  
  376.     return root;
  377. }
  378.  
  379. int sum = 0;
  380. float entropy = 0;
  381.  
  382. void ToNull()
  383. {
  384.     for(int i = 0; i < M; i++)
  385.     {
  386.         A[i].a = char(i-128);
  387.         A[i].p = 0;
  388.         A[i].q = 0;
  389.         A[i].l = 0;
  390.     }
  391.     sum = 0;
  392.     entropy = 0;
  393.     codblock = 0;
  394.     simvB = 0;
  395.     leng = 0;
  396.     sumA = 0;
  397.     sumL = 0;
  398.    
  399. }
  400.  
  401. void CreateP()
  402. {
  403.     for(int i = 0; i < M; i++)
  404.     {
  405.         A[i].p /= sum;
  406.         if(A[i].p > 0)
  407.         {
  408.             simvB += 1;
  409.             entropy += A[i].p * abs(log2(A[i].p));
  410.         }
  411.     }
  412. }
  413. void CreateB()
  414. {
  415.     int j = 0;
  416.     for(int i = 0; i < M; i++)
  417.     {
  418.         if(A[i].p > 0)
  419.         {
  420.             B[j] = A[i];
  421.             j++;
  422.         }
  423.     }
  424. }
  425.  
  426. void MoorCode()
  427. {
  428.     float pr = 0;
  429.     for(int i = 0; i < simvB; i++)
  430.     {
  431.         B[i].q = pr + B[i].p/2;
  432.         pr += B[i].p;
  433.         B[i].l = ceil(-log2(B[i].p)) + 1;
  434.     }
  435.     for(int i = 0; i < simvB; i++)
  436.     {
  437.         for(int j = 0; j < B[i].l; j++)
  438.         {
  439.             B[i].q *= 2;
  440.             C[i][j] = floor(B[i].q);
  441.             if(B[i].q > 1)
  442.             {
  443.                 B[i].q -= 1;
  444.             }  
  445.         }
  446.     }
  447. }
  448.  
  449. void CodePrepar()
  450. {
  451.     FILE* fp;
  452.     fp = fopen("testBase2.dat", "rb");
  453.     ToNull();
  454.    
  455.     while (!feof(fp))
  456.     {
  457.         char c;
  458.         fscanf(fp, "%c", &c);
  459.  
  460.         A[c + 128].p++;
  461.         sum++;
  462.     }
  463.     CreateP();
  464.     CreateB();
  465.     MoorCode();
  466.     fclose(fp);
  467. }
  468.  
  469. void codeOutput()
  470. {
  471.     int mode = 0;
  472.     while(mode != 1)
  473.     {
  474.         leng = 0;
  475.         cout << "Gilbert-Moore code" << endl;
  476.         cout << endl;
  477.         cout << "num" << "  " << "Symbol " << " " << "Probability" << " " << "Code word" << "   " << "Code word length" << endl;
  478.         float q = 0;
  479.         for (int i = 0; i < simvB; i++)
  480.         {
  481.             leng += B[i].p * B[i].l;
  482.             q += B[i].p;
  483.             cout << i+1 << "    " << B[i].a << "    "; printf("%.5f", B[i].p);
  484.             cout << "       ";
  485.             for(int j = 0; j < B[i].l; j++)
  486.             {
  487.                 cout << C[i][j];
  488.             }
  489.             if(B[i].l >= 8)
  490.             {
  491.                 cout << "\t\t";
  492.             }
  493.             else
  494.             {
  495.                 cout << "\t\t\t";
  496.             }
  497.             cout << B[i].l;
  498.             cout << endl;
  499.         }
  500.         cout << "\t" << "Sum frequencies = " << q << endl;
  501.         cout << endl;
  502.         cout << "Entropy:" << " " << "Average length:" << endl;
  503.         cout << entropy << "\t\t"; printf("%.5f", leng);
  504.         cout << endl;
  505.         cout << endl;
  506.         cout << "data compression ratio: " << (sumL / 8) * 100 / sumA << endl;
  507.         cout << endl;
  508.         cout << "Input command number: ";
  509.         cin >> mode;
  510.     }
  511. }
  512.  
  513. void codeDB()
  514. {
  515.     FILE *fp, *fc;
  516.     fp = fopen("testBase2.dat", "rb");
  517.     fc = fopen("codedBase2.dat", "wb");
  518.    
  519.     char c;
  520.     while(!feof(fp))
  521.     {
  522.         fscanf(fp, "%c", &c);
  523.         sumA++;
  524.         for(int i = 0; i < simvB; i++)
  525.         {
  526.             if(c == B[i].a)
  527.             {
  528.                 sumL += B[i].l;
  529.                 for(int j = 0; j < B[i].l; j++)
  530.                 {
  531.                     putc(C[i][j], fc);
  532.                 }
  533.             }
  534.         }
  535.     }
  536.    
  537.     fclose(fp);
  538.     fclose(fc);
  539. }
  540.  
  541. int main() {
  542.     system("chcp 866 > nul");
  543.     FILE* fp = fopen("testBase2.dat", "rb");
  544.  
  545.     if (!fp) {
  546.         cout << "Error opening file!" << endl;
  547.         return 1;
  548.     }
  549.  
  550.     record1 records[4000] = { 0 };
  551.     tLE* head = nullptr;
  552.     tLE* tail = nullptr;
  553.  
  554.     vertex* searchAVLRoot = nullptr;
  555.     bool Rost;
  556.  
  557.     int count = fread(records, sizeof(record1), 4000, fp);
  558.     fclose(fp);
  559.  
  560.     for (int i = 0; i < count; i++) {
  561.         tLE* node = new tLE();
  562.         node->data = records[i];
  563.         node->next = nullptr;
  564.         convertDateToDigits(node);
  565.  
  566.         if (head == nullptr) {
  567.             head = node;
  568.         } else {
  569.             tail->next = node;
  570.         }
  571.         tail = node;
  572.     }
  573.  
  574.     DigitalSort(&head);
  575.  
  576.     int start = 0;
  577.     const int linesPerPage = 20;
  578.     int totalRecords = getTotalRecords(head);
  579.     char command;
  580.  
  581.     while (true) {
  582.         system("cls");
  583.         displayRecords(head, start, linesPerPage);
  584.  
  585.         cout << "\nCommands: (N)ext page, (P)revious page, (L)ast page, (B)inary search by birth date, ";
  586.         cout << "(S)earch by FIO (AVL), (O)bhod LR, (C)ode, (E)xit\n";
  587.         cout << "Enter command: ";
  588.         cin >> command;
  589.  
  590.         if (command == 'N' || command == 'n') {
  591.             start += linesPerPage;
  592.             if (start >= totalRecords) start = totalRecords - linesPerPage;
  593.         } else if (command == 'P' || command == 'p') {
  594.             start -= linesPerPage;
  595.             if (start < 0) start = 0;
  596.         } else if (command == 'L' || command == 'l') {
  597.             start = totalRecords - linesPerPage;
  598.             if (start < 0) start = 0;
  599.         } else if (command == 'B' || command == 'b') {
  600.             cout << "Enter birth year (yy): ";
  601.             string year;
  602.             cin >> year;
  603.  
  604.             queue searchResultQueue = BinarySearchByYear(head, year.c_str());
  605.  
  606.             if (!searchResultQueue.isEmpty()) {
  607.                 searchAVLRoot = buildAVLFromQueue(searchResultQueue);
  608.             }
  609.  
  610.             cout << "\nPress any key to return to the main menu...";
  611.             cin.ignore();
  612.             cin.get();
  613.         } else if (command == 'S' || command == 's') {
  614.             cout << "Enter FIO for search: ";
  615.             char fullname[32];
  616.             string fio;
  617.             cin.ignore();
  618.             getline(cin, fio);
  619.             for (int i = 0; i < fio.length(); i++){
  620.                 fullname[i] = fio[i];
  621.             }
  622.             fullname[fio.length()] = '\0';
  623.             vertex* result = NULL;
  624.             result = tree_search(searchAVLRoot, fullname, fio.length());
  625.             if (result) {
  626.                 cout << "Found: " << endl;
  627.                 int i = 0;
  628.                 while (result != NULL){
  629.                     cout << i + 1 << "\t" << result->data->a << "\t" << result->data->b << "\t"
  630.                      << result->data->c << "\t" << result->data->d << endl;
  631.                     result = result->next;
  632.                     i++;
  633.                 }
  634.             } else {
  635.                 cout << "FIO not found.\n";
  636.             }
  637.  
  638.             cout << "\nPress any key to return to the main menu...";
  639.             cin.ignore();
  640.             cin.get();
  641.         } else if (command == 'O' || command == 'o') {
  642.             size_tree = 0;
  643.             if (searchAVLRoot) {
  644.                 cout << "\nLR Obhod:\n";
  645.                 obhod_left(searchAVLRoot);
  646.                 cout << "Total records displayed: " << size_tree << endl;
  647.             } else {
  648.                 cout << "No search result tree available. Perform a binary search first.\n";
  649.             }
  650.             cout << "\nPress any key to return to the main menu...";
  651.             cin.ignore();
  652.             cin.get();
  653.         } else if (command == 'C' || command == 'c'){
  654.             SetConsoleCP(866);
  655.                 if(codblock == 0)
  656.                 {
  657.                     CodePrepar();
  658.                     codeDB();
  659.                     codblock = 1;
  660.                 }
  661.                 codeOutput();
  662.         }else if (command == 'E' || command == 'e') {
  663.             break;
  664.         } else {
  665.             cout << "Invalid command!" << endl;
  666.         }
  667.     }
  668.  
  669.     tLE* current = head;
  670.     while (current != nullptr) {
  671.         tLE* next = current->next;
  672.         delete current;
  673.         current = next;
  674.     }
  675.  
  676.     return 0;
  677. }
  678.  
Add Comment
Please, Sign In to add comment