Questo topic e' stato chiuso dal moderatore.
03/07/12 19:31
nicspa92
Il problema che devo risolvere è quello di caricare in una apposita struttura un file contenente matricole ed esami, quindi permettere la variazione di un esame, l' inserimento di un nuovo esame e la cancellazione di un esame. La struttura del file è la seguente:
numero totale matricole
matricola
numero esami matricola
esame1 descrizione voto
esame2 descrizione voto
matricola
numero esami matricola
.....
Mi sono rimasti due problemi: il primo è che una volta effettuato l' inserimento del nuovo esame, quanto vado a visualizzare gli esami della matricola non vedo l' esame appena inserito, il secondo è che quando vado a cancellare un esame ho un appcrash sul modulo ntdll.dll.
Vi ringrazio già in anticipo per l'aiuto!!
/*
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++;
n++;
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[i].matricola, matricole[i].nEsami);
stEsami = stEsami + matricole[i].nEsami;
sVoti = 0;
for (int y=0; y != pE ; y++)
{
if (strcmp (esami[y].matricola, matricole[i].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[i].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[i].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[i].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[i].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);
n++;
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;
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[i].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))
{
for(int i=y;y != pE ;i++)
{
esami[i].matricola = esami[i + 1].matricola;
esami[i].esame = esami[i + 1].esame;
esami[i].nomeEsame = esami[i + 1].nomeEsame;
esami[i].votoEsame = esami[i + 1].votoEsame;
}
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 );
}
numero totale matricole
matricola
numero esami matricola
esame1 descrizione voto
esame2 descrizione voto
matricola
numero esami matricola
.....
Mi sono rimasti due problemi: il primo è che una volta effettuato l' inserimento del nuovo esame, quanto vado a visualizzare gli esami della matricola non vedo l' esame appena inserito, il secondo è che quando vado a cancellare un esame ho un appcrash sul modulo ntdll.dll.
Vi ringrazio già in anticipo per l'aiuto!!
/*
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++;
n++;
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[i].matricola, matricole[i].nEsami);
stEsami = stEsami + matricole[i].nEsami;
sVoti = 0;
for (int y=0; y != pE ; y++)
{
if (strcmp (esami[y].matricola, matricole[i].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[i].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[i].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[i].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[i].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);
n++;
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;
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[i].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))
{
for(int i=y;y != pE ;i++)
{
esami[i].matricola = esami[i + 1].matricola;
esami[i].esame = esami[i + 1].esame;
esami[i].nomeEsame = esami[i + 1].nomeEsame;
esami[i].votoEsame = esami[i + 1].votoEsame;
}
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 );
}
aaa