Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Author: Leandro Soares
- School: INETE
- Date: 13-03-2012
- Time: 23:32 */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define TRUE 1
- /* Estructuras onde sao guardados os nomes e as
- respectivas quantidades de letras e palavras. */
- struct tagPeople {
- char nome[80]; // Nome lido.
- int Pal; // Quantidade de palavras.
- int Let; // Quantidade de letras.
- struct tagPeople *pNext;
- };
- struct tagPeople *CriarNo(); // Criar uma nova estructura do tipo tagPeople.
- struct tagPeople *NewPess(char sbuffer[]); // Ler uma linha do .txt para o sbuffer.
- void AddPess(struct tagPeople **pHead, struct tagPeople **pNew); // Inserir a nova estructura na lista ligada.
- void Inicializar_String(char *string, char x, int tam); // Inicializar strings.
- void WritePersRes(struct tagPeople **Head); // Criar um novo .txt e inserir os resultados.
- int main() {
- /* Declaracao e inicializacao de vars e pointers */
- FILE * pFile;
- char sbuffer[81], tempName[80];
- struct tagPeople *pHead, *pNew;
- /* Inicializacao de vars e abertura de ficheiros */
- pFile = fopen("names.txt", "r");
- Inicializar_String(sbuffer, '\0', 80);
- pHead = NULL;
- if( pFile != NULL ) { // Se o pointer do ficheiro nao estiver vazio.
- while( fgets(sbuffer,80, pFile) != NULL ) { // Ler os nomes ate nao haver mais.
- pNew = NewPess(sbuffer); // Guardar um nome (struct) no pointer temporario.
- AddPess(&pHead, &pNew); // Adicionar o nome (struct) a lista ligada.
- }
- fclose(pFile);
- WritePersRes(&pHead); // Escrever os resultados.
- printf("\n\n\t\t...100%% Successfull!!\n\n\n");
- }
- else
- printf("\nFicheiro nao encontrado!!\n\n"); // Se o pointer do ficheiro estiver vazio.
- return 0;
- }
- struct tagPeople *CriarNo() {
- struct tagPeople *pNew;
- /* Criar uma nova struct dinamicamente e apontar o pNew para ela,
- depois, apontar o pNext para NULL (inicializacao). */
- pNew = (struct tagPeople *) malloc(sizeof(struct tagPeople));
- pNew->pNext = NULL;
- return pNew; // Retornar o endereco da nova struct alocada dinamicamente.
- }
- struct tagPeople *NewPess(char sbuffer[]) {
- /* Declaracao e inicializacao de vars e pointers */
- int cntPal = 1, cntLetras = 0, idx, idxb;
- char tempName[80];
- struct tagPeople *pNew;
- idx = idxb = 0;
- while(isspace(sbuffer[idx])) idx++; // Ignorar os espacos (se houver) ate a primeira palavra.
- for(;sbuffer[idx] != '\n'; idx++) { // Enquanto nao terminar...
- if( isspace(sbuffer[idx]) && !isspace(sbuffer[idx+1]) ) { // Se houver uma proxima palavra.
- tempName[idxb] = '\0'; // Fim de string (temporario).
- if( !isspace(tempName[idxb]) ) { // Se o char for um espaco.
- tempName[idxb] = ' ';
- idxb++;
- }
- cntPal++;
- }
- if( isalpha(sbuffer[idx]) ) {
- tempName[idxb] = sbuffer[idx];
- idxb++;
- cntLetras++;
- }
- }
- tempName[idxb] = '\0';
- if( cntLetras != 0 ) {
- pNew = CriarNo();
- pNew->Let = cntLetras;
- pNew->Pal = cntPal;
- strcpy(pNew->nome, tempName);
- }
- return pNew;
- }
- void AddPess(struct tagPeople **pHead, struct tagPeople **pNew) {
- if( (*pHead) != NULL )
- (*pNew)->pNext = (*pHead);
- (*pHead) = (*pNew);
- }
- void Inicializar_String(char *string, char x, int tam) {
- while( tam >= 0 ) {
- *(string+(80-tam)) = x;
- tam--;
- }
- }
- void WritePersRes(struct tagPeople **pHead) {
- FILE *pFile;
- struct tagPeople *pAux, *pMaiorNlet, *pMenorNlet;
- pMaiorNlet = pMenorNlet = NULL;
- pAux = (*pHead);
- pFile = fopen("resultados.txt","w+");
- while(pAux != NULL) {
- fprintf(pFile, "\n%s:\n\tLetras: %d\n\tPalavras: %d\n", pAux->nome, pAux->Let, pAux->Pal);
- if( pMaiorNlet == NULL || pMaiorNlet->Let < pAux->Let )
- pMaiorNlet = pAux;
- if( pMenorNlet == NULL || pMenorNlet->Let > pAux->Let )
- pMenorNlet = pAux;
- pAux = pAux->pNext;
- }
- fprintf(pFile, "\nMaior Nome: %s", pMaiorNlet->nome);
- fprintf(pFile, "\nMenor Nome: %s\n\n", pMenorNlet->nome);
- fclose(pFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement