473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

is a good C?

TC
I'm not a C programmer.
This is a good C?
how check the return value of getch or scanf?
and how check for the \n at the end of the text read?
FEOF do not return true if the file (text file) is finish?
if no what use?
/*Inclusione librerie*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*Definizione della strutura Elemento*/
typedef struct
{
char *parola;
unsigned int freq;
}Elem;

/*Definizione della lista delle parole trovate*/
struct Albero
{
Elem Elemento;
struct Albero *s;
struct Albero *d;
};

/*Definizione variabili*/
/*Albero ordinato per alfabeto*/
struct Albero *treeA = NULL;
/*Albero ordinato per frequenze*/
struct Albero *treeF = NULL;

unsigned int count = 0;

/*Definizione funzioni*/
/*Funzione di scansione del file costruzione delle parole e chiamata
della funzione di inseriento nell'albero ordinato per alfabeto*/
void Scansione(FILE *pf);
/*Aggiungi nodo all'albero ordinato per alfabeto*/
struct Albero *AggNodo_TreeA(struct Albero *nodo, char *parola);
/*Stampa albero*/
void Print(struct Albero *nodo);
/*Crea un nuovo albero ordinato per frequenza utilizzando come sorgente
l'albero
ordinato per alfabeto*/
void CreaAlberoF(struct Albero *sorg);
/*Aggiunge un nuovo nodo all'albero ordinato pre frequenze copiando i
valori dal
nodo passato come sorgente*/
struct Albero * AggNodo_TreeF(struct Albero *sorg, struct Albero *dest);
main (void)
{
char scelta;
int fOK;
int g;
/*Nome del file di testo*/
char nf[50];
/*Puntatore al file*/
FILE *pf;
/*MAIN*/
printf("\n Avvio del programma \n");

do
{
fOK = 0;
printf("\nInserire il nome del file di testo che si desidera
utilizzare (Esempio: Testo.txt) \n");

g = fscanf(stdin,"%s",nf); /*Lettura del nome del file */

pf=fopen(nf,"r"); /*Apertura del file in modalità di lettura*/

/*Controllo apertura del file*/
if (pf==NULL)
{
/*Restituzione del messaggio di errore in caso di mancata apertura*/
perror("ERRORE nell'apertura del file");
fOK = 1;

}

}while(fOK != 0);

printf("\n\n");

do
{
scelta = 0;
/*Menù*/
printf("\n 1- Scansione del file di testo \n");
printf("\n 2- Creazione del file delle parole ordinato per frequenza \n");
printf("\n 3- Stampa a video del file delle parole ordinato per
frequenza \n");
printf("\n 4- Stampa a video del file delle parole ordinato per ordine
alfabetico \n");
printf("\n 5- Esci \n");

do
{
scelta = getchar();
}while(scelta!= '1' && scelta!= '2' && scelta!= '3' && scelta!= '4' &&
scelta!= '5');
count=0;
/*Blocco di scelta*/
switch(scelta)
{
case ('1'):
Scansione(pf);
printf("\n Scansione EFFETTUATA! \n");
break;
case ('2'):
CreaAlberoF(treeA);
printf("\n Lista delle parole ordinata per frequenza creata! \n");
break;
case ('3'):
Print(treeF);
break;
case('4'):
Print(treeA) ;
break;
default:
exit(0);
}
}while(scelta!=5);
}

/*Funzione che effettua la scansione del file per individuare le parole
e richiama la funzione di aggiunta di un nuovo elemento all'albero
alfabeto*/
void Scansione (FILE *pf)
{
char *ptr;
char *parola;
char *allocazione;
char c; /*Variabile per la letura dei caratteri dal file di testo*/
char *carattere; /*Variabile utilizzatata per la concatenazione*/
int lung; /*Lunghezza della parola*/

/*Istanziazione e inizializzazione della variabile carattere*/
carattere = malloc(2*sizeof(carattere));
carattere[0]='\0';

/*Istanziazione e inizializzazione della variabile parola*/
parola = malloc(1*sizeof(parola));
parola[0]='\0';

while(!feof(pf)) /*Ripete fino alla fine del file*/
{
c = fgetc(pf);/*Lettura del carattere */

/*Controllo se il carattere è presente nell'alfabeto*/
if (isalpha(c))
{
/*Calcolo della lunghezza della parola */
lung=strlen(parola);

/*Assgnamento del valore alla variabile carattere*/
carattere[0]=c;
carattere[1]='\0';

if(!(allocazione = realloc(parola,(lung+1)*sizeof(parola))))
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
else
{
parola = allocazione;
}
/*lowercase*/
*carattere = tolower(*carattere);
/*Concatenzazione dei caratteri per formare la parola*/
parola = strcat(parola,carattere);
}
else
{
/*Inserimento della parola*/
if ((parola[0]!='\0'))
{

/*Richiamo della funzione che aggiunge le parole nell'albero
ordinato per alfabeto*/
if (treeA == NULL)
{
/*Aggionamemto della radice */
treeA = AggNodo_TreeA(treeA,parola);
}
else
{
AggNodo_TreeA(treeA,parola);
}
/*Reinizializzazione della variabile parola*/
parola= realloc(parola,sizeof(char));
parola[0]='\0';
}
}
}
/*Chiusura file*/
fclose(pf);
}

/*Funzione di aggiunta di un nodo all'albero*/
struct Albero *AggNodo_TreeA(struct Albero *nodo, char *parola)
{

if (nodo == NULL)
{
/*Creazione nuovo nodo dell'albero*/
if(!(nodo = (struct Albero*) malloc(sizeof(struct Albero))))
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Imposto il valore dei puntatori ai nodi figli*/
nodo->d = NULL;
nodo->s = NULL;
/*Impostazione della frequenza*/
nodo->Elemento.freq = 1;

/*Allocazione della memoria per la stringa contenete la parola*/
nodo->Elemento.parola =
malloc(strlen(parola)*sizeof(nodo->Elemento.parola));
if(nodo->Elemento.parola == NULL)
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Copia della stringa*/
strcpy(nodo->Elemento.parola,parola);

}
else
{
/*Variabile che restituisce il valore della comparazione parola*/
int conf;
/*Comparazione della parola*/
conf = strcmp(nodo->Elemento.parola,parola);

switch(conf)
{
case (1):
nodo->s = AggNodo_TreeA(nodo->s,parola);
break;
case (-1):
nodo->d = AggNodo_TreeA(nodo->d,parola);
break;
default:
/*Se le parole sono uguali incremento il valore della frequenza*/
nodo->Elemento.freq = nodo->Elemento.freq + 1 ;
break;
}
}
return nodo;
}

/*Funzione di stampa dell'albero passato in input*/
void Print(struct Albero *nodo)
{

if (nodo != NULL)
{

/*Richiamo della funzione sul ramo sinistro*/
Print(nodo->s);

if (count == 10 )
{
printf("\n Premere un tasto per continuare...\n");
getchar();
count = 0;
}
else
{
count++;
}
/*Stampa della parola*/
printf("\n PAROLA: %s ", nodo->Elemento.parola);
printf(" FREQUENZA: %d ", nodo->Elemento.freq);

/*Richiamo della funzione sul ramo destro*/
Print(nodo->d);
}
}
struct Albero * AggNodo_TreeF(struct Albero *sorg, struct Albero *dest)
{
if (dest == NULL)
{
/*Creazione nuovo nodo dell'albero*/
dest = (struct Albero*) malloc(sizeof(struct Albero));
/*Imposto il valore dei puntatori ai nodi figli*/
dest->d = NULL;
dest->s = NULL;
/*Impostazione della frequenza*/
dest->Elemento.freq = sorg->Elemento.freq;
/*Allocazione della memoria per la stringa contenete la parola*/
dest->Elemento.parola =
malloc(strlen(sorg->Elemento.parola)*sizeof(dest->Elemento.parola));
if(dest->Elemento.parola == NULL)
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Copia della stringa*/
strcpy(dest->Elemento.parola,sorg->Elemento.parola);
}
else
{
/*Comparazione Frequenza*/
if ( dest->Elemento.freq < sorg->Elemento.freq)
{
dest->s = AggNodo_TreeF(sorg,dest->s);
}
else
{
dest->d = AggNodo_TreeF(sorg,dest->d);
}
}
return dest;
}

/*Funzione che crea l'abero delle frequenze partendo dall'abero ordinato
per
alfabeto passato in input*/
void CreaAlberoF(struct Albero *sorg)
{
if (sorg != NULL)
{
CreaAlberoF(sorg->s);
if (treeF == NULL)
{
treeF = AggNodo_TreeF(sorg, treeF);
}
else
{
AggNodo_TreeF(sorg, treeF);
}
CreaAlberoF(sorg->d);
}
}
Dec 14 '05 #1
3 1958

"TC" <te******@inwind.it> wrote in message
news:3C******************@twister2.libero.it...
I'm not a C programmer.
This is a good C?
how check the return value of getch or scanf?
and how check for the \n at the end of the text read?
FEOF do not return true if the file (text file) is finish?
if no what use?
/*Inclusione librerie*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*Definizione della strutura Elemento*/
typedef struct
{
char *parola;
unsigned int freq;
}Elem;

/*Definizione della lista delle parole trovate*/
struct Albero
{
Elem Elemento;
struct Albero *s;
struct Albero *d;
};

/*Definizione variabili*/
/*Albero ordinato per alfabeto*/
struct Albero *treeA = NULL;
/*Albero ordinato per frequenze*/
struct Albero *treeF = NULL;

unsigned int count = 0;

/*Definizione funzioni*/
/*Funzione di scansione del file costruzione delle parole e chiamata
della funzione di inseriento nell'albero ordinato per alfabeto*/
void Scansione(FILE *pf);
/*Aggiungi nodo all'albero ordinato per alfabeto*/
struct Albero *AggNodo_TreeA(struct Albero *nodo, char *parola);
/*Stampa albero*/
void Print(struct Albero *nodo);
/*Crea un nuovo albero ordinato per frequenza utilizzando come sorgente
l'albero
ordinato per alfabeto*/
void CreaAlberoF(struct Albero *sorg);
/*Aggiunge un nuovo nodo all'albero ordinato pre frequenze copiando i
valori dal
nodo passato come sorgente*/
struct Albero * AggNodo_TreeF(struct Albero *sorg, struct Albero *dest);
main (void)
{
char scelta;
int fOK;
int g;
/*Nome del file di testo*/
char nf[50];
/*Puntatore al file*/
FILE *pf;
/*MAIN*/
printf("\n Avvio del programma \n");

do
{
fOK = 0;
printf("\nInserire il nome del file di testo che si desidera utilizzare
(Esempio: Testo.txt) \n");

g = fscanf(stdin,"%s",nf); /*Lettura del nome del file */

pf=fopen(nf,"r"); /*Apertura del file in modalità di lettura*/

/*Controllo apertura del file*/
if (pf==NULL)
{
/*Restituzione del messaggio di errore in caso di mancata apertura*/
perror("ERRORE nell'apertura del file");
fOK = 1;

}

}while(fOK != 0);

printf("\n\n");

do
{
scelta = 0;
/*Menù*/
printf("\n 1- Scansione del file di testo \n");
printf("\n 2- Creazione del file delle parole ordinato per frequenza \n");
printf("\n 3- Stampa a video del file delle parole ordinato per frequenza
\n");
printf("\n 4- Stampa a video del file delle parole ordinato per ordine
alfabetico \n");
printf("\n 5- Esci \n");

do
{
scelta = getchar();
}while(scelta!= '1' && scelta!= '2' && scelta!= '3' && scelta!= '4' &&
scelta!= '5');
count=0;
/*Blocco di scelta*/
switch(scelta)
{
case ('1'):
Scansione(pf);
printf("\n Scansione EFFETTUATA! \n");
break;
case ('2'):
CreaAlberoF(treeA);
printf("\n Lista delle parole ordinata per frequenza creata! \n");
break;
case ('3'):
Print(treeF);
break;
case('4'):
Print(treeA) ;
break;
default:
exit(0);
}
}while(scelta!=5);
}

/*Funzione che effettua la scansione del file per individuare le parole
e richiama la funzione di aggiunta di un nuovo elemento all'albero
alfabeto*/
void Scansione (FILE *pf)
{
char *ptr;
char *parola;
char *allocazione;
char c; /*Variabile per la letura dei caratteri dal file di testo*/
char *carattere; /*Variabile utilizzatata per la concatenazione*/
int lung; /*Lunghezza della parola*/

/*Istanziazione e inizializzazione della variabile carattere*/
carattere = malloc(2*sizeof(carattere));
carattere[0]='\0';

/*Istanziazione e inizializzazione della variabile parola*/
parola = malloc(1*sizeof(parola));
parola[0]='\0';

while(!feof(pf)) /*Ripete fino alla fine del file*/
{
c = fgetc(pf);/*Lettura del carattere */

/*Controllo se il carattere è presente nell'alfabeto*/
if (isalpha(c))
{
/*Calcolo della lunghezza della parola */
lung=strlen(parola);

/*Assgnamento del valore alla variabile carattere*/
carattere[0]=c;
carattere[1]='\0';

if(!(allocazione = realloc(parola,(lung+1)*sizeof(parola))))
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
else
{
parola = allocazione;
}
/*lowercase*/
*carattere = tolower(*carattere);
/*Concatenzazione dei caratteri per formare la parola*/
parola = strcat(parola,carattere);
}
else
{
/*Inserimento della parola*/
if ((parola[0]!='\0'))
{

/*Richiamo della funzione che aggiunge le parole nell'albero
ordinato per alfabeto*/
if (treeA == NULL)
{
/*Aggionamemto della radice */
treeA = AggNodo_TreeA(treeA,parola);
}
else
{
AggNodo_TreeA(treeA,parola);
}
/*Reinizializzazione della variabile parola*/
parola= realloc(parola,sizeof(char));
parola[0]='\0';
}
} }
/*Chiusura file*/
fclose(pf);
}

/*Funzione di aggiunta di un nodo all'albero*/
struct Albero *AggNodo_TreeA(struct Albero *nodo, char *parola)
{

if (nodo == NULL)
{
/*Creazione nuovo nodo dell'albero*/
if(!(nodo = (struct Albero*) malloc(sizeof(struct Albero))))
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Imposto il valore dei puntatori ai nodi figli*/
nodo->d = NULL;
nodo->s = NULL;
/*Impostazione della frequenza*/
nodo->Elemento.freq = 1;

/*Allocazione della memoria per la stringa contenete la parola*/
nodo->Elemento.parola =
malloc(strlen(parola)*sizeof(nodo->Elemento.parola));
if(nodo->Elemento.parola == NULL)
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Copia della stringa*/
strcpy(nodo->Elemento.parola,parola);

}
else
{
/*Variabile che restituisce il valore della comparazione parola*/
int conf;
/*Comparazione della parola*/
conf = strcmp(nodo->Elemento.parola,parola);

switch(conf)
{
case (1):
nodo->s = AggNodo_TreeA(nodo->s,parola);
break;
case (-1):
nodo->d = AggNodo_TreeA(nodo->d,parola);
break;
default:
/*Se le parole sono uguali incremento il valore della frequenza*/
nodo->Elemento.freq = nodo->Elemento.freq + 1 ;
break;
}
}
return nodo;
}

/*Funzione di stampa dell'albero passato in input*/
void Print(struct Albero *nodo)
{

if (nodo != NULL)
{

/*Richiamo della funzione sul ramo sinistro*/
Print(nodo->s);

if (count == 10 )
{
printf("\n Premere un tasto per continuare...\n");
getchar();
count = 0;
}
else
{
count++;
}
/*Stampa della parola*/
printf("\n PAROLA: %s ", nodo->Elemento.parola);
printf(" FREQUENZA: %d ", nodo->Elemento.freq);

/*Richiamo della funzione sul ramo destro*/
Print(nodo->d);
}
}
struct Albero * AggNodo_TreeF(struct Albero *sorg, struct Albero *dest)
{
if (dest == NULL)
{
/*Creazione nuovo nodo dell'albero*/
dest = (struct Albero*) malloc(sizeof(struct Albero));
/*Imposto il valore dei puntatori ai nodi figli*/
dest->d = NULL;
dest->s = NULL;
/*Impostazione della frequenza*/
dest->Elemento.freq = sorg->Elemento.freq;
/*Allocazione della memoria per la stringa contenete la parola*/
dest->Elemento.parola =
malloc(strlen(sorg->Elemento.parola)*sizeof(dest->Elemento.parola));
if(dest->Elemento.parola == NULL)
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Copia della stringa*/
strcpy(dest->Elemento.parola,sorg->Elemento.parola);
}
else
{
/*Comparazione Frequenza*/
if ( dest->Elemento.freq < sorg->Elemento.freq)
{
dest->s = AggNodo_TreeF(sorg,dest->s);
}
else
{
dest->d = AggNodo_TreeF(sorg,dest->d);
}
}
return dest;
}

/*Funzione che crea l'abero delle frequenze partendo dall'abero ordinato
per
alfabeto passato in input*/
void CreaAlberoF(struct Albero *sorg)
{
if (sorg != NULL)
{ CreaAlberoF(sorg->s);
if (treeF == NULL)
{
treeF = AggNodo_TreeF(sorg, treeF);
}
else
{
AggNodo_TreeF(sorg, treeF);
} CreaAlberoF(sorg->d); }
}


Whilst I understabnd the C (can read it), I can't see the wood for the
blurry trees. Anyone a C -> English converter?
Dec 14 '05 #2
TC wrote:
I'm not a C programmer.
Well, OK... This is a good C?
Not really.
how check the return value of getch or scanf?
and how check for the \n at the end of the text read?
FEOF do not return true if the file (text file) is finish?
if no what use?
Actually it's not of much use. Please see the FAQ (which, being a good
citizen of comp.lang.c, you've already read, yes?)...

/*Inclusione librerie*/ They are *headers*. They are not *libraries*. #include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*Definizione della strutura Elemento*/
typedef struct
{
char *parola;
unsigned int freq;
}Elem;

/*Definizione della lista delle parole trovate*/
struct Albero
{
Elem Elemento;
struct Albero *s;
struct Albero *d;
};

/*Definizione variabili*/
/*Albero ordinato per alfabeto*/
struct Albero *treeA = NULL;
Since it's static, it would be initialized to NULL anyway. /*Albero ordinato per frequenze*/
struct Albero *treeF = NULL;
Similarly.
unsigned int count = 0;
Similarly (albeit, to 0).

Making these explicit, OTOH, doesn't hurt anything.
/*Definizione funzioni*/
/*Funzione di scansione del file costruzione delle parole e chiamata
della funzione di inseriento nell'albero ordinato per alfabeto*/
void Scansione(FILE *pf);
/*Aggiungi nodo all'albero ordinato per alfabeto*/
struct Albero *AggNodo_TreeA(struct Albero *nodo, char *parola);
/*Stampa albero*/
void Print(struct Albero *nodo);
/*Crea un nuovo albero ordinato per frequenza utilizzando come sorgente
l'albero
ordinato per alfabeto*/
void CreaAlberoF(struct Albero *sorg);
/*Aggiunge un nuovo nodo all'albero ordinato pre frequenze copiando i
valori dal
nodo passato come sorgente*/
struct Albero * AggNodo_TreeF(struct Albero *sorg, struct Albero *dest);
main (void) Implicit int has gone away as of C99. Better would be:
int main(void) {
char scelta;
int fOK;
int g;
/*Nome del file di testo*/
char nf[50];
/*Puntatore al file*/
FILE *pf;
/*MAIN*/
printf("\n Avvio del programma \n");

do
{
fOK = 0;
printf("\nInserire il nome del file di testo che si desidera
utilizzare (Esempio: Testo.txt) \n");

g = fscanf(stdin,"%s",nf); /*Lettura del nome del file */ And what happens if the filename is longer than 49 characters? A buffer
overrun, that's what. *Big* security hole.

Using fgets() would be better.
pf=fopen(nf,"r"); /*Apertura del file in modalità di lettura*/

/*Controllo apertura del file*/
if (pf==NULL)
{
/*Restituzione del messaggio di errore in caso di mancata
apertura*/
perror("ERRORE nell'apertura del file");
fOK = 1;

}

}while(fOK != 0);
Think about it. Evenif you don't successfully open the file you get
here. I don't think that's what you want.
printf("\n\n");

do
{
scelta = 0;
/*Menù*/
printf("\n 1- Scansione del file di testo \n");
printf("\n 2- Creazione del file delle parole ordinato per
frequenza \n");
printf("\n 3- Stampa a video del file delle parole ordinato per
frequenza \n");
printf("\n 4- Stampa a video del file delle parole ordinato per
ordine alfabetico \n");
printf("\n 5- Esci \n");

do
{
scelta = getchar(); Ouch! The function getchar() returns an *int*. How do you expect to
handle an end of file condition? }while(scelta!= '1' && scelta!= '2' && scelta!= '3' && scelta!=
'4' && scelta!= '5');
count=0;
/*Blocco di scelta*/
switch(scelta)
{
case ('1'):
Scansione(pf);
printf("\n Scansione EFFETTUATA! \n");
break;
case ('2'):
CreaAlberoF(treeA);
printf("\n Lista delle parole ordinata per frequenza
creata! \n");
break;
case ('3'):
Print(treeF);
break;
case('4'):
Print(treeA) ;
break;
default:
exit(0);
}
}while(scelta!=5);
}

/*Funzione che effettua la scansione del file per individuare le parole
e richiama la funzione di aggiunta di un nuovo elemento all'albero
alfabeto*/
void Scansione (FILE *pf)
{
char *ptr;
char *parola;
char *allocazione;
char c; /*Variabile per la letura dei caratteri dal file di testo*/ int c; /* for the same reason as stated above */ char *carattere; /*Variabile utilizzatata per la concatenazione*/
int lung; /*Lunghezza della parola*/

/*Istanziazione e inizializzazione della variabile carattere*/
carattere = malloc(2*sizeof(carattere));
carattere[0]='\0';

/*Istanziazione e inizializzazione della variabile parola*/
parola = malloc(1*sizeof(parola));
parola[0]='\0';

while(!feof(pf)) /*Ripete fino alla fine del file*/

Nope. See the FAQ!

At this point, I've become bored. You've posted too much code. The
excessive comments in a language other than that which this newsgroup
uses have become tiring.

In the future, post a relevant, compilable snippet. Deal with one
problem at a time.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 14 '05 #3
TC wrote:
I'm not a C programmer.
This is a good C?
how check the return value of getch or scanf?
and how check for the \n at the end of the text read?
FEOF do not return true if the file (text file) is finish?
if no what use?
It may be better to ask this questions separately with
a small example to illustrate what you mean -- this gives
you a better chance of helpful answers.
Also, a hint as to what the program is supposed to do or
a comment illustrating your problems may help.

/*Inclusione librerie*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> You forgot <ctype.h> for isalpha() and tolower()

/*Definizione della strutura Elemento*/
typedef struct
{
char *parola;
unsigned int freq;
}Elem;

/*Definizione della lista delle parole trovate*/
struct Albero
{
Elem Elemento;
struct Albero *s;
struct Albero *d;
};

/*Definizione variabili*/
/*Albero ordinato per alfabeto*/
struct Albero *treeA = NULL;
/*Albero ordinato per frequenze*/
struct Albero *treeF = NULL;

unsigned int count = 0;

/*Definizione funzioni*/
/*Funzione di scansione del file costruzione delle parole e chiamata
della funzione di inseriento nell'albero ordinato per alfabeto*/
void Scansione(FILE *pf);
/*Aggiungi nodo all'albero ordinato per alfabeto*/
struct Albero *AggNodo_TreeA(struct Albero *nodo, char *parola);
/*Stampa albero*/
void Print(struct Albero *nodo);
/*Crea un nuovo albero ordinato per frequenza utilizzando come sorgente
l'albero
ordinato per alfabeto*/
void CreaAlberoF(struct Albero *sorg);
/*Aggiunge un nuovo nodo all'albero ordinato pre frequenze copiando i
valori dal
nodo passato come sorgente*/
struct Albero * AggNodo_TreeF(struct Albero *sorg, struct Albero *dest);
main (void) Make it explicit:
int main (void) {
char scelta;
Note that single characters are best contained in an int
as character constants are of type int and many functions
return an int which is either EOF (EOF is always <0) or
a character cast to unsigned char cast to int.
int fOK;
int g;
/*Nome del file di testo*/
char nf[50];
/*Puntatore al file*/
FILE *pf;
/*MAIN*/
printf("\n Avvio del programma \n");

do
{
fOK = 0;
printf("\nInserire il nome del file di testo che si desidera
utilizzare (Esempio: Testo.txt) \n");

g = fscanf(stdin,"%s",nf); /*Lettura del nome del file */
Use either fgets() plus sscanf() or fscanf() with a sensible restriction
on the input length, e.g.
fscanf(stdin, "%49s", nf);
Test whether g equals the number of expected input items, i.e.
if (g != 1)
{ /* Handle error here */ }

pf=fopen(nf,"r"); /*Apertura del file in modalità di lettura*/

/*Controllo apertura del file*/
if (pf==NULL)
{
/*Restituzione del messaggio di errore in caso di mancata
apertura*/
perror("ERRORE nell'apertura del file");
fOK = 1;

}

}while(fOK != 0);

printf("\n\n");

do
{
scelta = 0;
/*Menù*/
printf("\n 1- Scansione del file di testo \n");
printf("\n 2- Creazione del file delle parole ordinato per
frequenza \n");
printf("\n 3- Stampa a video del file delle parole ordinato per
frequenza \n");
printf("\n 4- Stampa a video del file delle parole ordinato per
ordine alfabetico \n");
printf("\n 5- Esci \n");

do
{
scelta = getchar();
}while(scelta!= '1' && scelta!= '2' && scelta!= '3' && scelta!=
'4' && scelta!= '5');
See above, here you may make a mistake, depending on whether
plain char has the value range of signed char or the value
range of unsigned char.
Better:
int scelta;
In addition, you forget to check for EOF.
while (EOF != (scelta = getchar()))
if (scelta >= (unsigned char) '1' && scelta <= (unsigned char) '5')
break;

As you are following this up with a switch having a default case,
you do not need special error handling for the case scelta==EOF.
count=0;
/*Blocco di scelta*/
switch(scelta)
{
case ('1'):
Scansione(pf);
printf("\n Scansione EFFETTUATA! \n");
break;
case ('2'):
CreaAlberoF(treeA);
printf("\n Lista delle parole ordinata per frequenza
creata! \n");
break;
case ('3'):
Print(treeF);
break;
case('4'):
Print(treeA) ;
break;
default:
exit(0);
This is rather abrupt. Consider issuing an explaining message
along with exiting.
In addition, if you have dynamically allocated memory somewhere,
then you should free it before the termination of your program.
}
}while(scelta!=5);
return 0; }

/*Funzione che effettua la scansione del file per individuare le parole
e richiama la funzione di aggiunta di un nuovo elemento all'albero
alfabeto*/
void Scansione (FILE *pf)
{
char *ptr;
You do not use this one. char *parola;
char *allocazione;
char c; /*Variabile per la letura dei caratteri dal file di testo*/
char *carattere; /*Variabile utilizzatata per la concatenazione*/
int lung; /*Lunghezza della parola*/
This is an int but the return type of strlen() is size_t

/*Istanziazione e inizializzazione della variabile carattere*/
carattere = malloc(2*sizeof(carattere));
carattere[0]='\0';

/*Istanziazione e inizializzazione della variabile parola*/
parola = malloc(1*sizeof(parola));
parola[0]='\0';

while(!feof(pf)) /*Ripete fino alla fine del file*/
Read the comp.lang.c FAQ, it covers how not to use feof()
{
c = fgetc(pf);/*Lettura del carattere */

/*Controllo se il carattere è presente nell'alfabeto*/
if (isalpha(c))
{
/*Calcolo della lunghezza della parola */
lung=strlen(parola);

/*Assgnamento del valore alla variabile carattere*/
carattere[0]=c;
carattere[1]='\0';

if(!(allocazione =
realloc(parola,(lung+1)*sizeof(parola))))
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
else
{
parola = allocazione;
}
/*lowercase*/
*carattere = tolower(*carattere);
/*Concatenzazione dei caratteri per formare la parola*/
parola = strcat(parola,carattere);
}
else
{
/*Inserimento della parola*/
if ((parola[0]!='\0'))
{

/*Richiamo della funzione che aggiunge le parole
nell'albero
ordinato per alfabeto*/
if (treeA == NULL)
{
/*Aggionamemto della radice */
treeA = AggNodo_TreeA(treeA,parola);
}
else
{
AggNodo_TreeA(treeA,parola);
}
/*Reinizializzazione della variabile parola*/
parola= realloc(parola,sizeof(char));
parola[0]='\0';
}
}
}
/*Chiusura file*/
fclose(pf);
}

/*Funzione di aggiunta di un nodo all'albero*/
struct Albero *AggNodo_TreeA(struct Albero *nodo, char *parola)
{

if (nodo == NULL)
{
/*Creazione nuovo nodo dell'albero*/
if(!(nodo = (struct Albero*) malloc(sizeof(struct Albero))))
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Imposto il valore dei puntatori ai nodi figli*/
nodo->d = NULL;
nodo->s = NULL;
/*Impostazione della frequenza*/
nodo->Elemento.freq = 1;

/*Allocazione della memoria per la stringa contenete la parola*/
nodo->Elemento.parola =
malloc(strlen(parola)*sizeof(nodo->Elemento.parola));
if(nodo->Elemento.parola == NULL)
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Copia della stringa*/
strcpy(nodo->Elemento.parola,parola);

}
else
{
/*Variabile che restituisce il valore della comparazione parola*/
int conf;
/*Comparazione della parola*/
conf = strcmp(nodo->Elemento.parola,parola);

switch(conf)
{
case (1):
nodo->s = AggNodo_TreeA(nodo->s,parola);
break;
case (-1):
nodo->d = AggNodo_TreeA(nodo->d,parola);
break;
default:
/*Se le parole sono uguali incremento il valore della
frequenza*/
nodo->Elemento.freq = nodo->Elemento.freq + 1 ;
break;
}
}
return nodo;
}

/*Funzione di stampa dell'albero passato in input*/
void Print(struct Albero *nodo)
{

if (nodo != NULL)
{

/*Richiamo della funzione sul ramo sinistro*/
Print(nodo->s);

if (count == 10 )
{
printf("\n Premere un tasto per continuare...\n");
getchar();
count = 0;
}
else
{
count++;
}
/*Stampa della parola*/
printf("\n PAROLA: %s ", nodo->Elemento.parola);
printf(" FREQUENZA: %d ", nodo->Elemento.freq);
Correct format:
%u
/*Richiamo della funzione sul ramo destro*/
Print(nodo->d);
}
}
struct Albero * AggNodo_TreeF(struct Albero *sorg, struct Albero *dest)
{
if (dest == NULL)
{
/*Creazione nuovo nodo dell'albero*/
dest = (struct Albero*) malloc(sizeof(struct Albero));
/*Imposto il valore dei puntatori ai nodi figli*/
dest->d = NULL;
dest->s = NULL;
/*Impostazione della frequenza*/
dest->Elemento.freq = sorg->Elemento.freq;
/*Allocazione della memoria per la stringa contenete la parola*/
dest->Elemento.parola =
malloc(strlen(sorg->Elemento.parola)*sizeof(dest->Elemento.parola));
if(dest->Elemento.parola == NULL)
{
printf("\n Errore di allocazione della memoria.\n");
exit(0);
}
/*Copia della stringa*/
strcpy(dest->Elemento.parola,sorg->Elemento.parola);
}
else
{
/*Comparazione Frequenza*/
if ( dest->Elemento.freq < sorg->Elemento.freq)
{
dest->s = AggNodo_TreeF(sorg,dest->s);
}
else
{
dest->d = AggNodo_TreeF(sorg,dest->d);
}
}
return dest;
}

/*Funzione che crea l'abero delle frequenze partendo dall'abero ordinato
per
alfabeto passato in input*/
void CreaAlberoF(struct Albero *sorg)
{
if (sorg != NULL)
{
CreaAlberoF(sorg->s);
if (treeF == NULL)
{
treeF = AggNodo_TreeF(sorg, treeF);
}
else
{
AggNodo_TreeF(sorg, treeF);
}
CreaAlberoF(sorg->d);
}
}


Please correct the above, then come back with _minimal_ examples
to illustrate your problems or an explicit request for a code
review.
Give us all information necessary to help you (this may be more
than you think is necessary, so give as much as you can).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: KN | last post by:
I know both are pretty much the same and it comes down to personal choice. But I have to make the choice for the team. Things so far that I am considering 1. XML documentation in C# -- thats...
29
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
59
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
17
by: Brett | last post by:
I'd like references on where to find some good (quality) icons to use for form and application icons (for the EXE). Most of the free stuff isn't that great looking and there isn't a good...
15
by: Alex L Pavluck | last post by:
I am new to programming other than SAS. I read that C# is a good starting language and I have started to create some simple programs with C# 2005 express edition. Can someone let me know if this...
6
by: Jamiil | last post by:
I am not a programmer by any means, but a dedicated aficionado. I have good understanding of Java and C/C++, and now I would like to learn javascript->ajax, but I don't know where to start. My HTML...
30
by: mistral | last post by:
Neeed good javascript unescape encoder, to protect javascript code. Some advices? Online tool, or ready javascript only. Any opinions about the Javascript Obfuscator:...
244
by: Ajinkya | last post by:
Can anyone suggest me a good compiler for(c/cpp) for windows? I tried dev cpp but its debugging facility is very poor.
76
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.