Così?
codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DIM 50
#define N 20+1
#define M 30+1
struct info {
char nome[N];
int matricola;
int voto_laurea;
};
void vote(struct info *studenti, int c);
void freshman(struct info *studenti, int c);
void name(struct info *studenti);
int main()
{
struct info studenti[MAX_DIM];
char riga[M];
int i = 0, cnt = 0, somma_voti = 0, key;
float media_voti;
FILE* fp;
fp = fopen("Dati.txt","r");
if(fp == NULL) {
printf("E' stato commesso un errorenell'apertura del file.");
exit(0); }
while((fgets(riga,100,fp) != NULL) && (i <= MAX_DIM)) {
sscanf(riga,"%s %d %d",studenti[i].nome, &studenti[i].matricola, &studenti[i].voto_laurea);
cnt++;
somma_voti += studenti[i].voto_laurea;
i++;
}
media_voti= (float) (somma_voti/cnt);
printf("La media dei voti di laurea e': %.2f\n\n",media_voti);
printf("I dati sono stati acquisiti. Sono possibili le seguenti operazioni:\n\n");
/* Ordine crescente */
printf("1. ordinare i dati per nome;\n");
printf("2. ordinare i dati a seconda della matricola;\n");
printf("3. ordinare i dati per voto di laurea.\n\n");
printf("Fa' la tua scelta.\n");
while(key != 0) {
scanf("%d",&key);
fflush(stdin);
switch(key) {
case(2):
freshman(studenti,cnt);
for(i=0;i<=cnt;i++) {
printf("%s %d %d", studenti[i].nome, studenti[i].matricola, studenti[i].voto_laurea); }
break;
case(3):
vote(studenti,cnt);
for(i=0;i<=cnt;i++) {
printf("%s %d %d", studenti[i].nome, studenti[i].matricola, studenti[i].voto_laurea); }
break;
default:
printf("Scelta non prevista.\n");
break;
}
}
return 0;
}
void vote(struct info *studenti, int c)
{
int i, i_min, j;
for(i=0;i<c-1;i++) {
i_min = i;
for(j=0;j<c;j++) {
if(studenti[j].voto_laurea<studenti[i].voto_laurea) {
i_min = j; }
}
studentiè[c+1] = studenti[i];
studenti[i] = studenti[i_min];
studenti[i_min] = studenti[c+1];
}
}
void freshman(struct info *studenti, int c)
{
int i, i_min, j;
for(i=0;i<c-1;i++) {
i_min = i;
for(j=0;j<c;j++) {
if(studenti[j].matricola<studenti[i].matricola) {
i_min = j; }
}
studenti[c+1] = studenti[i];
studenti[i] = studenti[i_min];
studenti[i_min] = studenti[c+1];
}
}