Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Author: Leandro Soares
- School: INETE
- Date: 18-04-2012
- Time: 11:25 */
- #include <stdio.h>
- #include <stdlib.h>
- #define MaxLenght 1024
- struct tagDados {
- char CHAR;
- int count;
- struct tagDados *pnext;
- struct tagDados *pprev;
- };
- struct tagDadosWrite {
- char CHAR;
- int count;
- };
- struct tagDados *Countline(FILE *finput);
- struct tagDados *NewNode(void);
- void InsertNode(struct tagDados **phead, struct tagDados *pnew);
- void OutputTheResult(struct tagDados *phead);
- int main() {
- FILE *finput, *foutput;
- char read[MaxLenght];
- struct tagDados *phead;
- struct tagDados *pnew;
- phead = pnew = NULL;
- finput = fopen("input.txt", "r+");
- while( fgets(read,MaxLenght+2,finput) != NULL ) {
- pnew = Countline(read);
- InsertNode(&phead, pnew);
- }
- OutputTheResult(phead);
- return 0;
- }
- struct tagDados *Countline(char read[]) {
- int idx, count, LASTcount;
- char cmp, LASTcmp;
- struct tagDados *Dados;
- Dados = NewNode();
- count = LASTcount = 0;
- cmp = LASTcmp = '\0';
- for(idx=0; read[idx] != '\0' && idx <= 1024; idx++) {
- if( read[idx] >= 32 && read[idx] <= 125 ) {
- if( cmp == '\0' ) {
- cmp = read[idx];
- count++;
- }
- else if( read[idx] == cmp )
- count++;
- else if( read[idx] != cmp ) {
- if( count > LASTcount ) {
- LASTcmp = cmp;
- LASTcount = count;
- }
- cmp = read[idx];
- count = 1;
- }
- }
- }
- if( count > LASTcount ) {
- LASTcmp = cmp;
- LASTcount = count;
- }
- Dados->CHAR = LASTcmp;
- Dados->count = LASTcount;
- return Dados;
- }
- struct tagDados *NewNode(void) {
- struct tagDados *Dados;
- Dados = (struct tagDados*) malloc(sizeof(struct tagDados));
- Dados->pnext = NULL;
- Dados->pprev = NULL;
- return Dados;
- }
- void InsertNode(struct tagDados **phead, struct tagDados *pnew) {
- struct tagDados *paux;
- paux = (*phead);
- if( (*phead) != NULL ) {
- pnew->pnext = (*phead);
- pnew->pprev = NULL;
- }
- (*phead) = pnew;
- }
- void OutputTheResult(struct tagDados *phead) {
- FILE *foutput, *foutputDEBUG;
- struct tagDadosWrite *pnew;
- foutput = fopen("output.txt", "wb");
- foutputDEBUG = fopen("outputDEBUG.txt", "w");
- pnew = (struct tagDadosWrite*) malloc(sizeof(struct tagDadosWrite));
- while( phead != NULL ) {
- pnew->CHAR = phead->CHAR;
- pnew->count = phead->count;
- fwrite(pnew,sizeof(struct tagDadosWrite), 1, foutput);
- fprintf(foutputDEBUG, "%c %d\n", pnew->CHAR, pnew->count);
- phead = phead->pnext;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement