Advertisement
FoxTuGa

SeqChars

Apr 18th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. /*  Author: Leandro Soares
  2. School: INETE
  3. Date:   18-04-2012
  4. Time:   11:25   */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define MaxLenght 1024
  9.  
  10. struct tagDados {
  11.     char CHAR;
  12.     int count;
  13.     struct tagDados *pnext;
  14.     struct tagDados *pprev;
  15. };
  16.  
  17. struct tagDadosWrite {
  18.     char CHAR;
  19.     int count;
  20. };
  21.  
  22. struct tagDados *Countline(FILE *finput);
  23. struct tagDados *NewNode(void);
  24. void InsertNode(struct tagDados **phead, struct tagDados *pnew);
  25. void OutputTheResult(struct tagDados *phead);
  26.  
  27. int main() {
  28.     FILE *finput, *foutput;
  29.     char read[MaxLenght];
  30.     struct tagDados *phead;
  31.     struct tagDados *pnew;
  32.  
  33.     phead = pnew = NULL;
  34.     finput = fopen("input.txt", "r+");
  35.  
  36.     while( fgets(read,MaxLenght+2,finput) != NULL ) {
  37.         pnew = Countline(read);
  38.         InsertNode(&phead, pnew);
  39.     }
  40.     OutputTheResult(phead);
  41.  
  42.     return 0;
  43. }
  44.  
  45. struct tagDados *Countline(char read[]) {
  46.     int idx, count, LASTcount;
  47.     char cmp, LASTcmp;
  48.     struct tagDados *Dados;
  49.  
  50.     Dados = NewNode();
  51.  
  52.     count = LASTcount = 0;
  53.     cmp = LASTcmp = '\0';
  54.  
  55.     for(idx=0; read[idx] != '\0' && idx <= 1024; idx++) {
  56.         if( read[idx] >= 32 && read[idx] <= 125 ) {
  57.             if( cmp == '\0' ) {
  58.                 cmp = read[idx];
  59.                 count++;
  60.             }
  61.             else    if( read[idx] == cmp )
  62.                 count++;
  63.             else    if( read[idx] != cmp ) {
  64.                 if( count > LASTcount ) {
  65.                     LASTcmp = cmp;
  66.                     LASTcount = count;
  67.                 }
  68.  
  69.                 cmp = read[idx];
  70.                 count = 1;
  71.             }
  72.         }
  73.     }
  74.     if( count > LASTcount ) {
  75.         LASTcmp = cmp;
  76.         LASTcount = count;
  77.     }
  78.     Dados->CHAR = LASTcmp;
  79.     Dados->count = LASTcount;
  80.  
  81.     return Dados;
  82. }
  83. struct tagDados *NewNode(void) {
  84.     struct tagDados *Dados;
  85.  
  86.     Dados = (struct tagDados*) malloc(sizeof(struct tagDados));
  87.  
  88.     Dados->pnext = NULL;
  89.     Dados->pprev = NULL;
  90.  
  91.     return Dados;
  92. }
  93. void InsertNode(struct tagDados **phead, struct tagDados *pnew) {
  94.     struct tagDados *paux;
  95.     paux = (*phead);
  96.  
  97.     if( (*phead) != NULL ) {
  98.         pnew->pnext = (*phead);
  99.         pnew->pprev = NULL;
  100.     }
  101.     (*phead) = pnew;
  102. }
  103. void OutputTheResult(struct tagDados *phead) {
  104.     FILE *foutput, *foutputDEBUG;
  105.     struct tagDadosWrite *pnew;
  106.     foutput = fopen("output.txt", "wb");
  107.     foutputDEBUG = fopen("outputDEBUG.txt", "w");
  108.  
  109.     pnew = (struct tagDadosWrite*) malloc(sizeof(struct tagDadosWrite));
  110.  
  111.     while( phead != NULL ) {
  112.         pnew->CHAR = phead->CHAR;
  113.         pnew->count = phead->count;
  114.         fwrite(pnew,sizeof(struct tagDadosWrite), 1, foutput);
  115.         fprintf(foutputDEBUG, "%c %d\n", pnew->CHAR, pnew->count);
  116.  
  117.         phead = phead->pnext;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement