-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
102 lines (84 loc) · 3.05 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* File: main.cpp
* Author: matyzhonok
*
* Created on May 29, 2013, 12:19 AM
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string.h>
#include <string>
#include <iomanip>
#include "Combination.h"
// generator = {10001000000100001};
const char* strToGenerator(const char* str) {
char* generator = new char[strlen(str)];
u_int64_t i = 0;
while (*str) {
if (*str != '0')
generator[i] = 1;
else
generator[i] = 0;
++i;
++str;
}
return generator;
}
u_int64_t factorial(u_int64_t n) {
u_int64_t fact = 1;
for (u_int64_t i = 1; i <= n; ++i)
fact *= i;
return fact;
}
u_int64_t combinationsNumber(u_int64_t selection_size, u_int64_t total_count) {
if (selection_size > (total_count - selection_size))
selection_size = (total_count - selection_size);
u_int64_t numerator = 1;
for (int i = 0; i < selection_size; ++i) {
numerator *= total_count;
--total_count;
}
return (numerator / factorial(selection_size));
}
int main(int argc, char** argv) {
u_int64_t Combination_len;
u_int64_t Generator_len;
const char* generator;
if (argc == 3) {
Combination_len = atoi(argv[1]);
Generator_len = strlen(argv[2]);
generator = strToGenerator(argv[2]);
} else {
std::cout << "Please enter total code length: ";
std::cin >> Combination_len;
std::string raw_generator;
std::cout << "Please enter generator polynomial: ";
std::cin >> raw_generator;
Generator_len = raw_generator.size();
generator = strToGenerator(raw_generator.c_str());
}
u_int64_t Information_len = Combination_len - Generator_len + 1;
u_int64_t Combinations_num = 1 << (Information_len);
Combination base[Information_len];
for (u_int64_t i = 0; i < Information_len; ++i) {
base[i] = MakeBaseCombination(Information_len - i - 1, Combination_len, generator, Generator_len);
//std::cout << "base[" << i + 1 << "] = " << base[i] << std::endl;
}
u_int64_t combinationWeightCounter[Combination_len];
for (int i = 0; i < Combination_len; ++i)
combinationWeightCounter[i] = 0;
for (u_int64_t i = 0; i < Combinations_num; ++i)
++combinationWeightCounter[GenerateComplexCombination(i, base).Count1() - 1];
const int r1=20;
const int r2=20;
const int r3=20;
const int r4=30;
std::cout << std::setw(r1) << "Kratnost Oshibki" << std::setw(r2) << "Neobnarujennix" << std::setw(r3) << "Vsego vozmojno" << std::setw(r4) << "Verojatnost obnarujenija" << std::endl;
for (u_int64_t i = 1; i <= Combination_len; ++i) {
u_int64_t undetected_errors = combinationWeightCounter[i -1];
u_int64_t total_possibly_errors = combinationsNumber(i, Combination_len);
std::cout << std::setw(r1) << i << std::setw(r2) << undetected_errors << std::setw(r3) << total_possibly_errors << std::setw(r4) << 1- float(undetected_errors) / total_possibly_errors << std::endl;
}
return 0;
}