codice:
#include <stdio.h>
#include <stdlib.h>
#define n 2
/*
*
*/
int main() {
struct studenti {
int ordi;
char cogno[20];
int vot;
}studente[n];
int ordine;
char cognome[20];
int voto;
int v;
int z = 0;
int i = 0;
int j = 0;
char buffer[20];
int ordapp;
int votapp;
FILE *cfPtr; /*cfPtr = punta al file ingresso.txt*/
/*fopen apre il file*/
if ((cfPtr = fopen("ingresso.txt", "w")) == NULL){
printf ("Il file non può essere aperto");
}
else{
printf ("Inserisci ordine, cognome, ed il voto dei 10 studenti: \n");
printf ("Inserisci EOF per terminare l'inserimento \n");
scanf ("%d%s%d", &ordine, cognome, &voto);
/*scrive nel file*/
while (!feof(stdin)){
fprintf (cfPtr, "%d %s %d\n", ordine, cognome, voto);
scanf ("%d%s%d", &ordine, cognome, &voto);
}
fclose(cfPtr);
}
/*fopen apre il file*/
if ((cfPtr = fopen("ingresso.txt", "r")) == NULL){
printf ("Il file non può essere aperto");
}
else{
printf ("%15s%15s%15s","Ordine","Cognome","Voto\n");
fscanf (cfPtr, "%d%s%d", &ordine, cognome, &voto);
studente[i].ordi = ordine;
strcpy(studente[i].cogno,cognome);
studente[i].vot = voto;
i++;
while (!feof(cfPtr)){
printf ("%15d%15s%14d\n", ordine, cognome, voto);
fscanf (cfPtr, "%d%s%d", &ordine, cognome, &voto);
studente[i].ordi = ordine;
strcpy(studente[i].cogno,cognome);
studente[i].vot = voto;
i++;
}
}
fclose(cfPtr);
system("PAUSE");
for (j = 0; j < i - 2; j++){
for (z = j + 1; z <= i - 1; z++){
v = strcmp(studente[j].cogno, studente[z].cogno);
if (v > 0){
strcpy(buffer, studente[z].cogno);
strcpy(studente[z].cogno, studente[j].cogno);
strcpy(studente[j].cogno, buffer);
ordapp = studente[z].ordi;
studente[z].ordi = studente[j].ordi;
studente[j].ordi = ordapp;
votapp = studente[z].vot;
studente[z].vot = studente[j].vot;
studente[j].vot = votapp;
}
}
}
printf ("\nStampa ordinata per Cognome\n");
printf ("%25s%25s%30s\n","ORDINE","COGNOME","VOTO");
for (j = 0; j < i - 1; j++){
printf ("%25d%25s%30d", studente[j].ordi, studente[j].cogno, studente[j].vot);
}
if ((cfPtr = fopen("uscita.txt", "w")) == NULL){
printf ("Il file non può essere aperto");
}
else{
j = 0;
scanf ("%d%s%d", studente[j].ordi, studente[j].cogno, studente[j].vot);
/*scrive nel file*/
while (!feof(stdin)){
fprintf (cfPtr, "%d %s %d\n", ordine, cognome, voto);
scanf ("%d%s%d", studente[j].ordi, studente[j].cogno, studente[j].vot);
j++;
}
for (j = 0; j < i - 2; j++){
for (z = j + 1; z <= i - 1; z++){
if (studente[j].ordi > studente[z].ordi){
strcpy(buffer, studente[z].cogno);
strcpy(studente[z].cogno, studente[j].cogno);
strcpy(studente[j].cogno, buffer);
ordapp = studente[z].ordi;
studente[z].ordi = studente[j].ordi;
studente[j].ordi = ordapp;
votapp = studente[z].vot;
studente[z].vot = studente[j].vot;
studente[j].vot = votapp;
}
}
}
printf ("\nStampa ordinata per Ordine di Consegna\n");
printf ("%25s%25s%30s\n","ORDINE","COGNOME","VOTO");
for (j = 0; j < i - 1; j++){
printf ("%25d%25s%30d", studente[j].ordi, studente[j].cogno, studente[j].vot);
}
j = 0;
scanf ("%d%s%d", studente[j].ordi, studente[j].cogno, studente[j].vot);
/*scrive nel file*/
while (!feof(stdin)){
fprintf (cfPtr, "%d %s %d\n", ordine, cognome, voto);
scanf ("%d%s%d", studente[j].ordi, studente[j].cogno, studente[j].vot);
j++;
}
for (j = 0; j < i - 2; j++){
for (z = j + 1; z <= i - 1; z++){
if (studente[j].vot < studente[z].vot){
strcpy(buffer, studente[z].cogno);
strcpy(studente[z].cogno, studente[j].cogno);
strcpy(studente[j].cogno, buffer);
ordapp = studente[z].ordi;
studente[z].ordi = studente[j].ordi;
studente[j].ordi = ordapp;
votapp = studente[z].vot;
studente[z].vot = studente[j].vot;
studente[j].vot = votapp;
}
}
}
printf ("\nStampa ordinata per Voto\n");
printf ("%25s%25s%30s\n","ORDINE","COGNOME","VOTO");
for (j = 0; j < i - 1; j++){
printf ("%25d%25s%30d", studente[j].ordi, studente[j].cogno, studente[j].vot);
}
j = 0;
scanf ("%d%s%d", studente[j].ordi, studente[j].cogno, studente[j].vot);
/*scrive nel file*/
while (!feof(stdin)){
fprintf (cfPtr, "%d %s %d\n", ordine, cognome, voto);
scanf ("%d%s%d", studente[j].ordi, studente[j].cogno, studente[j].vot);
j++;
}
fclose(cfPtr);
}
return 0;
}