[Algorithm] Optimal Coding – The encryption method Shanon
Content
Shanon encryption algorithm
Code algorithm
In this article I just mentioned Shanon and encryption method code C / C ++ algorithms. All related concepts default you know, if not clear concept you call big brother Google offline. ^^
Shanon encryption algorithm
Considering the source U with probability distribution table
The1 | The2 | … | TheN |
P1 | P2 | … | PN |
Step 1: Sort items in descending order according to the probability
Step 2: Calculate the length of message encryption code Uin according to the formula:
From this formula we change the formula is easier:
Step 3: calculated F(Thein) according to the formula:
Step 4: Switch F(Thein) from step 3 into binary
Step 5: Get nin The following characters are decimal calculation of F(Thein) It was the code word uin
Example: With power on a string nguyenvanquan7826. After performing the steps above have the following results:
These are the results of running the program below. In the program, P and F its calculation constant right up to its decimal form but then when calculating fractions nin New move to decimal to more accurate results.
Code algorithm
//============================================================================ // Name : Shanon.cpp // Author : nguyenvanquan7826 //============================================================================ #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> typedef struct { char s1; int ni, P, F, b[100]; } nguon; void doicho(nguon *a, nguon *b) { nguon c; c = *a; *a = *b; *b = c; } int main() { char *s; nguon *kitu; int i, j, n, k = 1, *a, ni, maxni; s = (char *) malloc(100 * sizeof(char)); a = (int *) malloc(100 * sizeof(int)); kitu = (nguon *) malloc(100 * sizeof(nguon)); printf("Moi ban nhap day nguon: "); gets(s); fflush(stdin); puts(s); n = strlen(s); //Phan loai ki tu va tinh P(Ui) for (j = 1; j <= 256; j++) { a[k] = 0; for (i = 0; i <= n; i++) if ((int) s[i] == j) { a[k]++; kitu[k].s1 = s[i]; } if (a[k] > 0) { kitu[k].P = a[k]; k++; } } for (i = 1; i < k; i++) for (j = i; j < k; j++) if (kitu[i].P < kitu[j].P) doicho(&kitu[i], &kitu[j]); //Tinh F(Ui) for (i = 1; i <= k; i++) { if (i == 1) kitu[1].F = 0; if (i > 1) kitu[i].F = kitu[i - 1].F + kitu[i - 1].P; } //Tinh ni for (i = 1; i < k; i++) { float j; j = n / kitu[i].P; ni = (int) (log(j) / log(2)) + 1; kitu[i].ni = ni; } maxni = kitu[1].ni; for (i = 1; i < k; i++) if (kitu[i].ni > maxni) maxni = kitu[i].ni; //Tinh F(Ui) he 2 int l; float F; for (i = 1; i < k; i++) { l = 1; F = (float) kitu[i].F / n; while ((l <= maxni) && (F != 0)) { F = F * 2; kitu[i].b[l] = (int) F / 1; l++; F = F - (int) F; } } printf("\n\n%5s %10s %10s %7s", "Ui", "P(Ui)", "F(Ui)", "ni"); for (i = 1; i <= maxni + 1; i++) printf(" "); printf("%9s", "F(Ui)2"); for (i = 1; i <= maxni; i++) printf(" "); printf("%10s", "Ma"); for (i = 1; i < k; i++) { ni = kitu[i].ni; printf("\n%5c %7d/%d %7d/%d %7d", kitu[i].s1, kitu[i].P, n, kitu[i].F, n, kitu[i].ni); for (j = 1; j <= maxni - ni; j++) printf(" "); printf("%10s", "0."); for (j = 1; j <= ni; j++) printf("%d", kitu[i].b[j]); for (j = 1; j <= maxni - ni; j++) printf(" "); printf("%10s", " "); for (j = 1; j <= ni; j++) printf("%d", kitu[i].b[j]); } printf("\n"); return 0; }
đoan code chạy bị lỗi bạn ơi
Errors do you?
Lúc mình chạy thì bị lỗi, lúc nhập vào bắt thoát ra. Kiểu như bị vòng lặp vô hạn hay sao đấy. Mong b chỉnh sửa lại
Thank you, mình đã sửa lại code rồi nhé. 🙂