-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinSearch.c
190 lines (163 loc) · 5.79 KB
/
binSearch.c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "binSearch.h"
void lerEPreencher(FILE *file, Candidato vetor[] ){
char linha[252];
char estado[3];
char cidade[50];
char numeroUrna[6];
char cargo[13];
char nome[50];
char nomeNaUrna[50];
char partido[20];
char genero[10];
char escolaridade[35];
char raca[15];
int i = 0;
//Tiramos a primeira linha q noa tem conteudo
if(i == 0) fgets(linha, sizeof(linha), file);
while(fgets(linha, sizeof(linha), file) != NULL) {
strcpy(estado,strtok(linha,";"));
strcpy(cidade,strtok(NULL,";"));
strcpy(numeroUrna,strtok(NULL,";"));
strcpy(cargo, strtok(NULL,";"));
strcpy(nome, strtok(NULL,";"));
strcpy(nomeNaUrna, strtok(NULL,";"));
strcpy(partido, strtok(NULL,";"));
strcpy(genero, strtok(NULL,";"));
strcpy(escolaridade, strtok(NULL,";"));
strcpy(raca, strtok(NULL,";"));
strcpy(vetor[i].estado,estado);
strcpy(vetor[i].cidade,cidade);
strcpy(vetor[i].numeroUrna,numeroUrna);
strcpy(vetor[i].cargo, cargo );
strcpy(vetor[i].nome, nome);
strcpy(vetor[i].nomeNaUrna, nomeNaUrna);
strcpy(vetor[i].partido, partido);
strcpy(vetor[i].genero, genero);
strcpy(vetor[i].escolaridade, escolaridade);
strcpy(vetor[i].raca, raca);
i++;
}
fclose(file);
}
void shellSort(Candidato vet[], int n){
int i, j, h = 1;
Candidato aux;
do{
h = h * 3 + 1;
}while(h < n);
do{
h /= 3;
for(i = h; i < n; i++){
aux = vet[i];
j = i;
while(j >= h && maiorCand(vet[j - h], aux) == 1){
vet[j] = vet[j - h];
j -= h;
}
vet[j] = aux;
}
}while(h > 1);
}
int maiorCand(Candidato a, Candidato b){ // a > b = 1 ; a < b = 0 ; a == b = 2
int cmp;
cmp = strcmp(a.estado, b.estado);
if (cmp < 0) return 0;
else if (cmp > 0) return 1;
else {
cmp = strcmp(a.cidade, b.cidade);
if (cmp < 0) return 0;
else if (cmp > 0) return 1;
else{
cmp = strcmp(a.numeroUrna, b.numeroUrna);
if (cmp < 0) return 0;
else if (cmp > 0) return 1;
}
}
return 2;
}
int binSearch_Estado(Candidato vetor[], int inicio, int fim, char *estado, int chave){
int i = (inicio + fim) / 2;
if (inicio > fim )
return -1;
if (strcmp(vetor[i].estado, estado) == 0){
int k = i;
while(strcmp(vetor[k].estado,vetor[i].estado) == 0) k--;
k++;
if(chave == 1){
return k;
}
while(strcmp(vetor[k].estado,vetor[i].estado) == 0){
imprimeCandidato(vetor[k]);
k++;
}
return 0;
}
if (strcmp(vetor[i].estado, estado) < 0)
return binSearch_Estado(vetor, i + 1, fim, estado, chave);
else
return binSearch_Estado(vetor, inicio, i - 1, estado, chave);
}
int binSearch_Cidade(Candidato vetor[], int inicio, int fim, char *estado, char *cidade, int chave){
int i = (inicio + fim) / 2;
if (inicio > fim )
return -1;
if (strcmp(vetor[i].cidade, cidade) == 0){
int k = i;
while(strcmp(vetor[k].cidade,cidade) == 0) k--;
k++;
if(chave == 1){
return k;
}
while(!strcmp(vetor[k].cidade, cidade)){
imprimeCandidato(vetor[k]);
k++;
}
}
else if (strcmp(vetor[i].cidade, cidade) < 0)
return binSearch_Cidade(vetor, i + 1, fim, estado, cidade, chave);
else
return binSearch_Cidade(vetor, inicio, i - 1, estado, cidade, chave);
return 0;
}
int binSearch_Candidato(Candidato vetor[], int inicio, int fim, char *estado, char *cidade, char *numeroUrna){
int i = (inicio + fim) / 2;
if (inicio > fim) return -1;
if (strcmp(vetor[i].numeroUrna, numeroUrna) == 0){
imprimeCandidato(vetor[i]);
return 0;
}
if (strcmp(vetor[i].numeroUrna, numeroUrna) < 0)
return binSearch_Candidato(vetor, i + 1, fim, estado, cidade, numeroUrna);
else
return binSearch_Candidato(vetor, inicio, i - 1, estado, cidade, numeroUrna);
}
int imprimeCandidato(Candidato candidato){
printf("\nUF: %s\nCidade: %s\nNumero de Urna: %s\nCargo: %s\nNome:%s\nNome na urna: %s\nPartido: %s\nGenero: %s\nEscolaridade: %s\n"
"Raca/etnia: %s\n", candidato.estado,candidato.cidade, candidato.numeroUrna, candidato.cargo, candidato.nome, candidato.nomeNaUrna,
candidato.partido, candidato.genero, candidato.escolaridade, candidato.raca);
return 0;
}
int compFiltroBin(Candidato vetor[], int pos, char *filtro, int chave){
if(chave == 1) return strcmp(vetor[pos].genero, filtro);
else if(chave == 2) return strcmp(vetor[pos].partido, filtro);
else if(chave == 3) return strcmp(vetor[pos].raca, filtro);
}
void imprimeEstadoBin_filtro(Candidato vetor[], char *estado, char *filtro, int MAX, int chave){
int k = binSearch_Estado(vetor, 0, MAX, estado, 1);
while(!strcmp(vetor[k].estado, estado)){
if(!compFiltroBin(vetor, k, filtro, chave))
imprimeCandidato(vetor[k]);
k++;
}
}
void imprimeCidadeBin_filtro(Candidato vetor[],char *estado, char *cidade, char *filtro, int MAX, int chave){
int inicio = binSearch_Estado(vetor, 0, MAX, estado, 1);
int fim = inicio;
while(!strcmp(vetor[fim].estado, estado)) fim++;
int k = binSearch_Cidade(vetor, inicio, fim,estado, cidade, 1);
while(!strcmp(vetor[k].cidade, cidade)){
if(!compFiltroBin(vetor, k, filtro, chave))
imprimeCandidato(vetor[k]);
k++;
}
}