Advertisement
FoxTuGa

Fich_CNTNames.c

Mar 13th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. /*  Author: Leandro Soares
  2.     School: INETE
  3.     Date:   13-03-2012
  4.     Time:   23:32   */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. #define TRUE 1
  12.  
  13. /*  Estructuras onde sao guardados os nomes e as
  14. respectivas quantidades de letras e palavras.   */
  15. struct tagPeople {
  16.     char nome[80];  //  Nome lido.
  17.     int Pal;    //   Quantidade de palavras.
  18.     int Let;    //  Quantidade de letras.
  19.     struct tagPeople *pNext;
  20. };
  21.  
  22. struct tagPeople *CriarNo();    //  Criar uma nova estructura do tipo tagPeople.
  23. struct tagPeople *NewPess(char sbuffer[]);  //  Ler uma linha do .txt para o sbuffer.
  24. void AddPess(struct tagPeople **pHead, struct tagPeople **pNew);    //  Inserir a nova estructura na lista ligada.
  25. void Inicializar_String(char *string, char x, int tam); // Inicializar strings.
  26. void WritePersRes(struct tagPeople **Head); //  Criar um novo .txt e inserir os resultados.
  27.  
  28. int main() {
  29.     /*  Declaracao e inicializacao de vars e pointers   */
  30.     FILE * pFile;
  31.     char sbuffer[81], tempName[80];
  32.     struct tagPeople *pHead, *pNew;
  33.  
  34.     /*  Inicializacao de vars e abertura de ficheiros   */
  35.     pFile = fopen("names.txt", "r");
  36.     Inicializar_String(sbuffer, '\0', 80);
  37.     pHead = NULL;
  38.  
  39.     if( pFile != NULL ) {   //  Se o pointer do ficheiro nao estiver vazio.
  40.         while( fgets(sbuffer,80, pFile) != NULL ) { // Ler os nomes ate nao haver mais.
  41.             pNew = NewPess(sbuffer);    //  Guardar um nome (struct) no pointer temporario.
  42.             AddPess(&pHead, &pNew); //  Adicionar o nome (struct) a lista ligada.
  43.         }
  44.         fclose(pFile);
  45.         WritePersRes(&pHead);   //  Escrever os resultados.
  46.  
  47.         printf("\n\n\t\t...100%% Successfull!!\n\n\n");
  48.     }
  49.     else
  50.         printf("\nFicheiro nao encontrado!!\n\n");  //  Se o pointer do ficheiro estiver vazio.
  51.  
  52.     return 0;
  53. }
  54.  
  55. struct tagPeople *CriarNo() {
  56.     struct tagPeople *pNew;
  57.  
  58.     /*  Criar uma nova struct dinamicamente e apontar o pNew para ela,
  59.         depois, apontar o pNext para NULL (inicializacao).  */
  60.     pNew = (struct tagPeople *) malloc(sizeof(struct tagPeople));
  61.  
  62.     pNew->pNext = NULL;
  63.  
  64.     return pNew;    //  Retornar o endereco da nova struct alocada dinamicamente.
  65. }
  66. struct tagPeople *NewPess(char sbuffer[]) {
  67.     /*  Declaracao e inicializacao de vars e pointers   */
  68.     int cntPal = 1, cntLetras = 0, idx, idxb;
  69.     char tempName[80];
  70.     struct tagPeople *pNew;
  71.     idx = idxb = 0;
  72.  
  73.     while(isspace(sbuffer[idx])) idx++; //  Ignorar os espacos (se houver) ate a primeira palavra.
  74.     for(;sbuffer[idx] != '\n'; idx++) { //  Enquanto nao terminar...
  75.         if( isspace(sbuffer[idx]) && !isspace(sbuffer[idx+1]) )  {  //  Se houver uma proxima palavra.
  76.             tempName[idxb] = '\0';  //  Fim  de string (temporario).
  77.             if( !isspace(tempName[idxb]) ) {    // Se o char for um espaco.
  78.                 tempName[idxb] = ' ';
  79.                 idxb++;
  80.             }
  81.             cntPal++;
  82.         }
  83.         if( isalpha(sbuffer[idx]) ) {
  84.             tempName[idxb] = sbuffer[idx];
  85.             idxb++;
  86.             cntLetras++;
  87.         }
  88.     }
  89.     tempName[idxb] = '\0';
  90.     if( cntLetras != 0 ) {
  91.         pNew = CriarNo();
  92.         pNew->Let = cntLetras;
  93.         pNew->Pal = cntPal;
  94.         strcpy(pNew->nome, tempName);
  95.     }
  96.  
  97.     return pNew;
  98. }
  99. void AddPess(struct tagPeople **pHead, struct tagPeople **pNew) {
  100.     if( (*pHead) != NULL )
  101.         (*pNew)->pNext = (*pHead);
  102.     (*pHead) = (*pNew);
  103. }
  104. void Inicializar_String(char *string, char x, int tam) {
  105.     while( tam >= 0 ) {
  106.         *(string+(80-tam)) = x;
  107.         tam--;
  108.     }
  109. }
  110. void WritePersRes(struct tagPeople **pHead) {
  111.     FILE *pFile;
  112.     struct tagPeople *pAux, *pMaiorNlet, *pMenorNlet;
  113.  
  114.     pMaiorNlet = pMenorNlet = NULL;
  115.     pAux = (*pHead);
  116.     pFile = fopen("resultados.txt","w+");
  117.  
  118.     while(pAux != NULL) {
  119.         fprintf(pFile, "\n%s:\n\tLetras: %d\n\tPalavras: %d\n", pAux->nome, pAux->Let, pAux->Pal);
  120.         if( pMaiorNlet == NULL || pMaiorNlet->Let < pAux->Let )
  121.             pMaiorNlet = pAux;
  122.         if( pMenorNlet == NULL || pMenorNlet->Let > pAux->Let )
  123.             pMenorNlet = pAux;
  124.  
  125.         pAux = pAux->pNext;
  126.     }
  127.  
  128.     fprintf(pFile, "\nMaior Nome: %s", pMaiorNlet->nome);
  129.     fprintf(pFile, "\nMenor Nome: %s\n\n", pMenorNlet->nome);
  130.     fclose(pFile);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement