-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdados.h
More file actions
316 lines (236 loc) · 8.17 KB
/
dados.h
File metadata and controls
316 lines (236 loc) · 8.17 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <time.h>
#include <stdio.h>
#include <string.h> // funcao strncmp() referencia: http://cplusplus.com/reference/cstring/strncmp/?kw=strncmp
int *vetorInt; //Vetor auxiliar de entradas
//Declaracao das funcoes
void geraVetor(int vetor[],int tamanho,char tipoEntrada[]);
void entradaUserRandom(char tipo[]);
void lerEntradaUser();
void lerArquivo();
void salvaResultados(int vetor[], int tamanho, int tipo);
int randomNumeros(int vetor[],int tamanho);
int randomSemiOrdenado(int vetor[], int tamanho);
struct relatorio {
int quantidade;
char tipovetor[15];
float tMerge;
float tQuick;
float tShell;
int movMerge;
int movQuick;
int movShell;
int comparaMerge;
int comparaQuick;
int comparaShell;
};
void gerarRelatorio(relatorio relatorioDados);
void lerArquivo(){
FILE *arquivo;
arquivo = fopen("arquivo.txt","r");
if(arquivo==NULL){
printf("\nErro ao abrir arquivo!\n");
system("pause");
}else{
printf("\nArquivo Aberto.");
int qtdItens = 0;
//Conta quantidade de linhas
int n;
while(fscanf(arquivo,"%d\n",&n)==1){
qtdItens++;
printf("\n%d",n);
}
int vetorArquivo[qtdItens];
puts("\n-------");
int i =0;
while(!feof(arquivo)){
fscanf(arquivo,"%d\n",&vetorArquivo[i]);
printf("\n%d",vetorArquivo[i]);
i++;
}
printf("\nQuantidade: %d\n",qtdItens);
fclose(arquivo);
printf("\nArquivo Fechado.");
}
}
void lerEntradaUser(){
int tamanho;
printf("\nQuantidade de numeros: ");
scanf("%d",&tamanho);
int *nums;
geraVetor(nums,tamanho,"user");
}
void entradaUserRandom(char tipo[]){
int tamanho;
printf("Quantidade de numeros %s: ",tipo);
scanf("%d",&tamanho);
int *nums;
//encaminha tipo de geração de vetor
geraVetor(nums,tamanho,tipo);
}
void geraVetor(int vetor[],int tamanho, char tipoEntrada[]){
int tamanhoExibicao;
if(tamanho >300){
tamanhoExibicao = 300;
}else{
tamanhoExibicao = tamanho;
}
//Verifica se é entrada por usuario ou randomico
if(strncmp(tipoEntrada,"randomicos",11)==0){
vetor = (int* )malloc(tamanho * sizeof(int));
randomNumeros(vetor,tamanho);
printf("RESULTADO DE VALORES RANDOMICOS - tamanho vetor: %d",tamanho);
}else if(strncmp(tipoEntrada,"semi ordenados",15)==0){
vetor = (int* )malloc(tamanho * sizeof(int));
randomSemiOrdenado(vetor,tamanho);
printf("RESULTADO DE VALORES SEMI ORDENADOS - tamanho vetor: %d",tamanho);
}else if(strncmp(tipoEntrada,"ordenados",10)==0){
vetor = (int* )malloc(tamanho * sizeof(int));
randomNumeros(vetor,tamanho);
quickSort(vetor,0,tamanho-1);
printf("RESULTADO DE VALORES JA ORDENADOS - tamanho vetor: %d",tamanho);
}else if(tipoEntrada == "user"){
vetor = (int* )malloc(tamanho * sizeof(int));
for(int i=0;i<tamanho;i++){
printf("\nNumero: ");
scanf("%d",&vetor[i]);
}
system("cls");
printf("RESULTADO DE ENTRADA PELO USUARIO - tamanho vetor: %d",tamanho);
}
//declara variaveis de tempo
clock_t tMerge,tQuick,tShell;
//variaveis de comparacoes
int comparaMerge,comparaQuick,comparaShell;
//variaveis de movimentacoes
int movMerge,movQuick,movShell;
//declara vetores auxiliares
int vMerge[tamanho];
int vQuick[tamanho];
int vShell[tamanho];
//Copia o vetor para os vetores auxiliares
for(int i=0;i<tamanho;i++){
vMerge[i] = vetor[i];
vQuick[i] = vetor[i];
vShell[i] = vetor[i];
}
//limpa o buffer
setbuf(stdin,NULL);
comparacoes = 0;
movimentacoes = 0;
//Marca os tempos e chama os métodos de ordenação
tMerge = clock();
mergeSort(vMerge,tamanho);
tMerge = clock() - tMerge;
comparaMerge = comparacoes;
movMerge = movimentacoes;
comparacoes = 0;
movimentacoes = 0;
tQuick = clock();
quickSort(vQuick,0,tamanho-1);
tQuick = clock() - tQuick;
comparaQuick = comparacoes;
movQuick = movimentacoes;
comparacoes=0;
movimentacoes=0;
tShell = clock();
shellSort(vShell,tamanho);
tShell = clock() - tShell;
comparaShell = comparacoes;
movShell = movimentacoes;
comparacoes = 0;
movimentacoes = 0;
float tempoMerge = ((double)tMerge/CLOCKS_PER_SEC);
float tempoQuick = ((double)tQuick/CLOCKS_PER_SEC);
float tempoShell = ((double)tShell/CLOCKS_PER_SEC);
//Formata e exibe saída
printf("\n-----------------------------------------------------------");
printf("\nDesordenado Merge Sort Quick Sort Shell Sort");
for(int i=0;i<tamanhoExibicao;i++){
printf("\n%11d%16d%16d%16d",vetor[i],vMerge[i],vQuick[i],vShell[i]);
}
salvaResultados(vMerge, tamanho, 1);
salvaResultados(vQuick, tamanho, 2);
salvaResultados(vShell, tamanho, 3);
printf("\n-----------------------------------------------------------");
printf("\nTempos de\nordenacao:%16fs%15fs%15fs", tempoMerge, tempoQuick, tempoShell);
printf("\nComparacoes:%15d%16d%16d",comparaMerge,comparaQuick,comparaShell);
printf("\nMovimentacoes:%13d%16d%16d",movMerge,movQuick,movShell);
printf("\n\nObs: O tempo de ordenacao pode n aparecer devido\nao metodo estar preparado para lidar com\nvetores pequenos ou grandes.\n");
//Funcionalidade de gerar um arquivo com os resultados
//Perguntar se deseja exibir mais resultados
// Colocando Dados no Relatorio
relatorio relatorioDados;
relatorioDados.quantidade = tamanho;
strcpy(relatorioDados.tipovetor, tipoEntrada);
relatorioDados.tMerge = tempoMerge;
relatorioDados.tQuick = tempoQuick;
relatorioDados.tShell = tempoShell;
relatorioDados.movMerge = movMerge;
relatorioDados.movQuick = movQuick;
relatorioDados.movShell = movShell;
relatorioDados.comparaMerge = comparaMerge;
relatorioDados.comparaQuick = comparaQuick;
relatorioDados.comparaShell = comparaShell;
gerarRelatorio(relatorioDados);
free(vetor);
}
void gerarRelatorio(relatorio relatorioDados) {
FILE *arquivo;
arquivo = fopen("report/dataReport.json","w");
fprintf(arquivo, "dataReport = {\n");
fprintf(arquivo, "quantidade: %d,\n", relatorioDados.quantidade);
fprintf(arquivo, "tipovetor: \"%s\",\n", relatorioDados.tipovetor);
fprintf(arquivo, "tempos: {'tempoMerge': %f, 'tempoQuick': %f, 'tempoShell': %f},\n ", relatorioDados.tMerge, relatorioDados.tQuick, relatorioDados.tShell);
fprintf(arquivo, "movimentacoes: { 'movMerge': %d, 'movQuick': %d, 'movShell': %d},\n", relatorioDados.movMerge, relatorioDados.movQuick, relatorioDados.movShell);
fprintf(arquivo, "comparacoes: { 'compMerge': %d, 'compQuick': %d, 'compShell': %d}\n ", relatorioDados.comparaMerge, relatorioDados.comparaQuick, relatorioDados.comparaShell);
fprintf(arquivo, "}");
fclose(arquivo);
}
void salvaResultados(int vetor[], int tamanho, int tipo){
if(tipo == 1){
FILE *merge;
merge = fopen("arquivos/merge.txt","w");
int i;
for(i = 0; i < tamanho;i++){
fprintf(merge,"\n%d",vetor[i]);
}
fclose(merge);
}
if(tipo == 2){
FILE *quick;
quick = fopen("arquivos/quick.txt","w");
int i;
for(i = 0; i < tamanho;i++){
fprintf(quick,"\n%d",vetor[i]);
}
fclose(quick);
} else{
FILE *shell;
shell = fopen("arquivos/shell.txt","w");
int i;
for(i = 0; i < tamanho;i++){
fprintf(shell,"\n%d",vetor[i]);
}
fclose(shell);
}
}
int randomNumeros(int vetor[], int tamanho){
srand((unsigned)time(NULL));
for(int i=0;i<tamanho;i++){
vetor[i] = rand()%1000 +1; // de 1 a 100
}
}
int randomSemiOrdenado(int vetor[], int tamanho){
srand((unsigned)time(NULL));
int parte = 0;
int aux = 0;
for(int i=0;i<tamanho;i++){
if((i/10)!=parte){
aux +=10;
vetor[i] = rand()%10 + (aux+1); //Valores de aux+1 a aux+10
parte = i/10;
}else{
vetor[i] = rand()%10 + (aux+1); //Valores de aux+1 a aux+10
}
}
}