Hi everyone, these last days ive been trying to make an assigment my teacher left, i think i have the basic idea of everything, but i just cant get it working correctly at the end.
Im starting to lose my patience since my teacher wont even answer her e-mails (she never does on weekends...) and the test is this tuesday.
The assigment is in spanish (im a spanish native speaker) but i will translate all i can, i also used spanish words when putting names in the program, but it shouldnt make it too hard to understand, ill put notes explaining everything anyway.
I know it may be kinda big, but i would really appreciate the persons that help me on this one.
This is the assigment:
Starting from a text archives (called "palabras.txt") ((this one was gave to us by the teacher)) , make a list and determine the number of times a word is repeated on the file, and at the end, make a new .txt file called "salida.txt" that contains the words and the number of times it was repeated.
We just put all the files, the program, the .exe file is made when you compile the program and the palabras.txt in the same folder so we dont have to bother with the file's direction.
For example:
If the palabras.txt file has: L = { blue,red,yellow,red,yellow,red } the salida.txt would be :
blue 1
red 3
yellow 2
And well, this is the program ive been doing, but it just doesnt goes out right way and i cant find the problem...
Code:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct campos {
char valor[10]; /*will contain the words*/
int repetido; /*will contain the times the word is repeated*/
struct campos *prox; /* "prox" will be the one that represents the value/space/whatever after the
memory the pointer is pointing*/
};
typedef struct campos Nodo; /*change the name to "Nodo"*/
Nodo *primero;
void Crear_Lista() /*making a list, using pointers to put the words*/
{primero = NULL;}
/*primero would always be pointing at the beginning of the list*/
char EsListaVacia() /*made to see if the list is empty*/
{ if (primero == NULL)
return 1;
else
return 0;
}
void Insertar(char val[]) /*this one is supposed to insert the words into a list*/
{
Nodo *anterior,*aux;
aux = (Nodo*)malloc(sizeof(Nodo));
strcpy(aux->valor,val); /*storing the word on the memory this pointer is uh.. pointing*/
aux->prox = NULL;
if(EsListaVacia()==1) /*if the list is empty, i just make the aux the first one on the list*/
{primero = aux;}
else
{
anterior = primero;
while(anterior->prox != NULL)
{anterior = anterior->prox;}
aux->prox = anterior->prox;
anterior->prox = aux;
} /*its easier to see if you draw it on a paper, when anterior has a NULL next to it (and aux had
Null after it from the beginning, the next from aux becomes next of anterior (so it becomes NULL
and the next of anterior becomes aux, so now it comes anterior->aux->NULL, so everytime i call
"insertar" the words will be added in order */
}
void InsTexto() /*ok, here is where i started doing the new stuff, i made this one to insert the words into the
pointer's list*/
{FILE *pal;
char palabra[10];
if (!(pal=fopen("palabras.txt","r"))) /*if there is no file like this it will give this printf*/
{printf("Error al abrir uno de los dos archivos, porfavor intente de nuevo \n");} /*this just says that
there was an error opening the file*/
else
{while (!feof(pal)){ /*so, while the file pal (wich is palabras.txt) doesnt hits the end symbol*/
fgets(palabra,10,pal); /*it gets the word from the list, and put it into palabra*/
Insertar(palabra);} /*then, it is inserted into the list, note that here the while is closed*/
fclose(pal); /*close the file*/
}printf ("insertado\n");system ("pause"); /*used it to know when what is happening, when i see this
i know the program could do the "InsTexto()"*/
}
void comparacion() /*this one is supposed to compare the words between them, to know how many times they
apprear repeated*/
{
Nodo *aux1,*aux2,*numerador;
aux1=primero;
aux2=primero; /*i put all these 3 pointers to point to the first space of the list*/
numerador=primero;
int m1=0; /*this will keep the track on how many times it is repeated*/
while (aux1!=NULL)/*while aux1 doesnt get to the end of the list*/
{
while(aux2!=NULL)/*while aux2 doesnt get to the end of the list*/
{
if (strcmp(aux1->valor,aux2->valor)== 0) /*if the strcmp gives the 0, it means the words are the
same so...*/
{m1++;}/*i put one more to the m1, to know it already has been repeated once (here, i close the if*/
aux2=aux2->prox;
}
numerador->repetido=m1; /*i insert the value of m1 into the slot i made on the struct for times repeated*/
numerador=numerador->prox; /*Now numerador goes into the next memory to insert the next value into it*/
m1=0;/*i reset m1 back to 0 again*/
aux2=primero; /*i put aux2 pointing to the first one again*/
aux1=aux1->prox; /*i advance aux 1 and the while loop should start again*/
} printf ("comparado\n"); system ("pause"); /*same as before, i put this to know this part has been
succesfully done*/
}
void creacion() /* and this is the last one, it will create the "salida.txt" with all the things i was asked for*/
{
FILE *fin;
Nodo *aux1;
aux1=primero; /*i make aux1 to point at the beginning of the list*/
fin=fopen("salida.txt","w"); /*i open the new salida.txt, and make it "fin"*/
while (aux1!=NULL) /*while aux1 doesnt reach the NULL*/
{
fprintf(fin,"%s\n",aux1->valor); /* this should put the word that was already stored into the new text file*/
fprintf(fin,"%d\n",aux1->repetido); /*this should put the numbers i stored before into the new file*/
aux1=aux1->prox; /*and then i just advance the pointer*/
}
fclose(fin);printf ("creado\n");system ("pause"); /*close the salida.txt, and the same print i use to know
if this part have been done*/
}
main()
{
Crear_Lista();
printf ("Presione cualquier tecla para ejecutar programa\n"); /*translation: press any key to start the
program*/
system ("pause");
InsTexto(); /*run the function i made to put the words into a list*/
comparacion(); /*compare words between them and insert the number of times repeated into the list*/
creacion(); /*create the salida.txt*/
}