Anda di halaman 1dari 7

#include <stdio.

h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int cambio = 0;
struct _renglon {
char *palabras;
struct _renglon *next;
};
char *erase(char *string, char *word){
char *aux = NULL;
char *found = NULL;
char *first = NULL;
char *end =NULL;
int count = 0;
int i;
aux= string;
first = aux;
found = strstr(string, word);
if(found != NULL){
cambio = 1;
first = found;
while(*found == *word && found != '\0' && *word!= '\0'){
count++;
found++;
word++;
}
end = first + count;
while(*end != '\0'){
*first = *end;
first++;
end++;
}
*first = '\0';
}
return(aux);
}
char *getLine(FILE *theFile) {
char *textLine = NULL;
char *auxTextLine = NULL;

int countMAX = 10;


int countAct = 0;
char chr = 'a';
textLine = (char *)malloc(countMAX*sizeof(char));
while ((chr != 10) && (!feof(theFile))) {
chr = getc(theFile);
if ((chr != 10) && (!feof(theFile))) {
textLine[countAct++] = chr;
if (countAct == countMAX-1) {
auxTextLine = (char *)malloc((countMAX + 10)*sizeof(char));
memcpy(auxTextLine, textLine, countMAX);
free(textLine);
textLine = auxTextLine;
countMAX+=10;
}
textLine[countAct] = '\n';
textLine[countAct+1] = '\0';
}
}
return(textLine);
}
void main(int counter, char *parameter[]){
struct _renglon *node = NULL;
struct _renglon *ultimo = NULL;
struct _renglon *primero = NULL;
char *string = NULL;
char *word = NULL;

FILE * fp;
fp = fopen (parameter[1], "r");
word = parameter[2];
while(!feof(fp)){
string = getLine(fp);
if(string!=NULL){
node = (struct _renglon *) malloc(sizeof(struct _renglon));
node->palabras = string;
if(primero == NULL){
node->next = NULL;
primero = node;

ultimo = node;
}
else{
ultimo->next = node;
ultimo = node;
ultimo->next = NULL;
}
}
}
fclose(fp);
fp = fopen (parameter[1], "w+");
while(primero->next != NULL){
primero->palabras = erase(primero->palabras, word);
while(cambio == 1){
cambio = 0;
primero->palabras = erase(primero->palabras, word);
}
fprintf(fp, primero->palabras);
primero=primero->next;
}
fclose(fp);
}
// segundo
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int cambio = 0;
struct _renglon *primero = NULL;
struct _renglon {
char *palabras;
int numero;
struct _renglon *next;
};
int contar(char *string){

char * aux = NULL;


int cuenta = 0;
int primeraLetra = 0;
aux = string;
while(*aux != '\0'){
if((*aux == ' ') ||(*aux == '\t') || (*aux == '\n')){
if(primeraLetra == 1){
primeraLetra = 0;
cuenta++;
}
aux++;
}
else{
if(primeraLetra == 0){
primeraLetra = 1;
}
aux++;
}
}
return cuenta;
}
char *getLine(FILE *theFile) {
char *textLine = NULL;
char *auxTextLine = NULL;
int countMAX = 10;
int countAct = 0;
char chr = 'a';
textLine = (char *)malloc(countMAX*sizeof(char));
while ((chr != 10) && (!feof(theFile))) {
chr = getc(theFile);
//printf("%d ",chr);
if ((chr != 10) && (!feof(theFile))) {
textLine[countAct++] = chr;
if (countAct == countMAX-1) {
auxTextLine = (char *)malloc((countMAX + 10)*sizeof(char));
memcpy(auxTextLine, textLine, countMAX);
free(textLine);
textLine = auxTextLine;
countMAX+=10;
}

textLine[countAct] = '\n';
textLine[countAct+1] = '\0';
}
}
return(textLine);
}
void sortByWordCount(){
struct _renglon *auxList = NULL;
struct _renglon *maxNode, *prevMaxNode;
struct _renglon *checkNode, *prevCheckNode;
while(primero != NULL){
prevMaxNode = NULL;
maxNode = primero;
prevCheckNode = primero;
checkNode = maxNode->next;
while(checkNode!= NULL){
if(maxNode->numero < checkNode->numero){
prevMaxNode = prevCheckNode;
maxNode = checkNode;
}
prevCheckNode = checkNode;
checkNode = checkNode->next;
}
if(auxList == NULL){
auxList = maxNode;
if(prevMaxNode == NULL){
primero=maxNode->next;
}else{
prevMaxNode->next = maxNode->next;
}
maxNode->next = NULL;
} else{
if(prevMaxNode == NULL){
primero=maxNode->next;
}else{
prevMaxNode->next = maxNode->next;
}
maxNode->next= auxList;
auxList = maxNode;
}

}
primero = auxList;
}
void main(int counter, char *parameter[]){
struct _renglon *node = NULL;
struct _renglon *ultimo = NULL;
char *string = NULL;
char *word = NULL;
FILE * fp;
fp = fopen (parameter[1], "r");
while(!feof(fp)){
string = getLine(fp);
if(string!=NULL && !feof(fp) && *string!= '\0'){
node = (struct _renglon *) malloc(sizeof(struct _renglon));
node->palabras = string;
if(primero == NULL){
node->next = NULL;
primero = node;
ultimo = node;
ultimo->next = NULL;
primero->next= NULL;
}
else{
ultimo->next = node;
ultimo = node;
ultimo->next = NULL;
}
}
}
fclose(fp);
fp = fopen (parameter[2], "w+");
node = primero;
while(node != NULL){
node->numero = contar(node->palabras);
node=node->next;

}
sortByWordCount();
node = primero;
while(node != NULL){
fprintf(fp,node->palabras);
node=node->next;
}
fclose(fp);

Anda mungkin juga menyukai