#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct student {
char name[20];
double height;
double weight;
double bmi;
};
// ファイルのオープン
FILE *file_open(void)
{
FILE *fp;
char s[100];
return fp;
}
// データ構造の用意
struct student *malloc_data(int *pc, FILE *fp)
{
struct student *data;
char buf[100];
while (fgets(buf
, 100, fp
)) (*pc
)++; data
= (struct student
*)malloc(*pc
* sizeof (struct student
)); return data;
}
// データの読み込み
void read_data(struct student *data, int c, FILE *fp)
{
char buf[100];
int i;
for (i
= 0; fgets(buf
, 100, fp
); i
++) { sscanf(buf
, "%s %lf %lf", data
[i
].
name, &data
[i
].
height, &data
[i
].
weight); }
}
// BMIの算出
void calc_bmi(struct student *data, int c)
{
int i;
double h;
for (i = 0; i < c; i++) {
h = data[i].height / 100;
data[i].bmi = data[i].weight / (h * h);
}
}
// BMIの大きい者順に並べ替える
void sort_bmi(struct student *data, int c)
{
struct student t;
int i, j;
for (i = 0; i < c - 1; i++) {
for (j = i + 1; j < c; j++) {
if (data[i].bmi < data[j].bmi) {
t = data[i];
data[i] = data[j];
data[j] = t;
}
}
}
}
// 画面表示
void print_data(struct student *data, int c)
{
int i;
for (i = 0; i < c; i++) {
printf("%s BMI=%f", data
[i
].
name, data
[i
].
bmi); if (data
[i
].
bmi > 25) printf(" himan"); }
}
int main(void)
{
struct student *data;
FILE *fp;
int c = 0;
fp = file_open(); // ファイルのオープン
if (fp == NULL) {
fprintf(stderr
, "file open error\n"); return 1;
}
data = malloc_data(&c, fp); // データ構造の用意
read_data(data, c, fp); // データの読み込み
calc_bmi(data, c); // BMIの算出
sort_bmi(data, c); // BMIの大きい者順に並べ替える
print_data(data, c); // 画面表示
return 0;
}
I2RlZmluZSBfQ1JUX1NFQ1VSRV9OT19XQVJOSU5HUwojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKc3RydWN0IHN0dWRlbnQgewoJY2hhcgluYW1lWzIwXTsKCWRvdWJsZQlo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