//動態配置結構陣列,
//讓使用者輸入n後,
//先讓使用者依序輸入n個學生姓名、身高、體重
//算出身高及體重平均,並輸出平均
//最後再輸出身高或體重任一項(一項兩項都可以)低於平均的學生姓名
//名字最長不會超過20字元
//承上題,
//輸入改為由檔案輸入
//輸出改為輸出至檔案
//程式流程:
//先後讀取輸入檔名與輸出檔名後,過程如上題,
//檔名不會超過50字元,程式畫面如下圖,
//讀入檔名時請分兩次(兩次 gets 或 scanf)
//測試資料可以在課程網頁下載
//內容有:
//輸入檔:可用複製貼上至黑黑的那個視窗
//輸出檔:答案,請仔細比較
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[21];
int height, weight;
};
int main(){
FILE *in, *out;
int i, n;
char str[51], string[51];
double sum_h = 0, sum_w = 0, H_avg, W_avg;
if (in == NULL) {
printf("open input file failed!\n"); return 0;
}
printf("Output file:%s", string
);
out
= fopen(string
, "w"); //
struct student
*a
= (struct student
*)malloc(sizeof(struct student
)*n
);
for (i = 0; i < n; i++){
fscanf(in
, "%s %d %d", a
[i
].
name, &a
[i
].
height, &a
[i
].
weight); }
for (i = 0; i < n; i++){
sum_h += a[i].height;
sum_w += a[i].weight;
}
H_avg = sum_h / n;
W_avg = sum_w / n;
fprintf(out
, "H_avg:%.2lf\n", H_avg
); fprintf(out
, "W_avg:%.2lf\n", W_avg
);
for (i = 0; i < n; i++){
if(a[i].height < H_avg || a[i].weight < W_avg){
//動態配置結構陣列,
//讓使用者輸入n後,
//先讓使用者依序輸入n個學生姓名、身高、體重
//算出身高及體重平均,並輸出平均
//最後再輸出身高或體重任一項(一項兩項都可以)低於平均的學生姓名
//名字最長不會超過20字元
//承上題,
//輸入改為由檔案輸入
//輸出改為輸出至檔案
//程式流程:
//先後讀取輸入檔名與輸出檔名後,過程如上題,
//檔名不會超過50字元,程式畫面如下圖,
//讀入檔名時請分兩次(兩次 gets 或 scanf)
//測試資料可以在課程網頁下載
//內容有:
//輸入檔:可用複製貼上至黑黑的那個視窗
//輸出檔:答案,請仔細比較
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[21];
int height, weight;
};
int main(){
FILE *in, *out;
int i, n;
char str[51], string[51];
double sum_h = 0, sum_w = 0, H_avg, W_avg;
if (in == NULL) {
printf("open input file failed!\n"); return 0;
}
printf("Output file:%s", string
);
out
= fopen(string
, "w"); //
struct student
*a
= (struct student
*)malloc(sizeof(struct student
)*n
);
for (i = 0; i < n; i++){
fscanf(in
, "%s %d %d", a
[i
].
name, &a
[i
].
height, &a
[i
].
weight); }
for (i = 0; i < n; i++){
sum_h += a[i].height;
sum_w += a[i].weight;
}
H_avg = sum_h / n;
W_avg = sum_w / n;
fprintf(out
, "H_avg:%.2lf\n", H_avg
); fprintf(out
, "W_avg:%.2lf\n", W_avg
);
for (i = 0; i < n; i++){
if(a[i].height < H_avg || a[i].weight < W_avg){
}
}
return 0;
}
}
}
return 0;
}
Ly/li5XmhYvphY3nva7ntZDmp4vpmaPliJfvvIwKLy/orpPkvb/nlKjogIXovLjlhaVu5b6M77yMCi8v5YWI6K6T5L2/55So6ICF5L6d5bqP6Ly45YWlbuWAi+WtuOeUn+Wnk+WQjeOAgei6q+mrmOOAgemrlOmHjQovL+eul+WHuui6q+mrmOWPiumrlOmHjeW5s+Wdh++8jOS4pui8uOWHuuW5s+WdhwovL+acgOW+jOWGjei8uOWHuui6q+mrmOaIlumrlOmHjeS7u+S4gOmghSjkuIDpoIXlhanpoIXpg73lj6/ku6Up5L2O5pa85bmz5Z2H55qE5a2455Sf5aeT5ZCNCi8v5ZCN5a2X5pyA6ZW35LiN5pyD6LaF6YGOMjDlrZflhYMKCi8v5om/5LiK6aGM77yMCi8v6Ly45YWl5pS554K655Sx5qqU5qGI6Ly45YWlCi8v6Ly45Ye65pS554K66Ly45Ye66Iez5qqU5qGICi8v56iL5byP5rWB56iL77yaCi8v5YWI5b6M6K6A5Y+W6Ly45YWl5qqU5ZCN6IiH6Ly45Ye65qqU5ZCN5b6M77yM6YGO56iL5aaC5LiK6aGM77yMCi8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