66 lines
1.7 KiB
C
66 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "dicionario.h"
|
|
|
|
const int MAX_WORDS = 100;
|
|
const int MAX_WORD_SIZE = 50;
|
|
|
|
int main() {
|
|
FILE *input_file, *output_file;
|
|
char word[MAX_WORD_SIZE];
|
|
char **dict;
|
|
int dict_size = 0;
|
|
|
|
// Abre o arquivo de entrada
|
|
input_file = fopen("texto.txt", "r");
|
|
if (input_file == NULL) {
|
|
printf("Erro ao abrir arquivo de entrada\n");
|
|
exit(1);
|
|
}
|
|
|
|
// Aloca memória para o dicionário de palavras
|
|
dict = (char**) malloc(MAX_WORDS * sizeof(char*));
|
|
for (int i = 0; i < MAX_WORDS; i++) {
|
|
dict[i] = (char*) malloc(MAX_WORD_SIZE * sizeof(char));
|
|
}
|
|
|
|
// Lê as palavras do arquivo de entrada e insere no dicionário
|
|
while (fscanf(input_file, "%s", word) != EOF) {
|
|
to_lowercase(word);
|
|
if (strcmp(word, ".") == 0) {
|
|
break;
|
|
}
|
|
if (binary_search(dict, 0, dict_size-1, word) == -1) {
|
|
insert_word(dict, &dict_size, word);
|
|
}
|
|
}
|
|
|
|
// Fecha o arquivo de entrada
|
|
fclose(input_file);
|
|
|
|
// Abre o arquivo de saída
|
|
output_file = fopen("dicionario.txt", "w");
|
|
if (output_file == NULL) {
|
|
printf("Erro ao abrir arquivo de saída\n");
|
|
exit(1);
|
|
}
|
|
|
|
// Escreve as palavras do dicionário no arquivo de saída
|
|
for (int i = 0; i < dict_size; i++) {
|
|
fprintf(output_file, "%s\n", dict[i]);
|
|
}
|
|
fprintf(output_file, "\nTotal de palavras diferentes no dicionario: %d", dict_size);
|
|
|
|
// Fecha o arquivo de saída
|
|
fclose(output_file);
|
|
|
|
// Libera a memória alocada para o dicionário de palavras
|
|
for (int i = 0; i < MAX_WORDS; i++) {
|
|
free(dict[i]);
|
|
}
|
|
free(dict);
|
|
|
|
return 0;
|
|
}
|