nicspa92 (Normal User)
Newbie
Messaggi: 6
Iscritto: 29/06/2012
|
Questo topic è stato chiuso dal moderatore Dopo aver caricato in una apposita struttura matricole ed esami devo cancellare un esame ed inserire un nuovo esame. Cosa sto sbagliando ? Allego il programma. Grazie.
Codice sorgente - presumibilmente Plain Text |
/*
Leggere da file una struttura dati matricole ed esami
Il file ha la seguente struttura:
<numero matricole>
<matricola>
<numero esami>
<esame> <descrizione esame> <voto esame>
<esame> <descrizione esame> <voto esame>
..
<matricola>
<numero esami>
<esame> <descrizione esame> <voto esame>
..
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
#include <iostream>
#define MAX_CHARS 100
using namespace std;
/* Data structure matricole */
typedef struct matricole matricole_t;
struct matricole
{
char* matricola;
int nEsami;
} ;
/* Inizializza data structure matricole */
matricole_t * init_matricole( const unsigned size);
/* Data structure esami */
typedef struct esami esami_t;
struct esami
{
char* esame;
char* nomeEsame;
int votoEsame;
char* matricola;
} ;
/* Inizializza data structure esami */
esami_t * init_esami( const unsigned size );
/* Rilascia la memoria allocata per la struttura dati */
void free_matricole( matricole_t *matricole );
void free_esami( esami_t *esami);
/* Main */
main()
{
FILE *fileptr;
char buffer[ MAX_CHARS ];
int nMatricole;
int lMatricole;
char matricola[ MAX_CHARS ];
int nEsami;
char esame[ MAX_CHARS ];
char nomeEsame[ MAX_CHARS ];
int votoEsame;
int pM;
int pE;
int n;
int sVoti;
int sEsami;
int stEsami;
int stVoti;
float mVoti;
int ok;
matricole_t *matricole = NULL;
esami_t *esami = NULL;
printf( "Nome del file? " );
scanf( "%s", buffer );
fileptr = fopen( buffer, "rt" );
if (fileptr == NULL)
{
printf ( "Errore durante l' apertura del file richiesto.\n" );
system("PAUSE");
return 0;
}
pM = 0;
pE = 0;
n = 1;
nMatricole = 0;
fscanf( fileptr, "%d", &nMatricole );
// printf ("Numero matricole: %d\n", nMatricole);
n++;
for (int y=0; y < nMatricole; y++)
{
fscanf( fileptr, "%s", &matricola);
// printf ("Matricola: %s\n", matricola);
nEsami = 0;
fscanf( fileptr, "%d", &nEsami );
// printf ("Numero esami: %d\n", nEsami);
matricole = (matricole_t *)realloc(matricole,sizeof(matricole_t)*n);
matricole[pM].matricola = strdup(matricola);
matricole[pM].nEsami = nEsami;
pM++;
for (int i=0; i < nEsami; i++)
{
fscanf( fileptr, "%s %s %d", &esame , &nomeEsame , &votoEsame );
// printf ("%s %s %s %d %d\n", matricola, esame, nomeEsame, votoEsame, pE);
esami = (esami_t *)realloc(esami,sizeof(esami_t)*n);
esami[pE].matricola = strdup(matricola);
esami[pE].esame = strdup(esame);
esami[pE].nomeEsame = strdup(nomeEsame);
esami[pE].votoEsame = votoEsame;
pE++;
}
}
fclose( fileptr );
int scelta;
for(;
{
system("CLS");
/* Menu operazioni disponibili */
printf("\nGestione di un file contenente studenti ed esami\n");
printf("\n1) Visualizza dati matricole ed esami");
printf("\n2) Visualizza singola matricola");
printf("\n3) Modifica voto esame");
printf("\n4) Inserisci nuovo esame");
printf("\n5) Cancella esame");
printf("\n0) Fine elebaorazione\n");
printf("\nScelta operazione (1..5,0) ");
scanf("%d",&scelta);
while(!getchar()=='\n');
if(!scelta)
break;
/* Richiama funzione scelta */
switch(scelta)
{
/* Visualizza tutto il file matricole ed esami */
case 1:
system("CLS");
stVoti = 0;
for (int i=0; i != pM; i++)
{
printf ("Matricola: %s Numero esami sostenuti: %d\n", matricole.matricola, matricole.nEsami);
stEsami = stEsami + matricole.nEsami;
sVoti = 0;
for (int y=0; y != pE ; y++)
{
if (strcmp (esami[y].matricola, matricole.matricola) == 0)
{
printf ("%s %s %d\n", esami[y].esame, esami[y].nomeEsame, esami[y].votoEsame);
sVoti = sVoti + esami[y].votoEsame;
stVoti = stVoti + esami[y].votoEsame;
}
}
mVoti = (float)sVoti / matricole.nEsami;
printf("Totale voti matricola: %d Media voti matricola: %.2lf\n", sVoti, mVoti);
printf ("\n");
}
mVoti = (float)stVoti / stEsami;
printf("Totale generale voti: %d Media generale voti: %.2lf\n", stVoti, mVoti);
system("PAUSE");
break;
/* Visualizza matricole ed esami */
case 2:
system("CLS");
printf( "Digitare il codice matricola? " );
scanf( "%s", &matricola );
ok = 0;
for (int i=0; i != pM; i++)
{
if (strcmp (matricola, matricole.matricola) == 0)
ok = 1;
}
if (ok != 1)
{
printf ("La matricola richiesta non e' presente\n");
system("PAUSE");
break;
}
system("CLS");
printf ("Situazione esami matricola: %s\n", matricola);
sEsami = 0;
sVoti = 0;
for (int y=0; y != pE ; y++)
{
if (strcmp (esami[y].matricola,matricola) == 0)
{
printf ("%s %s %s %d\n", esami[y].matricola ,esami[y].esame, esami[y].nomeEsame, esami[y].votoEsame);
sEsami = sEsami + 1;
sVoti = sVoti + esami[y].votoEsame;
}
}
mVoti = (float)sVoti / sEsami;
printf("Esami sostenuti: %d Totale voti: %d Media voti : %.2lf\n", sEsami, sVoti, mVoti);
system("PAUSE");
break;
/* Modifica voto esame */
case 3:
system("CLS");
printf( "Digitare il codice matricola? " );
scanf( "%s", &matricola );
ok = 0;
for (int i=0; i != pM; i++)
{
if (strcmp (matricola, matricole.matricola) == 0)
ok = 1;
}
if (ok != 1)
{
printf ("La matricola richiesta non e' presente\n");
system("PAUSE");
break;
}
printf( "Digitare il codice esame? " );
scanf( "%s", &esame );
ok = 0;
for (int y=0; y != pE ; y++)
{
if ((strcmp (matricola,esami[y].matricola) == 0) &&
(strcmp (esame,esami[y].esame) == 0))
{
ok = 1;
strcpy(nomeEsame,esami[y].nomeEsame);
votoEsame = esami[y].votoEsame;
}
}
if (ok != 1)
{
printf ("L' esame richiesto non e' presente\n");
system("PAUSE");
break;
}
printf ("Esame : %s Voto: %d\n", nomeEsame, votoEsame);
printf( "Digitare il nuovo voto ? " );
scanf( "%d", &votoEsame);
/* Aggiorno il voto dell' esame richiesto */
for (int y=0; y != pE ; y++)
{
if ((strcmp (matricola,esami[y].matricola) == 0) &&
(strcmp (esame,esami[y].esame) == 0))
esami[y].votoEsame = votoEsame;
}
system("PAUSE");
break;
/* Inserisce nuovo esame */
case 4:
system("CLS");
printf( "Digitare il codice matricola " );
scanf( "%s", &matricola );
ok = 0;
for (int i=0; i != pM; i++)
{
if (strcmp (matricola, matricole.matricola) == 0)
ok = 1;
}
if (ok != 1)
{
printf ("La matricola richiesta non e' presente\n");
system("PAUSE");
break;
}
printf( "Digitare il codice esame " );
scanf( "%s", &esame );
printf( "Digitare la descrizione esame" );
scanf( "%s", &nomeEsame );
printf( "Digitare il voto esame" );
scanf( "%d", &votoEsame);
pE++;
n++;
esami = (esami_t *)realloc(esami,sizeof(esami_t)*n);
esami[pE].matricola = strdup(matricola);
esami[pE].esame = strdup(esame);
esami[pE].nomeEsame = strdup(nomeEsame);
esami[pE].votoEsame = votoEsame;
break;
/* Cancella esame */
case 5:
system("CLS");
printf( "Digitare il codice matricola? " );
scanf( "%s", &matricola );
ok = 0;
for (int i=0; i != pM; i++)
{
if (strcmp (matricola, matricole.matricola) == 0)
ok = 1;
}
if (ok != 1)
{
printf ("La matricola richiesta non e' presente\n");
system("PAUSE");
break;
}
printf( "Digitare il codice esame? " );
scanf( "%s", &esame );
ok = 0;
for (int y=0; y != pE ; y++)
{
if ((strcmp (matricola,esami[y].matricola) == 0) &&
(strcmp (esame,esami[y].esame) == 0))
{
ok = 1;
strcpy(nomeEsame,esami[y].nomeEsame);
votoEsame = esami[y].votoEsame;
}
}
if (ok != 1)
{
printf ("L' esame richiesto non e' presente\n");
system("PAUSE");
break;
}
for (int y=0; y != pE ; y++)
{
if ((strcmp (matricola,esami[y].matricola) == 0) &&
(strcmp (esame,esami[y].esame) == 0))
{
free (esami);
}
}
break;
}
}
free_matricole (matricole);
free_esami (esami);
}
/* Inizializza la struttura dati */
matricole_t * init_matricole( const unsigned size )
{
matricole_t *matricole = (matricole_t *) malloc( size * sizeof(matricole_t) );
if( matricole == NULL )
return NULL;
return matricole;
}
/* Inizializza la struttura dati */
esami_t * init_esami( const unsigned size )
{
esami_t *esami = (esami_t *) malloc( size * sizeof(esami_t) );
if( esami == NULL )
return NULL;
return esami;
}
/* Rilascia la memoria allocata per la struttura dati */
void free_matricole( matricole_t *matricole )
{
if( matricole != NULL )
free( matricole );
}
/* Rilascia la memoria allocata per la struttura dati */
void free_esami( esami_t *esami )
{
if( esami != NULL )
free( esami );
}
|