Skip to content

Commit fd32f55

Browse files
committed
First Commit
0 parents commit fd32f55

File tree

12 files changed

+521
-0
lines changed

12 files changed

+521
-0
lines changed

Bibliotecas/List

2.55 MB
Binary file not shown.

Bibliotecas/List.h

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# include <stdio.h>
2+
# include <stdlib.h>
3+
4+
typedef struct List{
5+
6+
short int num;
7+
struct List *next;
8+
9+
}List;
10+
11+
typedef struct CodeList{
12+
13+
short int* word;
14+
struct CodeList *next;
15+
16+
}CodeList;
17+
18+
typedef struct Code{
19+
20+
CodeList* code;
21+
int order;
22+
double numWord;
23+
int distMin;
24+
25+
}Code;
26+
27+
28+
CodeList* addListCode(CodeList* l, short int word[]){
29+
if(l == NULL){
30+
CodeList* newnode = (CodeList*)malloc(sizeof(CodeList));
31+
32+
newnode->word = word;
33+
newnode->next = NULL;
34+
35+
return newnode;
36+
}
37+
38+
l->next = addListCode(l->next,word);
39+
40+
return l;
41+
}
42+
43+
List* addListWord(List* l, short int num){
44+
if(l == NULL){
45+
List* newnode = (List*)malloc(sizeof(List));
46+
47+
newnode->num = num;
48+
newnode->next = NULL;
49+
50+
return newnode;
51+
}
52+
53+
l->next = addListWord(l->next,num);
54+
55+
return l;
56+
}
57+
58+
void rmvLastWord(Code* c){
59+
CodeList* aux = c->code;
60+
CodeList* aux_ant = NULL;
61+
62+
63+
while(aux->next!=NULL){ // caminha ate o final da lista;
64+
aux_ant = aux;
65+
aux = aux->next;
66+
}
67+
68+
if(aux_ant != NULL){ // nao eh o primeiro
69+
aux_ant->next = NULL;
70+
free(aux);
71+
}
72+
73+
else{ // eh o primeiro
74+
free(aux);
75+
c->code = NULL;
76+
}
77+
}
78+
79+
80+
81+
82+
void printList(List* l){
83+
if(l != NULL){
84+
printf("[%u]",l->num );
85+
printList(l->next);
86+
}
87+
}
88+
89+
double countNode(CodeList* l){
90+
if(l != NULL)
91+
return 1 + countNode(l->next);
92+
else
93+
return 0;
94+
}
95+
96+
void convertListtoArray(List* l, short int array[], int i){
97+
if(l != NULL){
98+
99+
array[i] = l->num;
100+
convertListtoArray(l->next,array,i+1);
101+
102+
}
103+
}
104+
105+
106+
107+
108+
List* freeList(List* l){
109+
if(l != NULL){
110+
List* aux;
111+
112+
while(l->next != NULL){
113+
aux = l->next;
114+
free(l);
115+
l = aux;
116+
117+
}
118+
119+
return NULL;
120+
121+
}
122+
123+
}
124+
125+
CodeList* freeCodeList(CodeList* l){
126+
if(l != NULL){
127+
CodeList* aux;
128+
129+
while(l->next != NULL){
130+
aux = l->next;
131+
free(l);
132+
l = aux;
133+
134+
}
135+
136+
return NULL;
137+
138+
}
139+
140+
}
141+
142+
143+

Bibliotecas/addword_codelib.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# include <stdlib.h>
2+
# include <stdlib.h>
3+
# include <time.h>
4+
5+
6+
void addRandomWord(Code* c, int numWord){
7+
8+
int o = c->order;
9+
CodeList *code = c->code;
10+
short int* newWord = NULL;
11+
int i,t,aux,dist = 0;
12+
13+
srand((unsigned)time(NULL)); //alimenta a semente
14+
15+
16+
17+
for (t= 0; t <numWord; t++){
18+
19+
newWord = (short int*)calloc(o,sizeof(short int));
20+
21+
aux = 1;
22+
23+
while(aux){
24+
25+
aux = 0;
26+
27+
for(i=0; i<o; i++)//cria palavra randomica
28+
newWord[i] = (rand()%2);
29+
30+
31+
while(code != NULL){
32+
33+
for(i=0; i<o; i++) //calcula a distancia da palavra com o enésima do codico
34+
dist += (code->word[i] + newWord[i])%2;
35+
36+
if(dist == 0 ){ // se a distancia for 0 significa q ela esta repetida
37+
aux = 1; // executa o laço novamente
38+
code = c->code;// volta ao inicio do codigo
39+
dist = 0;
40+
break;
41+
}
42+
dist = 0;
43+
44+
code = code->next;
45+
}
46+
}
47+
48+
c->code = addListCode(c->code,newWord); // adiciona palavra ao codigo;
49+
c->numWord++;
50+
51+
52+
53+
}
54+
55+
calcDistMin(c); // calcula nova distancia minima
56+
57+
}
58+
59+
Code* startCode(int order){
60+
61+
Code* c = (Code*)malloc(sizeof(Code));
62+
63+
c->code = NULL;
64+
c->order = order;
65+
c->numWord = 0;
66+
c->distMin = -1;
67+
68+
return c;
69+
}
70+
71+
void makeCodeDistMinLimited(Code* c, int distMin, int min){
72+
int boolean,i;
73+
74+
75+
76+
boolean = 1;
77+
78+
while(boolean){
79+
80+
addRandomWord(c,1);
81+
82+
if(c->distMin < distMin){
83+
rmvLastWord(c);
84+
c->numWord--;
85+
boolean = 0;
86+
}
87+
88+
89+
calcDistMin(c);
90+
91+
}
92+
93+
}
94+
95+
void shift(int* w, int n, int x){
96+
int last;
97+
int i,t;
98+
99+
for (t = 0; t < x; t++){
100+
101+
last = w[n-1];
102+
103+
for (i = n-1; i > 0 ; i--)
104+
w[i] = w[i-1];
105+
106+
w[0] = last;
107+
}
108+
109+
110+
}
111+
112+
long int pow2(int n){
113+
if(n != 0)
114+
return 2*pow2(n-1);
115+
else
116+
return 1;
117+
118+
}
119+
120+
long int binaryToDec(int* word, int n){
121+
long int num = 0;
122+
int i;
123+
n--;
124+
125+
for (i = 0; i < n; i++)
126+
num += word[i]*pow2(i);
127+
128+
return num;
129+
}

Bibliotecas/distmin_codelib

2.1 MB
Binary file not shown.

Bibliotecas/distmin_codelib.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Biblioteca com funçoes de calculo de distancia de Hamming*/
2+
# include <stdio.h>
3+
# include <stdlib.h>
4+
5+
6+
/*
7+
void calcDistMin(Code* code){
8+
9+
int dist = 0 ,distMin = code->order;
10+
int i;
11+
CodeList* w1 = code->code;
12+
CodeList* w2 = code->code;
13+
14+
while(w1 != NULL){
15+
while(w2 != NULL){
16+
17+
for (i = 0; i < code->order; i++)
18+
dist += (w1->word[i] + w2->word[i])%2;
19+
20+
if(dist < distMin && dist != 0)
21+
distMin = dist;
22+
23+
dist = 0;
24+
w2 = w2->next;
25+
}
26+
w2 = code->code;
27+
w1 = w1->next;
28+
}
29+
30+
31+
code->distMin = distMin;
32+
33+
34+
}
35+
*/
36+
37+
38+
void calcDistMin(Code* code){
39+
40+
int dist = 0 ,distMin = code->order;
41+
int i;
42+
CodeList* w1 = code->code;
43+
CodeList* w2 = code->code->next;
44+
45+
while(w1 != NULL){
46+
while(w2 != NULL){
47+
48+
for (i = 0; i < code->order; i++)
49+
dist += (w1->word[i] + w2->word[i])%2;
50+
51+
distMin = (dist < distMin && dist != 0)?dist:distMin;
52+
53+
dist = 0;
54+
w2 = w2->next;
55+
}
56+
w2 = w1->next;
57+
w1 = w1->next;
58+
}
59+
60+
61+
code->distMin = distMin;
62+
63+
64+
}

Bibliotecas/read_save_codelib

2.17 MB
Binary file not shown.

0 commit comments

Comments
 (0)