miguel747

Lempelzi

Mar 3rd, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2.     #include <string.h>
  3.     #include <stdlib.h>
  4.  
  5.     typedef struct elemento {
  6.       char dado[15];
  7.       int indici;
  8.       int indc_cod_caracter;
  9.       int indc_elem_igual;
  10.       struct elemento *prox;
  11.     } lista;
  12.  
  13.  
  14. int j = 0;
  15. FILE *arq;
  16.  
  17.  
  18. void Constroi_Lista_de_codigos(lista **edicionario) {
  19.       lista *p1, *p2, *p3;
  20.       int indc = 1, i = 0, k = 0;
  21.  
  22.       char c[3];
  23.       char  concat[15];
  24.       memset(concat, ' ', 15);
  25.       memset(c, ' ', 3);
  26.  
  27.       arq = fopen ("infile.txt", "r");
  28.       *edicionario = NULL;
  29.       while ((c[k] = getc (arq)) != EOF) {
  30.         p1 = malloc (sizeof (lista));
  31.         p1->prox = NULL;
  32.         if (*edicionario == NULL) {
  33.           p1->dado[j] = c[k];
  34.           p1->dado[j+1] = '\0';
  35.           *edicionario = p1;
  36.           p1->indici = indc;
  37.           p2 = *edicionario;
  38.         }
  39.         else {
  40.           p3 = *edicionario;
  41.           concat[i] = c[k];
  42.           while(p3 != NULL){
  43.              if ((p3->dado[j] != '\0') && (p3->dado[j] == concat[j])){
  44.                  c[k] = getc (arq);
  45.                  if(c[k] != EOF){
  46.                     i++;
  47.                     concat[i] = c[k];
  48.                     j++;
  49.                  }
  50.              }
  51.             p3 = p3->prox;
  52.             }
  53.            }
  54.           if(p3 == NULL){
  55.               if(c[k] != EOF){
  56.                 indc = indc + 1;
  57.                 j = 0;
  58.                 i = 0;
  59.                 while(concat[i] != ' '){
  60.                     p1->dado[i] = concat[i];
  61.                     i++;
  62.                 }
  63.                 if(concat[i] == ' ')
  64.                     p1->dado[i] = '\0';
  65.                 i = 0;
  66.                 p1->indici = indc;
  67.                 memset(concat, ' ', 15);
  68.               }else{
  69.                 p1->dado[j] = '*';
  70.                 p1->indc_elem_igual = indc;
  71.               }
  72.              p2->prox = p1;
  73.              p2 = p1;
  74.              p1->prox = NULL;
  75.           }
  76.         }
  77.         fclose (arq);
  78.     }
  79.  
  80.  
  81. void PercorreListaAS(lista *pinicio) {
  82.       lista *p1;
  83.  
  84.  
  85.       if (pinicio == NULL)
  86.         printf ("lista vazia \n");
  87.       else {
  88.         p1 = pinicio;
  89.         while (p1 != NULL) {
  90.               while(p1->dado[j] != '\0'){
  91.                  printf("%c", p1->dado[j]);
  92.                  j++;
  93.               }
  94.           printf("\t");
  95.           j = 0;
  96.           p1 = p1->prox;
  97.         }
  98.       }
  99.     }
  100.  
  101. int main (){
  102.       lista *pdicionario;
  103.  
  104.       arq = fopen ("infile.txt", "r");
  105.  
  106.       Constroi_Lista_de_codigos(&pdicionario);
  107.       PercorreListaAS(pdicionario);
  108.       printf ("\n");
  109.       return 0;
  110.       system("pause");
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment