Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Definicao do tipo ocorrencia */
typedef struct as_ocorrencias
{
int num_ocorrencias;
char *palavra;
struct as_ocorrencias *proximo;
} OCORRENCIAS;
typedef struct o_histograma
{
int num_vezes;
int tamanho;
struct o_histograma *proximo;
} HISTOGRAMA;
typedef OCORRENCIAS* ocorrencias;
typedef HISTOGRAMA* histograma;
int ja_existe_ocorrencia(char *palavra, ocorrencias* Ocorrencias, int n)
{
if(*Ocorrencias== NULL)
return n;
else
{
if(strcmp(palavra, (*Ocorrencias)->palavra))
n=n + (ja_existe_ocorrencia(palavra, &(**Ocorrencias).proximo, n));
else
{
(*Ocorrencias)->num_ocorrencias +=1;
n+=1;
}
}
return n;
}
int ja_existe_histograma(int tamanho, histograma* Histograma, int n)
{
if(*Histograma== NULL)
return n;
else
{
if(tamanho== (*Histograma)->tamanho)
{
(*Histograma)->num_vezes+=1;
n+=1;
}
else
n=n + (ja_existe_histograma(tamanho, &(**Histograma).proximo, n));
}
return n;
}
/* Inicia a ocorrencia*/
void inicia_ocorrencias(ocorrencias* Ocorrencias)
{
*Ocorrencias = NULL;
}
/* Inicia o histograma*/
void inicia_histograma(histograma* Histograma)
{
*Histograma = NULL;
}
void acrescentaux_ocorrencias (ocorrencias* Ocorrencias, int num_ocorrencias, char *palavra,int n)
{
if(*Ocorrencias == NULL)
{
*Ocorrencias=(ocorrencias)malloc(sizeof(OCORRENCIAS));
if(*Ocorrencias == NULL)
return;
(*Ocorrencias) -> num_ocorrencias=num_ocorrencias;
(*Ocorrencias) -> palavra=malloc (n);
strcpy ((*Ocorrencias) -> palavra, palavra);
(**Ocorrencias).proximo = NULL;
}
else
acrescentaux_ocorrencias(&(**Ocorrencias).proximo, num_ocorrencias, palavra,n);
}
void acrescentaux_histograma (histograma* Histograma, int num_vezes, int tamanho)
{
if(*Histograma == NULL)
{
*Histograma=(histograma)malloc(sizeof(HISTOGRAMA));
if(*Histograma == NULL)
return;
(*Histograma) -> num_vezes=num_vezes;
(*Histograma) -> tamanho=tamanho;
(**Histograma).proximo = NULL;
}
else
acrescentaux_histograma(&(**Histograma).proximo, num_vezes, tamanho);
}
/* Insere uma nova ocorrencia*/
void acrescenta_ocorrencias (ocorrencias* Ocorrencias, int num_ocorrencias, char *palavra,int n)
{
if((ja_existe_ocorrencia(palavra, Ocorrencias, 0))==0)
acrescentaux_ocorrencias(Ocorrencias, num_ocorrencias, palavra,n);
}
/* Insere um novo histograma*/
void acrescenta_histograma (histograma* Histograma, int num_vezes, int tamanho)
{
if((ja_existe_histograma(tamanho, Histograma, 0))==0)
acrescentaux_histograma(Histograma, num_vezes, tamanho);
}
void escreve_ocorrencias(ocorrencias Ocorrencias)
{
if(Ocorrencias == NULL)
return;
printf("%s %d\n", Ocorrencias -> palavra, Ocorrencias -> num_ocorrencias);
escreve_ocorrencias(Ocorrencias -> proximo);
}
void escreve_histograma(histograma Histograma)
{
if(Histograma == NULL)
return;
printf("%d %d\n", Histograma -> tamanho, Histograma -> num_vezes);
escreve_histograma(Histograma -> proximo);
}
char *limpa_palavra(char palavra[])
{
int ui;
for(ui=0; ui<(strlen(palavra));ui++)
palavra[ui]=' ';
return palavra;
}
int compare ( const void *a, const void *b )
{
const char **pa ;
const char **pb ;
size_t la = strlen( *pa );
size_t lb = strlen( *pb );
if ( la < lb ) return -1;
if ( la > lb ) return +1;
return 0;
}
main(int argc, char *argv[])
{
FILE *fp;
char palavra[BUFSIZ];
char texto [BUFSIZ][BUFSIZ];
char c;
int i=0;
int conta_palavras=0;
int p;
ocorrencias oc;
histograma h;
inicia_ocorrencias(&oc);
inicia_histograma(&h);
fp = fopen(argv[1],"r");
if(argc != 2)
{
printf("Numero de argumentos invalidos\n");
exit(0);
}
else
{
if(fp == NULL)
{
printf("Impossivel abrir o ficheiro %s\n",argv[1]);
exit(1);
}
else
{
while ((c=fgetc(fp))!=EOF)
{
if(i==0)
{
if ( c==95 || c>64 && c<91 || c>96 && c<123)
{
palavra[i]=c;
++i;
}
else continue;
}
else
{
if(c<58 && c>47 || c>64 && c<91 || c>96 && c<123 ||c==95 )
{
palavra[i]=c;
++i;
}
else
{
palavra[i]='\0';
strcpy(*(texto+conta_palavras),palavra);
conta_palavras=++conta_palavras;
i=0;
limpa_palavra(palavra);
}
}
}
palavra[i]='\0';
strcpy(*(texto+conta_palavras),palavra);
conta_palavras=++conta_palavras;
i=0;
limpa_palavra(palavra);
}
}
for(p=0;p< conta_palavras;++p)
{
acrescenta_ocorrencias(&oc, 1, texto[p],strlen(texto[p]));
acrescenta_histograma(&h, 1, strlen(texto[p]));
}
qsort(texto,sizeof(texto)/sizeof(texto[0]),sizeof(texto[0]),compare);
printf("TOTAL: %d\n", conta_palavras);
puts("HISTOGRAMA:");
escreve_histograma(h);
putchar('\n');
puts("OCORRENCIAS:");
escreve_ocorrencias(oc);
}