miguel747 icon

Lempelzi

miguel747 | PRO | 03/03/13 11:36:11 PM UTC | 0 ⭐ | 172 👁️ | Never ⏰ | []
C |

2.62 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
 
    typedef struct elemento {
      char dado[15];
      int indici;
      int indc_cod_caracter;
      int indc_elem_igual;
      struct elemento *prox;
    } lista;
 
 
int j = 0;
FILE *arq;
 
 
void Constroi_Lista_de_codigos(lista **edicionario) {
      lista *p1, *p2, *p3;
      int indc = 1, i = 0, k = 0;
 
      char c[3];
      char  concat[15];
      memset(concat, ' ', 15);
      memset(c, ' ', 3);
 
      arq = fopen ("infile.txt", "r");
      *edicionario = NULL;
      while ((c[k] = getc (arq)) != EOF) {
        p1 = malloc (sizeof (lista));
        p1->prox = NULL;
        if (*edicionario == NULL) {
          p1->dado[j] = c[k];
          p1->dado[j+1] = '\0';
          *edicionario = p1;
          p1->indici = indc;
          p2 = *edicionario;
        }
        else {
          p3 = *edicionario;
          concat[i] = c[k];
          while(p3 != NULL){
             if ((p3->dado[j] != '\0') && (p3->dado[j] == concat[j])){
                 c[k] = getc (arq);
                 if(c[k] != EOF){
                    i++;
                    concat[i] = c[k];
                    j++;
                 }
             }
            p3 = p3->prox;
            }
           }
          if(p3 == NULL){
              if(c[k] != EOF){
                indc = indc + 1;
                j = 0;
                i = 0;
                while(concat[i] != ' '){
                    p1->dado[i] = concat[i];
                    i++;
                }
                if(concat[i] == ' ')
                    p1->dado[i] = '\0';
                i = 0;
                p1->indici = indc;
                memset(concat, ' ', 15);
              }else{
                p1->dado[j] = '*';
                p1->indc_elem_igual = indc;
              }
             p2->prox = p1;
             p2 = p1;
             p1->prox = NULL;
          }
        }
        fclose (arq);
    }
 
 
void PercorreListaAS(lista *pinicio) {
      lista *p1;
 
 
      if (pinicio == NULL)
        printf ("lista vazia \n");
      else {
        p1 = pinicio;
        while (p1 != NULL) {
              while(p1->dado[j] != '\0'){
                 printf("%c", p1->dado[j]);
                 j++;
              }
          printf("\t");
          j = 0;
          p1 = p1->prox;
        }
      }
    }
 
int main (){
      lista *pdicionario;
 
      arq = fopen ("infile.txt", "r");
 
      Constroi_Lista_de_codigos(&pdicionario);
      PercorreListaAS(pdicionario);
      printf ("\n");
      return 0;
      system("pause");
    }

Comments