1、
#include <stdio.h> typedef struct{ char name[128]; double height; double weight; }Type1; void swap(Type1 *x, Type1 *y) { Type1 tmp = *x; *x = *y; *y = tmp; } void sort(Type1 x[], int n) { int i, j; for(i = 0; i < n - 1; i++) { for(j = n - 1; j > i; j--) { if(x[j - 1].height > x[j].height) { swap(&x[j - 1], &x[j]); } } } } int main(void) { FILE *fp; int lines = 0; char name[128]; double height, weight; double hsum = 0, wsum = 0; Type1 x[6]; if((fp = fopen("a.txt", "r")) == NULL) printf("\aFile open failed.\n"); else { while(fscanf(fp, "%s%lf%lf", name, &height, &weight) == 3) { printf("%-10s %5.1f %5.1f\n", name, height, weight); lines++; hsum += height; wsum += weight; } puts("\n================================\n"); printf("average %5.1f %5.1f\n\n\n", hsum/lines, wsum/lines); fp = fopen("a.txt", "r"); int i = 0; while(fscanf(fp, "%s%lf%lf", x[i].name, &x[i].height, &x[i].weight) == 3) { i++; } fclose(fp); sort(x, 6); puts("sort by height: \n"); for(i = 0; i < 6; i++) { printf("%-10s %5.1f %5.1f\n", x[i].name, x[i].height, x[i].weight); } } return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。