Tipo...
File escursione.h:
#ifndef ESCURSIONE_H
#define ESCURSIONE_H
#define QMAXESC 64 // la quantita' massima di escursioni
#define LMAXSTR 255 // la lunghezza massima delle stringhe
typedef struct ESCURSIONE {
char lp[LMAXSTR+1]; // luogo di partenza
char la[LMAXSTR+1]; // luogo di arrivo
unsigned int d; // la distanza (in metri)
unsigned int t; // il tempo richiesto (in minuti)
} ESCURSIONE;
ESCURSIONE *CreaEscursione( void );
void DistruggiEscursione( ESCURSIONE **e );
int AggiungiEscursione( ESCURSIONE **esc, unsigned int *qEsc );
void EliminaEscursione( ESCURSIONE **esc, unsigned int *qEsc );
void ImpostaEscursione( ESCURSIONE *e,
const char *partenza, const char *arrivo,
double km, unsigned int h, unsigned int m );
void ImpostaPartenzaEscursione( ESCURSIONE *e, const char *partenza );
void ImpostaArrivoEscursione( ESCURSIONE *e, const char *arrivo );
void ImpostaTempoEscursione( ESCURSIONE *e, unsigned int h, unsigned int m );
void ImpostaDistanzaEscursione( ESCURSIONE *e, double km );
void StampaEscursione( ESCURSIONE *e );
#endif // ESCURSIONE_H
File escursione.c
#include "escursione.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*==============================================================================
Alloca dinamicamente una struttura di tipo ESCURSIONE impostata interamente su
zeri e ne restituisce il puntatore. In caso di fallimento restituisce NULL.
==============================================================================*/
ESCURSIONE *CreaEscursione( void ) {
return calloc( 1, sizeof(ESCURSIONE) );
}
/*==============================================================================
Distrugge una struttura di tipo ESCURSIONE precedentemente allocata in memoria
dinamica e ne imposta il puntatore su NULL.
==============================================================================*/
void DistruggiEscursione( ESCURSIONE **e ) {
if( e ) { free( *e ); *e = NULL; }
}
/*==============================================================================
Aggiunge in cima all'array esc il puntatore ad una struttura di tipo ESCURSIONE
appositamente allocata in memoria dinamica e aggiorna il contatore dei puntatori
contenuti nell'array. Se l'aggiunta va a buon fine restituisce 0; in caso
contrario restituisce un codice di errore.
==============================================================================*/
int AggiungiEscursione( ESCURSIONE **esc, unsigned int *qEsc ) {
if( esc != NULL && qEsc != NULL ) {
if( *qEsc < QMAXESC ) {
esc[*qEsc] = CreaEscursione();
if( esc[*qEsc] != NULL ) {
++(*qEsc);
return 0; // tutto bene!
} else return 3; // allocazione fallita
} else return 2; // array saturo
} else return 1; // parametro non valido
}
/*==============================================================================
Elimina dalla cima dell'array esc il puntatore ad una struttura di tipo
ESCURSIONE appositamente allocata in memoria dinamica e aggiorna il contatore
dei puntatori contenuti nell'array.
==============================================================================*/
void EliminaEscursione( ESCURSIONE **esc, unsigned int *qEsc ) {
if( *qEsc > 0 ) DistruggiEscursione( &esc[--*qEsc] );
}
/*==============================================================================
Imposta tutti i campi della struttura di tipo ESCURSIONE coerentemente coi
parametri passati.
==============================================================================*/
void ImpostaEscursione( ESCURSIONE *e,
const char *partenza, const char *arrivo,
double km, unsigned int h, unsigned int m ) {
ImpostaPartenzaEscursione( e, partenza );
ImpostaArrivoEscursione( e, arrivo );
ImpostaDistanzaEscursione( e, km );
ImpostaTempoEscursione( e, h, m );
}
/*==============================================================================
Funzione ausiliaria per ImpostaPartenzaEscursione() e ImpostaArrivoEscursione().
==============================================================================*/
void ImpostaStringa( char *dest, const char *orig ) {
if( dest != NULL && orig != NULL )
strncpy( dest, orig, LMAXSTR );
else if( dest != NULL ) *dest = 'Tipo...
File escursione.h:
#ifndef ESCURSIONE_H
#define ESCURSIONE_H
#define QMAXESC 64 // la quantita' massima di escursioni
#define LMAXSTR 255 // la lunghezza massima delle stringhe
typedef struct ESCURSIONE {
char lp[LMAXSTR+1]; // luogo di partenza
char la[LMAXSTR+1]; // luogo di arrivo
unsigned int d; // la distanza (in metri)
unsigned int t; // il tempo richiesto (in minuti)
} ESCURSIONE;
ESCURSIONE *CreaEscursione( void );
void DistruggiEscursione( ESCURSIONE **e );
int AggiungiEscursione( ESCURSIONE **esc, unsigned int *qEsc );
void EliminaEscursione( ESCURSIONE **esc, unsigned int *qEsc );
void ImpostaEscursione( ESCURSIONE *e,
const char *partenza, const char *arrivo,
double km, unsigned int h, unsigned int m );
void ImpostaPartenzaEscursione( ESCURSIONE *e, const char *partenza );
void ImpostaArrivoEscursione( ESCURSIONE *e, const char *arrivo );
void ImpostaTempoEscursione( ESCURSIONE *e, unsigned int h, unsigned int m );
void ImpostaDistanzaEscursione( ESCURSIONE *e, double km );
void StampaEscursione( ESCURSIONE *e );
#endif // ESCURSIONE_H
File escursione.c
#include "escursione.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*==============================================================================
Alloca dinamicamente una struttura di tipo ESCURSIONE impostata interamente su
zeri e ne restituisce il puntatore. In caso di fallimento restituisce NULL.
==============================================================================*/
ESCURSIONE *CreaEscursione( void ) {
return calloc( 1, sizeof(ESCURSIONE) );
}
/*==============================================================================
Distrugge una struttura di tipo ESCURSIONE precedentemente allocata in memoria
dinamica e ne imposta il puntatore su NULL.
==============================================================================*/
void DistruggiEscursione( ESCURSIONE **e ) {
if( e ) { free( *e ); *e = NULL; }
}
/*==============================================================================
Aggiunge in cima all'array esc il puntatore ad una struttura di tipo ESCURSIONE
appositamente allocata in memoria dinamica e aggiorna il contatore dei puntatori
contenuti nell'array. Se l'aggiunta va a buon fine restituisce 0; in caso
contrario restituisce un codice di errore.
==============================================================================*/
int AggiungiEscursione( ESCURSIONE **esc, unsigned int *qEsc ) {
if( esc != NULL && qEsc != NULL ) {
if( *qEsc < QMAXESC ) {
esc[*qEsc] = CreaEscursione();
if( esc[*qEsc] != NULL ) {
++(*qEsc);
return 0; // tutto bene!
} else return 3; // allocazione fallita
} else return 2; // array saturo
} else return 1; // parametro non valido
}
/*==============================================================================
Elimina dalla cima dell'array esc il puntatore ad una struttura di tipo
ESCURSIONE appositamente allocata in memoria dinamica e aggiorna il contatore
dei puntatori contenuti nell'array.
==============================================================================*/
void EliminaEscursione( ESCURSIONE **esc, unsigned int *qEsc ) {
if( *qEsc > 0 ) DistruggiEscursione( &esc[--*qEsc] );
}
/*==============================================================================
Imposta tutti i campi della struttura di tipo ESCURSIONE coerentemente coi
parametri passati.
==============================================================================*/
void ImpostaEscursione( ESCURSIONE *e,
const char *partenza, const char *arrivo,
double km, unsigned int h, unsigned int m ) {
ImpostaPartenzaEscursione( e, partenza );
ImpostaArrivoEscursione( e, arrivo );
ImpostaDistanzaEscursione( e, km );
ImpostaTempoEscursione( e, h, m );
}
/*==============================================================================
Funzione ausiliaria per ImpostaPartenzaEscursione() e ImpostaArrivoEscursione().
==============================================================================*/
void ImpostaStringa( char *dest, const char *orig ) {
if( dest != NULL && orig != NULL )
strncpy( dest, orig, LMAXSTR );
else if( dest != NULL ) *dest = '{parsed_message}';
}
/*==============================================================================
Imposta il campo lp (luogo di partenza) della struttura ESCURSIONE, curando che
non venga superato il limite imposto dalle dimensioni dello spazio disponibile.
==============================================================================*/
void ImpostaPartenzaEscursione( ESCURSIONE *e, const char *partenza ) {
ImpostaStringa( e->lp, partenza );
}
/*==============================================================================
Imposta il campo la (luogo di arrivo) della struttura ESCURSIONE, curando che
non venga superato il limite imposto dalle dimensioni dello spazio disponibile.
==============================================================================*/
void ImpostaArrivoEscursione( ESCURSIONE *e, const char *arrivo ) {
ImpostaStringa( e->la, arrivo );
}
/*==============================================================================
Imposta il campo t (tempo) della struttura ESCURSIONE, "accorpando" i valori
delle ore e dei minuti.
==============================================================================*/
void ImpostaTempoEscursione( ESCURSIONE *e, unsigned int h, unsigned int m ) {
if( e != NULL ) e->t = h*60 + m;
}
/*==============================================================================
Imposta il campo d (distanza) della struttura ESCURSIONE, trasformando in
metri la quantita' dei km passata. Le quantita' inferiori al metro sono soggette
a un processo d'arrotondamento.
==============================================================================*/
void ImpostaDistanzaEscursione( ESCURSIONE *e, double km ) {
if( e != NULL ) {
if( km < 0.0 ) km = -km;
e->d = round( km*1000.0 );
}
}
/*==============================================================================
Visualizza in console il contenuto della struttura ESCURSIONE.
==============================================================================*/
void StampaEscursione( ESCURSIONE *e ) {
if( e != NULL ) {
unsigned int ore = e->t/60;
unsigned int min = e->t%60;
const char ps1[4] = "ea";
const char ps2[4] = "io";
printf( "Partenza: %s\n", e->lp );
printf( "Arrivo: %s\n", e->la );
printf( "Distanza: %.2lf km\n", ((double)e->d)/1000.0 );
printf( "Tempo: %u or%c", ore, ps1[ore==1] );
if( min ) printf( " e %u minut%c", min, ps2[min==1] );
printf( "\n" );
}
else {
printf( "Escursione non valida\n" );
}
}
Il file escursione.c è in effetti un po' lunghetto, però "isola" alcuni procedimenti in un file "a parte", cosicché uno possa poi pensare al resto delle operazioni con un po' più leggerezza. Ad esempio...
File main.c:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "escursione.h"
int main() {
ESCURSIONE *esc[QMAXESC]; // array di puntatori a strutture ESCURSIONE
unsigned int i, qEsc = 0; // qEsc = quantita' di escursioni
int errore = 0; // zero: nessun errore
memset( esc, 0, sizeof(ESCURSIONE*)*QMAXESC );
for( i=0; i<3 && !errore; ++i )
errore = AggiungiEscursione( esc, &qEsc );
if( !errore ) {
ImpostaEscursione( esc[0], "Palermo", "Trento", 1632, 1, 15 );
ImpostaEscursione( esc[1], "Olbia", "Vieste", 600.5, 6, 45 );
ImpostaEscursione( esc[2], "Loano", "Pietra Ligure", 0.5, 0, 5 );
for( i=0; i<qEsc; ++i ) {
StampaEscursione( esc[i] );
printf( "\n" );
}
while( qEsc != 0 )
EliminaEscursione( esc, &qEsc );
}
return 0;
}
';
}
/*==============================================================================
Imposta il campo lp (luogo di partenza) della struttura ESCURSIONE, curando che
non venga superato il limite imposto dalle dimensioni dello spazio disponibile.
==============================================================================*/
void ImpostaPartenzaEscursione( ESCURSIONE *e, const char *partenza ) {
ImpostaStringa( e->lp, partenza );
}
/*==============================================================================
Imposta il campo la (luogo di arrivo) della struttura ESCURSIONE, curando che
non venga superato il limite imposto dalle dimensioni dello spazio disponibile.
==============================================================================*/
void ImpostaArrivoEscursione( ESCURSIONE *e, const char *arrivo ) {
ImpostaStringa( e->la, arrivo );
}
/*==============================================================================
Imposta il campo t (tempo) della struttura ESCURSIONE, "accorpando" i valori
delle ore e dei minuti.
==============================================================================*/
void ImpostaTempoEscursione( ESCURSIONE *e, unsigned int h, unsigned int m ) {
if( e != NULL ) e->t = h*60 + m;
}
/*==============================================================================
Imposta il campo d (distanza) della struttura ESCURSIONE, trasformando in
metri la quantita' dei km passata. Le quantita' inferiori al metro sono soggette
a un processo d'arrotondamento.
==============================================================================*/
void ImpostaDistanzaEscursione( ESCURSIONE *e, double km ) {
if( e != NULL ) {
if( km < 0.0 ) km = -km;
e->d = round( km*1000.0 );
}
}
/*==============================================================================
Visualizza in console il contenuto della struttura ESCURSIONE.
==============================================================================*/
void StampaEscursione( ESCURSIONE *e ) {
if( e != NULL ) {
unsigned int ore = e->t/60;
unsigned int min = e->t%60;
const char ps1[4] = "ea";
const char ps2[4] = "io";
printf( "Partenza: %s\n", e->lp );
printf( "Arrivo: %s\n", e->la );
printf( "Distanza: %.2lf km\n", ((double)e->d)/1000.0 );
printf( "Tempo: %u or%c", ore, ps1[ore==1] );
if( min ) printf( " e %u minut%c", min, ps2[min==1] );
printf( "\n" );
}
else {
printf( "Escursione non valida\n" );
}
}
Il file escursione.c è in effetti un po' lunghetto, però "isola" alcuni procedimenti in un file "a parte", cosicché uno possa poi pensare al resto delle operazioni con un po' più leggerezza. Ad esempio...
File main.c:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "escursione.h"
int main() {
ESCURSIONE *esc[QMAXESC]; // array di puntatori a strutture ESCURSIONE
unsigned int i, qEsc = 0; // qEsc = quantita' di escursioni
int errore = 0; // zero: nessun errore
memset( esc, 0, sizeof(ESCURSIONE*)*QMAXESC );
for( i=0; i<3 && !errore; ++i )
errore = AggiungiEscursione( esc, &qEsc );
if( !errore ) {
ImpostaEscursione( esc[0], "Palermo", "Trento", 1632, 1, 15 );
ImpostaEscursione( esc[1], "Olbia", "Vieste", 600.5, 6, 45 );
ImpostaEscursione( esc[2], "Loano", "Pietra Ligure", 0.5, 0, 5 );
for( i=0; i<qEsc; ++i ) {
StampaEscursione( esc[i] );
printf( "\n" );
}
while( qEsc != 0 )
EliminaEscursione( esc, &qEsc );
}
return 0;
}
ATTENZIONE! Sono un hobbista e l'affidabilità delle mie conoscenze informatiche è molto limitata. Non prendere come esempio il codice che scrivo, perché non ho alcuna formazione accademica e rischieresti di apprendere pratiche controproducenti.