#include <stdio.h>
#include <string.h>
typedef struct student{
char name[100];
double height;
double weight;
} Student;
void swap_Student(Student *x, Student *y){
Student tmp = *x;
*x = *y;
*y = tmp;
}
void sort_by_name(Student a[], int n){
int i, j;
for(i = 0; i < n; i++){
for(j = n - 1; j > i; j--){
if(strcmp(a[j - 1].name, a[j].name) > 0) swap_Student(&a[j - 1], &a[j]);
}
}
}
int main(){
FILE *fp;
Student std[256];
int i, ninzu = 0;
double hsum = 0.0;
double wsum = 0.0;
if((fp = fopen("hw.dat", "r")) == NULL){
printf("ファイルをオープンできません\n");
return (-1);
}
while(fscanf(fp, "%s%lf%lf", std[ninzu].name, &std[ninzu].height, &std[ninzu].weight) == 3){
hsum += std[ninzu].height;
wsum += std[ninzu].weight;
ninzu++;
}
sort_by_name(std, ninzu);
for(i = 0; i < ninzu; i++){
printf("%-10s %5.1f %5.1f\n", std[i].name, std[i].height, std[i].weight);
}
printf("----------------------\n");
printf("平均 %5.1f %5.1f\n", hsum / ninzu, wsum / ninzu);
fclose(fp);
return 0;
}
0 件のコメント:
コメントを投稿