codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define n 10
struct esame {
int ordine;
char cognome[15];
int voto;
} esa[n];
int main()
{
int ord, vot, app;
char cogn[15];
int i = 0;
int j, k, v;
char buffer[15];
FILE *cfPtr;
if (( cfPtr = fopen("ingresso.txt", "w")) == NULL ) {
printf("Il file non può essere aperto.\n");
printf("Inserisci EOF per terminare\n");
} else {
printf("Inserisci l'ordine di consegna, il cognome dello studente e il voto : \n");
scanf("%d%s%d", &ord, cogn, &vot);
while (!feof(stdin)) {
fprintf(cfPtr, "%d %s %d\n", ord, cogn, vot);
scanf("%d%s%d", &ord, cogn, &vot);
}
fclose(cfPtr);
}
if (( cfPtr = fopen("ingresso.txt", "r")) == NULL ) {
printf("Il file non può essere aperto.\n");
} else {
fscanf(cfPtr, "%d %s %d", &ord, cogn, &vot);
esa[i].ordine = ord;
strcpy(esa[i].cognome, cogn);
esa[i].voto = vot;
while (!feof(cfPtr)) {
i++;
fscanf(cfPtr, "%d %s %d", &ord, cogn, &vot);
esa[i].ordine = ord;
strcpy(esa[i].cognome, cogn);
esa[i].voto = vot;
}
fclose(cfPtr);
}
/* ordino le informazioni in funzione dell'rodine alfabetico degli studenti */
for ( j = 0; j < i - 1; j++ ) {
for ( k = j + 1; k < i; k++ ) {
v = strcmp( esa[j].cognome, esa[k].cognome);
if ( v > 0 ) {
strcpy(buffer, esa[j].cognome);
strcpy(esa[j].cognome, esa[k].cognome);
strcpy(esa[k].cognome, buffer);
app = esa[j].ordine;
esa[j].ordine = esa[k].ordine;
esa[k].ordine = app;
app = esa[j].voto;
esa[j].voto = esa[k].voto;
esa[k].voto = app;
}
}
}
printf("\n");
if (( cfPtr = fopen("uscita.txt", "w")) == NULL ) {
printf("Il file non può essere aperto.\n");
} else {
printf("%27s","ORDINE ALFABETICO\n");
printf("%13s%14s%10s", "COGNOME", "NUMERO ORDINE", "VOTO\n");
fprintf(cfPtr, "%27s", "ORDINE ALFABETICO\n");
fprintf(cfPtr, "%13s%14s%10s", "COGNOME", "NUMERO ORDINE", "VOTO\n");
for ( j = 0; j < i; j++ ) {
printf("%13s%14d%9d\n", esa[j].cognome, esa[j].ordine, esa[j].voto);
fprintf(cfPtr, "%13s%14d%9d\n", esa[j].cognome, esa[j].ordine, esa[j].voto);
}
fclose(cfPtr);
}
printf("\n\n");
;
for ( j = 0; j < i - 1; j++ ) {
for ( k = j + 1; k < i; k++ ) {
if (esa[j].ordine > esa[k].ordine ) {
strcpy(buffer, esa[j].cognome);
strcpy(esa[j].cognome, esa[k].cognome);
strcpy(esa[k].cognome, buffer);
app = esa[j].ordine;
esa[j].ordine = esa[k].ordine;
esa[k].ordine = app;
app = esa[j].voto;
esa[j].voto = esa[k].voto;
esa[k].voto = app;
}
}
}
if (( cfPtr = fopen( "uscita.txt", "a")) == NULL ) {
printf("Il file non può essere aperto.\n");
} else {
printf("%27s", "ORDINE CONSEGNA\n");
printf("%13s%14s%10s", "NUMERO ORDINE", "VOTO", "COGNOME\n");
fprintf(cfPtr, "\n\n%27s", "ORDINE CONSEGNA\n");
fprintf(cfPtr, "%13s%14s%10s", "NUMERO ORDINE", "VOTO", "COGNOME\n");
for ( j = 0; j < i; j++) {
printf("%13d%14d%9s\n", esa[j].ordine, esa[j].voto, esa[j].cognome);
fprintf(cfPtr, "%13d%14d%9s\n", esa[j].ordine, esa[j].voto, esa[j].cognome);
}
fclose(cfPtr);
}
printf("\n\n");
for ( j = 0; j < i - 1; j++ ) {
for ( k = j + 1; k < i; k++ ) {
if (esa[j].voto < esa[k].voto ) {
strcpy(buffer, esa[j].cognome);
strcpy(esa[j].cognome, esa[k].cognome);
strcpy(esa[k].cognome, buffer);
app = esa[j].voto;
esa[j].voto = esa[k].voto;
esa[k].voto = app;
app = esa[j].ordine;
esa[j].ordine = esa[k].ordine;
esa[k].ordine = app;
}
}
}
if ((cfPtr = fopen("uscita.txt", "a")) == NULL) {
printf("Il file non può essere aperto.\n");
} else {
printf("%27s", "ORDINE DI VOTO\n");
printf("%13s%14s%10s", "VOTO", "COGNOME", "ORDINE\n");
fprintf(cfPtr, "\n\n%27s", "ORDINE DI VOTO\n");
fprintf(cfPtr,"%13s%14s%10s", "VOTO", "COGNOME", "ORDINE\n");
for ( j = 0; j < i; j++ ) {
printf("%13d%14s%9d\n", esa[j].voto, esa[j].cognome, esa[j].ordine);
fprintf(cfPtr, "%13d%14s%9d\n", esa[j].voto, esa[j].cognome, esa[j].ordine);
}
fclose(cfPtr);
}
return 0;
}
Grazie!